Skip to content

Commit

Permalink
Add support for the word "to" to indicate a range
Browse files Browse the repository at this point in the history
  • Loading branch information
jakeboone02 committed Feb 15, 2021
1 parent 2dc25ec commit 9929441
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 6 deletions.
54 changes: 54 additions & 0 deletions src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,60 @@ it('works', () => {
isGroupHeader: false,
},
]);
expect(parseIngredient('1\u20132 cups stuff')).toEqual([
{
quantity: 1,
quantity2: 2,
unitOfMeasure: 'cups',
description: 'stuff',
isGroupHeader: false,
},
]);
expect(parseIngredient('1\u20142 cups stuff')).toEqual([
{
quantity: 1,
quantity2: 2,
unitOfMeasure: 'cups',
description: 'stuff',
isGroupHeader: false,
},
]);
expect(parseIngredient('1 \u2013 2 cups stuff')).toEqual([
{
quantity: 1,
quantity2: 2,
unitOfMeasure: 'cups',
description: 'stuff',
isGroupHeader: false,
},
]);
expect(parseIngredient('1 \u2014 2 cups stuff')).toEqual([
{
quantity: 1,
quantity2: 2,
unitOfMeasure: 'cups',
description: 'stuff',
isGroupHeader: false,
},
]);
expect(parseIngredient('1 to 2 cups stuff')).toEqual([
{
quantity: 1,
quantity2: 2,
unitOfMeasure: 'cups',
description: 'stuff',
isGroupHeader: false,
},
]);
expect(parseIngredient('1 TO 2 cups stuff')).toEqual([
{
quantity: 1,
quantity2: 2,
unitOfMeasure: 'cups',
description: 'stuff',
isGroupHeader: false,
},
]);
expect(parseIngredient('1-NaN cups stuff')).toEqual([
{
quantity: 1,
Expand Down
17 changes: 11 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,13 +112,16 @@ const parseIngredient = (ingText: string): Ingredient[] => {
}

// Now check the description for a `quantity2` at the beginning.
// First we look for a dash to indicate a range, then process the next seven
// characters just like we did for `quantity`.
const firstChar = oIng.description.substring(0, 1);
if (firstChar === '-' || firstChar === '\u2013' || firstChar === '\u2014') {
// First we look for a dash, emdash, endash, or the word "to" to indicate
// a range, then process the next seven characters just like we did for
// `quantity`.
const q2re = /^(-|–|—|to )/i;
const q2reMatch = q2re.exec(oIng.description);
if (q2reMatch) {
const q2reMatchLen = q2reMatch[1].length;
const nqResultFirstChar = numericQuantity(
oIng.description
.substring(1)
.substring(q2reMatchLen)
.trim()
.substring(0, 1)
);
Expand All @@ -128,7 +131,9 @@ const parseIngredient = (ingText: string): Ingredient[] => {
let nqResult = NaN;

while (lenNum > 0 && isNaN(nqResult)) {
nqResult = numericQuantity(oIng.description.substring(1, lenNum));
nqResult = numericQuantity(
oIng.description.substring(q2reMatchLen, lenNum)
);

if (!isNaN(nqResult)) {
oIng.quantity2 = nqResult;
Expand Down

0 comments on commit 9929441

Please sign in to comment.