@jclind/ingredient-parser 1.0.25 → 1.0.26
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
export const convertFractions = (str) => {
|
|
2
|
+
const fractions = {
|
|
3
|
+
'¼': '1/4',
|
|
4
|
+
'½': '1/2',
|
|
5
|
+
'¾': '3/4',
|
|
6
|
+
'⅓': '1/3',
|
|
7
|
+
'⅔': '2/3',
|
|
8
|
+
'⅛': '1/8',
|
|
9
|
+
'⅜': '3/8',
|
|
10
|
+
'⅝': '5/8',
|
|
11
|
+
'⅞': '7/8',
|
|
12
|
+
'⅕': '1/5',
|
|
13
|
+
'⅖': '2/5',
|
|
14
|
+
'⅗': '3/5',
|
|
15
|
+
'⅘': '4/5',
|
|
16
|
+
'⅙': '1/6',
|
|
17
|
+
'⅚': '5/6',
|
|
18
|
+
'⅐': '1/7',
|
|
19
|
+
'⅑': '1/9',
|
|
20
|
+
'⅒': '1/10',
|
|
21
|
+
};
|
|
22
|
+
for (const [fraction, value] of Object.entries(fractions)) {
|
|
23
|
+
const re = new RegExp(fraction, 'g');
|
|
24
|
+
str = str.replace(re, value);
|
|
25
|
+
}
|
|
26
|
+
return str;
|
|
27
|
+
};
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { parse } from 'recipe-ingredient-parser-v3';
|
|
2
|
+
import { convertFractions } from './convertFractions.js';
|
|
2
3
|
export const parseIngredientString = (ingrStr) => {
|
|
3
4
|
// Define regular expressions for text inside parentheses and text before the first comma
|
|
4
5
|
const parenRegex = /\((.*?)\)/;
|
|
@@ -7,8 +8,11 @@ export const parseIngredientString = (ingrStr) => {
|
|
|
7
8
|
const commaIndex = ingrStr.indexOf(',');
|
|
8
9
|
// Extract the text inside the parentheses and the text before the first comma using regular expressions
|
|
9
10
|
const textInParentheses = ingrStr.match(parenRegex)?.[1] ?? '';
|
|
10
|
-
const comment = ingrStr
|
|
11
|
-
|
|
11
|
+
const comment = ingrStr
|
|
12
|
+
.replace(parenRegex, '')
|
|
13
|
+
.substring(commaIndex + 1)
|
|
14
|
+
.trim() + ` (${textInParentheses})`;
|
|
15
|
+
const ingrText = convertFractions(ingrStr.match(commaRegex)?.[1] ?? '');
|
|
12
16
|
const parsedIngrRes = parse(ingrText, 'eng');
|
|
13
17
|
if (!parsedIngrRes.ingredient) {
|
|
14
18
|
return { ...parsedIngrRes, originalIngredientString: ingrStr, comment };
|
package/package.json
CHANGED