@jclind/ingredient-parser 1.0.23 → 1.0.25
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.
- package/dist/api/http.js +0 -1
- package/dist/api/requests.js +0 -1
- package/dist/funcs/calculatePrice.js +1 -1
- package/dist/funcs/editIngredientString.js +1 -2
- package/dist/funcs/getIngredientInfo.js +0 -1
- package/dist/funcs/getSpoonacularIngrData.js +0 -1
- package/dist/funcs/ingredientParser.js +8 -17
- package/dist/funcs/parseIngredientString.js +31 -0
- package/dist/index.js +0 -1
- package/package.json +1 -1
package/dist/api/http.js
CHANGED
package/dist/api/requests.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
//@ts-ignore
|
|
1
2
|
import { converter } from '@jclind/ingredient-unit-converter';
|
|
2
3
|
export const calculatePrice = (quantity, unit, price) => {
|
|
3
4
|
if (!quantity)
|
|
@@ -17,4 +18,3 @@ export const calculatePrice = (quantity, unit, price) => {
|
|
|
17
18
|
const total = convertedGrams * price.estimatedGramPrice;
|
|
18
19
|
return Math.ceil(total * 100) / 100;
|
|
19
20
|
};
|
|
20
|
-
//# sourceMappingURL=calculatePrice.js.map
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
export const editIngredientString = (ingrStr) => {
|
|
2
2
|
// Get string after first comment in ingredient string
|
|
3
3
|
let ingr = ingrStr.split(',')[0]?.trim() ?? ingrStr;
|
|
4
|
-
let comment = ingrStr.split(',')[1]?.trim() ??
|
|
4
|
+
let comment = ingrStr.split(',')[1]?.trim() ?? '';
|
|
5
5
|
const wordsToRemove = ['small', 'medium', 'large', 'fresh', 'canned'];
|
|
6
6
|
const regex = new RegExp('\\b(' + wordsToRemove.join('|') + ')\\b', 'gi');
|
|
7
7
|
const formattedIngrName = ingr
|
|
@@ -15,4 +15,3 @@ export const editIngredientString = (ingrStr) => {
|
|
|
15
15
|
.trim();
|
|
16
16
|
return { formattedIngrName, comment };
|
|
17
17
|
};
|
|
18
|
-
//# sourceMappingURL=editIngredientString.js.map
|
|
@@ -1,29 +1,21 @@
|
|
|
1
|
-
import { parse } from 'recipe-ingredient-parser-v3';
|
|
2
1
|
import { calculatePrice } from './calculatePrice.js';
|
|
3
|
-
import {
|
|
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
|
|
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(
|
|
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:
|
|
15
|
+
parsedIngredient: parsedIngr ?? null,
|
|
24
16
|
};
|
|
25
17
|
}
|
|
26
|
-
if (
|
|
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:
|
|
29
|
+
parsedIngredient: parsedIngr,
|
|
38
30
|
};
|
|
39
31
|
}
|
|
40
32
|
else {
|
|
@@ -43,9 +35,8 @@ 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:
|
|
38
|
+
parsedIngredient: parsedIngr,
|
|
47
39
|
};
|
|
48
40
|
}
|
|
49
41
|
};
|
|
50
42
|
export default ingredientParser;
|
|
51
|
-
//# sourceMappingURL=ingredientParser.js.map
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { parse } from 'recipe-ingredient-parser-v3';
|
|
2
|
+
export const parseIngredientString = (ingrStr) => {
|
|
3
|
+
// Define regular expressions for text inside parentheses and text before the first comma
|
|
4
|
+
const parenRegex = /\((.*?)\)/;
|
|
5
|
+
const commaRegex = /^(.*?)(?=,)/;
|
|
6
|
+
// Find the index of the first ',' character
|
|
7
|
+
const commaIndex = ingrStr.indexOf(',');
|
|
8
|
+
// 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] ?? '';
|
|
12
|
+
const parsedIngrRes = parse(ingrText, 'eng');
|
|
13
|
+
if (!parsedIngrRes.ingredient) {
|
|
14
|
+
return { ...parsedIngrRes, originalIngredientString: ingrStr, comment };
|
|
15
|
+
}
|
|
16
|
+
const wordsToRemove = ['small', 'medium', 'large', 'fresh', 'canned'];
|
|
17
|
+
const regex = new RegExp('\\b(' + wordsToRemove.join('|') + ')\\b', 'gi');
|
|
18
|
+
const formattedIngrName = parsedIngrRes.ingredient
|
|
19
|
+
.trim()
|
|
20
|
+
.replace(/\s{2,}/g, ' ') // Replace multiple whitespace characters with a single space
|
|
21
|
+
.replace(/,/g, '') // Remove commas
|
|
22
|
+
.replace(/^(fluid|fl|oz) /, '') // Remove "fluid ", "fl ", or "oz " at the beginning of the string
|
|
23
|
+
.replace(regex, '')
|
|
24
|
+
.trim();
|
|
25
|
+
return {
|
|
26
|
+
...parsedIngrRes,
|
|
27
|
+
ingredient: formattedIngrName,
|
|
28
|
+
originalIngredientString: ingrStr,
|
|
29
|
+
comment,
|
|
30
|
+
};
|
|
31
|
+
};
|
package/dist/index.js
CHANGED
package/package.json
CHANGED