@jclind/ingredient-parser 1.0.25 → 1.0.27

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 = /\((.*?)\)/;
@@ -6,9 +7,24 @@ export const parseIngredientString = (ingrStr) => {
6
7
  // Find the index of the first ',' character
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
- const textInParentheses = ingrStr.match(parenRegex)?.[1] ?? '';
10
- const comment = ingrStr.substring(commaIndex + 1) + ' ' + textInParentheses;
11
- const ingrText = ingrStr.match(commaRegex)?.[1] ?? '';
10
+ const parenthesesStr = ingrStr.match(parenRegex)?.[1] ?? '';
11
+ const textInParenthesesStr = parenthesesStr ? ` (${parenthesesStr})` : '';
12
+ let ingrText;
13
+ let comment;
14
+ // If there is no comma in the string don't include a comment
15
+ if (commaIndex !== -1) {
16
+ ingrText = convertFractions(ingrStr.substring(0, commaIndex).replace(parenRegex, ''));
17
+ comment =
18
+ ingrStr
19
+ .replace(parenRegex, '')
20
+ .substring(commaIndex + 1)
21
+ .trim() + textInParenthesesStr;
22
+ }
23
+ else {
24
+ ingrText = convertFractions(ingrStr.replace(parenRegex, '').trim());
25
+ comment = textInParenthesesStr;
26
+ }
27
+ console.log(comment);
12
28
  const parsedIngrRes = parse(ingrText, 'eng');
13
29
  if (!parsedIngrRes.ingredient) {
14
30
  return { ...parsedIngrRes, originalIngredientString: ingrStr, comment };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jclind/ingredient-parser",
3
- "version": "1.0.25",
3
+ "version": "1.0.27",
4
4
  "description": "Parses given sentence including ingredient information and attemps to return quantity, measurement and ingredient data",
5
5
  "keywords": [
6
6
  "recipe",