@jclind/ingredient-parser 1.0.24 → 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,29 +1,21 @@
1
- import { parse } from 'recipe-ingredient-parser-v3';
2
1
  import { calculatePrice } from './calculatePrice.js';
3
- import { editIngredientString } from './editIngredientString.js';
2
+ import { parseIngredientString } from './parseIngredientString.js';
4
3
  import { getIngredientInfo } from './getIngredientInfo.js';
5
4
  const ingredientParser = async (ingrString, spoonacularAPIKey) => {
6
- const parsedIngr = parse(ingrString, 'eng');
7
- const formattedIngr = editIngredientString(parsedIngr.ingredient ?? '');
8
- const { formattedIngrName, comment } = formattedIngr;
9
- const updatedParsedIngr = {
10
- ...parsedIngr,
11
- ingredient: formattedIngrName,
12
- originalIngredientString: ingrString,
13
- comment,
14
- };
5
+ // const parsedIngr: ParsedIngredient = parse(ingrString, 'eng')
6
+ const parsedIngr = parseIngredientString(ingrString);
15
7
  let ingrData = null;
16
8
  try {
17
- ingrData = await getIngredientInfo(formattedIngrName, spoonacularAPIKey);
9
+ ingrData = await getIngredientInfo(parsedIngr.ingredient || '', spoonacularAPIKey);
18
10
  }
19
11
  catch (error) {
20
12
  return {
21
13
  error: { message: error.message },
22
14
  ingredientData: null,
23
- parsedIngredient: updatedParsedIngr ?? null,
15
+ parsedIngredient: parsedIngr ?? null,
24
16
  };
25
17
  }
26
- if (formattedIngr && ingrData) {
18
+ if (parsedIngr.ingredient && ingrData) {
27
19
  const { estimatedPrices, meta, categoryPath, unit, unitShort, unitLong, original, id, ...reducedIngrData } = ingrData;
28
20
  const totalPrice = calculatePrice(parsedIngr.quantity, parsedIngr.unit, estimatedPrices);
29
21
  const imagePath = `https://spoonacular.com/cdn/ingredients_100x100/${reducedIngrData.image}`;
@@ -34,7 +26,7 @@ const ingredientParser = async (ingrString, spoonacularAPIKey) => {
34
26
  };
35
27
  return {
36
28
  ingredientData: updatedIngrData,
37
- parsedIngredient: updatedParsedIngr,
29
+ parsedIngredient: parsedIngr,
38
30
  };
39
31
  }
40
32
  else {
@@ -43,7 +35,7 @@ const ingredientParser = async (ingrString, spoonacularAPIKey) => {
43
35
  message: 'Ingredient not formatted correctly or Ingredient Unknown. Please pass ingredient comments/instructions after a comma',
44
36
  },
45
37
  ingredientData: null,
46
- parsedIngredient: updatedParsedIngr,
38
+ parsedIngredient: parsedIngr,
47
39
  };
48
40
  }
49
41
  };
@@ -0,0 +1,35 @@
1
+ import { parse } from 'recipe-ingredient-parser-v3';
2
+ import { convertFractions } from './convertFractions.js';
3
+ export const parseIngredientString = (ingrStr) => {
4
+ // Define regular expressions for text inside parentheses and text before the first comma
5
+ const parenRegex = /\((.*?)\)/;
6
+ const commaRegex = /^(.*?)(?=,)/;
7
+ // Find the index of the first ',' character
8
+ const commaIndex = ingrStr.indexOf(',');
9
+ // Extract the text inside the parentheses and the text before the first comma using regular expressions
10
+ const textInParentheses = ingrStr.match(parenRegex)?.[1] ?? '';
11
+ const comment = ingrStr
12
+ .replace(parenRegex, '')
13
+ .substring(commaIndex + 1)
14
+ .trim() + ` (${textInParentheses})`;
15
+ const ingrText = convertFractions(ingrStr.match(commaRegex)?.[1] ?? '');
16
+ const parsedIngrRes = parse(ingrText, 'eng');
17
+ if (!parsedIngrRes.ingredient) {
18
+ return { ...parsedIngrRes, originalIngredientString: ingrStr, comment };
19
+ }
20
+ const wordsToRemove = ['small', 'medium', 'large', 'fresh', 'canned'];
21
+ const regex = new RegExp('\\b(' + wordsToRemove.join('|') + ')\\b', 'gi');
22
+ const formattedIngrName = parsedIngrRes.ingredient
23
+ .trim()
24
+ .replace(/\s{2,}/g, ' ') // Replace multiple whitespace characters with a single space
25
+ .replace(/,/g, '') // Remove commas
26
+ .replace(/^(fluid|fl|oz) /, '') // Remove "fluid ", "fl ", or "oz " at the beginning of the string
27
+ .replace(regex, '')
28
+ .trim();
29
+ return {
30
+ ...parsedIngrRes,
31
+ ingredient: formattedIngrName,
32
+ originalIngredientString: ingrStr,
33
+ comment,
34
+ };
35
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jclind/ingredient-parser",
3
- "version": "1.0.24",
3
+ "version": "1.0.26",
4
4
  "description": "Parses given sentence including ingredient information and attemps to return quantity, measurement and ingredient data",
5
5
  "keywords": [
6
6
  "recipe",