Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions src/__tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down Expand Up @@ -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', () => {
Expand Down
2 changes: 1 addition & 1 deletion src/utils/findQuantityAndConvertIfUnicode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: '⅝'
Expand Down
5 changes: 4 additions & 1 deletion website/docs/guides/unicode-fractions.md
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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.