diff --git a/src/__tests__/index.test.ts b/src/__tests__/index.test.ts index a191019..2e5bd44 100644 --- a/src/__tests__/index.test.ts +++ b/src/__tests__/index.test.ts @@ -140,6 +140,39 @@ describe('ingredient parser', () => { ); }); } + + const mixedValuesWithSpace = [ + '1 ¼', + '2 ½', + '3 ¾', + '4 ⅐', + '5 ⅑', + '6 ⅒', + '7 ⅓', + '8 ⅔', + '9 ⅕', + '10 ⅖', + '11 ⅗', + '12 ⅘', + '13 ⅙', + '14 ⅚', + '15 ⅛', + '16 ⅜', + '17 ⅝', + '18 ⅞', + ]; + + for (let u = 0; u < mixedValuesWithSpace.length; u++) { + const element = mixedValuesWithSpace[u]; + const expectedAmount = ( + Number(mixedExpectedValues[u]) + Number(unicodeExpectedAmounts[u]) + ).toString(); + it(`${element} to ${expectedAmount}`, () => { + expect(parse(`${element} teaspoon water`).quantity).toBe( + expectedAmount, + ); + }); + } }); it('150 cl of milk', () => { @@ -307,6 +340,17 @@ describe('ingredient parser', () => { maxQty: '1', }); }); + it('"1 ¼ cups milk"', () => { + expect(parse('1 ¼ cups milk')).toEqual({ + unit: 'cups', + symbol: 'c', + quantity: '1.25', + ingredient: 'milk', + article: null, + minQty: '1.25', + maxQty: '1.25', + }); + }) }); it('translates unit when no unit provided', () => { diff --git a/src/utils/findQuantityAndConvertIfUnicode.ts b/src/utils/findQuantityAndConvertIfUnicode.ts index fdc0a82..6550aa3 100644 --- a/src/utils/findQuantityAndConvertIfUnicode.ts +++ b/src/utils/findQuantityAndConvertIfUnicode.ts @@ -38,7 +38,7 @@ export default function findQuantityAndConvertIfUnicode( `^(\\d+\\-\\d+)|^(\\d+\\s\\-\\s\\d+)|^(\\d+\\s(${prepositions})\\s\\d+)`, 'g', ); // for ex: "1 to 2" or "1 - 2" - const unicodeFractionRegex = /\d*[^\u0020-\u007F]+/g; + const unicodeFractionRegex = /\d*\s*[^\u0020-\u007F]+/g; const onlyUnicodeFraction = /[^\u0020-\u007F]+/g; // found a unicode quantity inside our regex, for ex: '⅝' diff --git a/website/docs/guides/unicode-fractions.md b/website/docs/guides/unicode-fractions.md index b974ff6..911e43c 100644 --- a/website/docs/guides/unicode-fractions.md +++ b/website/docs/guides/unicode-fractions.md @@ -31,7 +31,7 @@ sidebar_position: 4 ## Mixed Numbers -Unicode fractions can be combined with whole numbers: +Unicode fractions can be combined with whole numbers, with or without a space between them: ```typescript import parse from '@magrinj/parse-ingredients'; @@ -41,6 +41,9 @@ parse('2½ cups flour'); parse('1¾ teaspoons salt'); // { quantity: '1.75', unit: 'teaspoon', ingredient: 'salt', ... } + +parse('1 ¼ cups milk'); +// { quantity: '1.25', unit: 'cups', ingredient: 'milk', ... } ``` These are commonly found in recipe content copied from websites, PDFs, or cookbooks that use special typography.