Skip to content

Commit

Permalink
feat(core): parse portions
Browse files Browse the repository at this point in the history
  • Loading branch information
Apkawa committed Feb 16, 2024
1 parent 5c2dd37 commit 9a91d19
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,8 @@ export function recipeToText(recipe: Recipe, lang: LANG_TYPE = 'ru'): string {
s += `${name} - ${value} ${getUnitDisplay(i.unit, lang, i.value)}\n`;
}
}
if (recipe.portion) {
s += `\nРецепт на ${recipe.portion} порций \n`;
}
return s;
}
13 changes: 13 additions & 0 deletions packages/food-recipe-core/src/food_recipe/core/parser/text.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,15 @@ export function parseTextRecipe(raw_text: string): Recipe {
}
const result = parseRecipeLine(line);
if (result) {
if (result.unit == 'portion') {
const v = result.value || 1;
if (typeof v == 'number') {
recipe.portion = v;
} else {
recipe.portion = v[0];
}
continue;
}
if (i == 1 && !result.name && recipe.name) {
// Скорее всего рецепт оказался без названия, а формат вида "название\n вес \n"
group.name = recipe.name;
Expand All @@ -35,6 +44,10 @@ export function parseTextRecipe(raw_text: string): Recipe {
// Наверное это какое то имя
const name = stripLine(line);
if (i == 0) {
if (name == END_LINE) {
// Пустой рецепт
break;
}
// maybe name
recipe.name = name;
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import {Unit} from '../unit/constants';

export interface Recipe {
name: string;
portion?: number;
calculated_portion?: number;
description?: string;
ingredient_groups: IngredientGroup[];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export const MEASURE_UNITS = [

export const COUNT_UNITS = [
'pcs', // шт, штуки, piece
'portion', // Special unit for parse portions
];

export const VALUE_UNITS = [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,12 @@ export const UNIT_PARSE_MAP: UnitMap = {
US_gill: ['gi', 'gill'],
US_pint: ['pi', 'pints?', buildPluralRe('пинт', ['а', 'ы'])],
US_quart: ['qt', 'quarts?', buildPluralRe('кварт', ['а', 'ы'])],
// Special
portion: [
'serves?',
'dish(?:es)',
'portions?',
buildPluralRe('порци', ['я', 'и', 'й']),
buildPluralRe('блюд', ['о', 'а']),
],
};
37 changes: 37 additions & 0 deletions packages/food-recipe-core/tests/unit/core/parser/parseText.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,43 @@
import {parseTextRecipe} from '@app/core/parser/text';

describe('parseTextRecipe', () => {
it('Empty recipe', () => {
const t = ``;
expect(parseTextRecipe(t)).toStrictEqual({
ingredient_groups: [],
name: '',
});
});
it('Only text', () => {
const t = `Foo
Bar
Baz`;
expect(parseTextRecipe(t)).toStrictEqual({
ingredient_groups: [],
name: 'Foo',
});
});
it('Parse portions', () => {
const t = `Foo
2 portions
4 onions`;
expect(parseTextRecipe(t)).toStrictEqual({
ingredient_groups: [
{
ingredients: [
{
name: 'onions',
unit: 'pcs',
value: 4,
},
],
name: '',
},
],
portion: 2,
name: 'Foo',
});
});
it('generic', () => {
const t = `Яйцо перепелиное – 6 шт.
Масло растительное – 150 мл
Expand Down

0 comments on commit 9a91d19

Please sign in to comment.