@jclind/ingredient-parser 1.0.28 → 1.0.29
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.
|
@@ -2,24 +2,30 @@ import { parse } from 'recipe-ingredient-parser-v3';
|
|
|
2
2
|
import { convertFractions } from './convertFractions.js';
|
|
3
3
|
export const parseIngredientString = (ingrStr) => {
|
|
4
4
|
// Define regular expressions for text inside parentheses and text before the first comma
|
|
5
|
-
const parenRegex =
|
|
5
|
+
const parenRegex = /(\(.*?\))/;
|
|
6
6
|
const commaRegex = /^(.*?)(?=,)/;
|
|
7
|
-
// Find the index of the first ',' character
|
|
8
|
-
const commaIndex = ingrStr.indexOf(',');
|
|
9
7
|
// Extract the text inside the parentheses and the text before the first comma using regular expressions
|
|
10
8
|
const parenthesesStr = ingrStr.match(parenRegex)?.[1] ?? '';
|
|
11
|
-
const
|
|
9
|
+
const textWithoutParenthesesStr = ingrStr.replace(parenthesesStr, '');
|
|
10
|
+
// Find the index of the first ',' character
|
|
11
|
+
const commaIndex = textWithoutParenthesesStr.indexOf(',');
|
|
12
12
|
let ingrText;
|
|
13
13
|
let comment;
|
|
14
14
|
// If there is no comma in the string don't include a comment
|
|
15
15
|
if (commaIndex !== -1) {
|
|
16
|
-
ingrText = convertFractions(
|
|
17
|
-
|
|
18
|
-
|
|
16
|
+
ingrText = convertFractions(textWithoutParenthesesStr
|
|
17
|
+
.substring(0, commaIndex)
|
|
18
|
+
.replace(parenRegex, '')
|
|
19
|
+
.trim());
|
|
20
|
+
comment = (textWithoutParenthesesStr
|
|
21
|
+
.replace(parenRegex, '')
|
|
22
|
+
.substring(commaIndex + 1) +
|
|
23
|
+
' ' +
|
|
24
|
+
parenthesesStr).trim();
|
|
19
25
|
}
|
|
20
26
|
else {
|
|
21
27
|
ingrText = convertFractions(ingrStr.replace(parenRegex, '').trim());
|
|
22
|
-
comment =
|
|
28
|
+
comment = parenthesesStr.trim();
|
|
23
29
|
}
|
|
24
30
|
console.log(ingrText, comment, parenthesesStr);
|
|
25
31
|
const parsedIngrRes = parse(ingrText, 'eng');
|
package/package.json
CHANGED