Skip to content

Commit

Permalink
Accept unicode dash for range and accept / for alternative ingredient (
Browse files Browse the repository at this point in the history
…#168)

* Added special dash as range marker

* Complete adjustments for issue 167

* Lintfix
  • Loading branch information
jlucaspains authored Jan 11, 2025
1 parent 04dd426 commit 2fd0f9f
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 7 deletions.
7 changes: 6 additions & 1 deletion src/ingredientParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,12 @@ export function parseIngredient(
let alternativeQuantities = [];
let alternativeQtyIndex = unitEndIndex;
let ingredientStartIndex = unitEndIndex;
if (tokens[alternativeQtyIndex + 1] === "(") {
if (
tokens[alternativeQtyIndex] === "(" ||
tokens[alternativeQtyIndex + 1] === "(" ||
tokens[alternativeQtyIndex] === "/" ||
tokens[alternativeQtyIndex + 1] === "/"
) {
const [
alternativeFirstQuantity,
alternativeQuantity,
Expand Down
2 changes: 1 addition & 1 deletion src/units.en.js
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ ingredientQuantities.set("eight", 8);
ingredientQuantities.set("nine", 9);
ingredientQuantities.set("ten", 10);

const ingredientRangeMarker = ["to", "-", "or"];
const ingredientRangeMarker = ["to", "-", "–", "or"];

const ingredientQuantityAddMarker = ["and"];

Expand Down
2 changes: 1 addition & 1 deletion src/units.pt.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ ingredientQuantities.set("oito", 8);
ingredientQuantities.set("nove", 9);
ingredientQuantities.set("dez", 10);

const ingredientRangeMarker = ["a", "-", "ou"];
const ingredientRangeMarker = ["a", "-", "–", "ou"];

const ingredientQuantityAddMarker = ["e"];

Expand Down
51 changes: 49 additions & 2 deletions test/index_en.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,7 @@ describe("Parse ingredient en-US", () => {
["1 tbsp . flour", 1, "1", "tablespoon", "flour", ""],
["1 and 1/2 cups flour", 1.5, "1 and 1/2", "cup", "flour", ""],
["carrot/parsnip", 0, "", "", "carrot/parsnip", ""],
["1/parsnip", 1, "1", "", "/parsnip", ""],
["carrot/1", 1, "1", "", "", ""],
["1 1/carrot", 1, "1 1", "", "/carrot", ""],
["1", 1, "1", "", "", ""],
];
it.each(table)(
Expand Down Expand Up @@ -273,6 +271,55 @@ describe("Parse ingredient with options EN", () => {
"gram",
],
["120g (1 cup) of flour", 120, 120, 120, "gram", "flour", 1, 1, 1, "cup"],
["120g(1 cup) of flour", 120, 120, 120, "gram", "flour", 1, 1, 1, "cup"],
[
"½-1 tsp dried chilli flakes, or to taste",
1,
0.5,
1,
"teaspoon",
"dried chilli flakes",
0,
0,
0,
"",
],
[
"½–1 tsp dried chilli flakes, or to taste",
1,
0.5,
1,
"teaspoon",
"dried chilli flakes",
0,
0,
0,
"",
],
[
"300g/10½oz onions, thinly sliced",
300,
300,
300,
"gram",
"onions",
10.5,
10.5,
10.5,
"ounce",
],
[
"300g / 10½oz onions, thinly sliced",
300,
300,
300,
"gram",
"onions",
10.5,
10.5,
10.5,
"ounce",
],
[
"100-200g (1-2 cup) of flour",
200,
Expand Down
40 changes: 38 additions & 2 deletions test/index_pt.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,7 @@ describe("Parse ingredient pt-BR", () => {
["10ml de agua", 10, "10", "mililitro", "agua", ""],
["1 e 1/2 copo de agua", 1.5, "1 e 1/2", "copo", "agua", ""],
["cenoura/laranja", 0, "", "", "cenoura/laranja", ""],
["1/cenoura", 1, "1", "", "/cenoura", ""],
["cenoura/1", 1, "1", "", "", ""],
["1 1/cenoura", 1, "1 1", "", "/cenoura", ""],
["1", 1, "1", "", "", ""],
];
it.each(table)(
Expand Down Expand Up @@ -364,6 +362,44 @@ describe("Parse ingredient with options pt-BR", () => {
1,
"copo",
],
[
"120g(1 copo) farinha",
120,
120,
120,
"grama",
"farinha",
1,
1,
1,
"copo",
],
["½-1 colher pimenta", 1, 0.5, 1, "colher", "pimenta", 0, 0, 0, ""],
["½–1 colher pimenta", 1, 0.5, 1, "colher", "pimenta", 0, 0, 0, ""],
[
"500g/½kg cebola",
500,
500,
500,
"grama",
"cebola",
0.5,
0.5,
0.5,
"quilograma",
],
[
"500g / ½kg cebola",
500,
500,
500,
"grama",
"cebola",
0.5,
0.5,
0.5,
"quilograma",
],
[
"100-200g (1-2 copo) farinha",
200,
Expand Down

0 comments on commit 2fd0f9f

Please sign in to comment.