Skip to content

Commit d130a4f

Browse files
committed
fix: parsing of single-word ingredient ending with {}
Fixes: #2 e.g. @flour{}
1 parent c3e9ab4 commit d130a4f

File tree

4 files changed

+51
-32
lines changed

4 files changed

+51
-32
lines changed

src/regex.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ export const metadataRegex = /---\n(.*?)\n---/s;
33
const multiwordIngredient =
44
/@(?<mIngredientModifier>[@\-&+*!?])?(?<mIngredientName>(?:[^\s@#~\[\]{(.,;:!?]+(?:\s+[^\s@#~\[\]{(.,;:!?]+)+))(?=\s*(?:\{|\}|\(\s*[^)]*\s*\)))(?:\{(?<mIngredientQuantity>\p{No}|(?:\p{Nd}+(?:[.,\/][\p{Nd}]+)?))?(?:%(?<mIngredientUnits>[^}]+?))?\})?(?:\((?<mIngredientPreparation>[^)]*?)\))?/gu;
55
const singleWordIngredient =
6-
/@(?<sIngredientModifier>[@\-&+*!?])?(?<sIngredientName>[^\s@#~\[\]{(.,;:!?]+)(?:\{(?<sIngredientQuantity>\p{No}|(?:\p{Nd}+(?:[.,\/][\p{Nd}]+)?))(?:%(?<sIngredientUnits>[^}]+?))?\})?(?:\((?<sIngredientPreparation>[^)]*?)\))?/gu;
6+
/@(?<sIngredientModifier>[@\-&+*!?])?(?<sIngredientName>[^\s@#~\[\]{(.,;:!?]+)(?:\{(?:(?<sIngredientQuantity>\p{No}|(?:\p{Nd}+(?:[.,\/][\p{Nd}]+)?))(?:%(?<sIngredientUnits>[^}]+?))?)?\})?(?:\((?<sIngredientPreparation>[^)]*?)\))?/gu;
77

88
const multiwordCookware =
99
/#(?<mCookwareModifier>[\-&+*!?])?(?<mCookwareName>(?:[^\s@#~\[\]{(.,;:!?]+(?:\s+[^\s@#~\[\]{(.,;:!?]+)+))(?=\s*(?:\{|\}|\(\s*[^)]*\s*\)))\{(?<mCookwareQuantity>.*?)\}/;

test/__snapshots__/recipe_parsing.test.ts.snap

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,20 @@ exports[`parse function > extracts steps correctly 1`] = `
2424
},
2525
{
2626
"type": "text",
27-
"value": ", and add ",
27+
"value": ". Mix with some ",
2828
},
2929
{
3030
"type": "ingredient",
3131
"value": 1,
3232
},
33+
{
34+
"type": "text",
35+
"value": " and add ",
36+
},
37+
{
38+
"type": "ingredient",
39+
"value": 2,
40+
},
3341
{
3442
"type": "text",
3543
"value": ".",
@@ -44,7 +52,7 @@ exports[`parse function > extracts steps correctly 1`] = `
4452
},
4553
{
4654
"type": "ingredient",
47-
"value": 2,
55+
"value": 3,
4856
},
4957
{
5058
"type": "text",

test/fixtures/recipes.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ title: Pancakes
44
tags: [breakfast, easy]
55
---
66
7-
Crack the @eggs{3} into a #bowl, and add @coarse salt{}.
7+
Crack the @eggs{3} into a #bowl. Mix with some @flour{} and add @coarse salt{}.
88
99
Melt the @butter{50%g} in a #pan{} on medium heat.
1010

test/recipe_parsing.test.ts

Lines changed: 39 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -11,34 +11,45 @@ describe("parse function", () => {
1111

1212
it("extracts ingredients correctly", () => {
1313
const result = new Recipe(simpleRecipe);
14-
expect(result.ingredients.length).toBe(3);
15-
expect(result.ingredients[0]).toEqual({
16-
name: "eggs",
17-
optional: false,
18-
hidden: false,
19-
quantity: 3,
20-
unit: undefined,
21-
preparation: undefined,
22-
isRecipe: false,
23-
});
24-
expect(result.ingredients[1]).toEqual({
25-
name: "coarse salt",
26-
optional: false,
27-
hidden: false,
28-
quantity: undefined,
29-
unit: undefined,
30-
preparation: undefined,
31-
isRecipe: false,
32-
});
33-
expect(result.ingredients[2]).toEqual({
34-
name: "butter",
35-
optional: false,
36-
hidden: false,
37-
quantity: 50,
38-
unit: "g",
39-
preparation: undefined,
40-
isRecipe: false,
41-
});
14+
expect(result.ingredients.length).toBe(4);
15+
expect(result.ingredients).toEqual([
16+
{
17+
name: "eggs",
18+
optional: false,
19+
hidden: false,
20+
quantity: 3,
21+
unit: undefined,
22+
preparation: undefined,
23+
isRecipe: false,
24+
},
25+
{
26+
name: "flour",
27+
optional: false,
28+
hidden: false,
29+
quantity: undefined,
30+
unit: undefined,
31+
preparation: undefined,
32+
isRecipe: false,
33+
},
34+
{
35+
name: "coarse salt",
36+
hidden: false,
37+
isRecipe: false,
38+
optional: false,
39+
preparation: undefined,
40+
quantity: undefined,
41+
unit: undefined,
42+
},
43+
{
44+
name: "butter",
45+
optional: false,
46+
hidden: false,
47+
quantity: 50,
48+
unit: "g",
49+
preparation: undefined,
50+
isRecipe: false,
51+
},
52+
]);
4253
});
4354

4455
it("parses ingredients that are other recipes", () => {

0 commit comments

Comments
 (0)