@jclind/ingredient-parser 1.0.5 → 1.0.7
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.
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { parse } from 'recipe-ingredient-parser-v3';
|
|
2
2
|
import { getIngredientInfo } from './getIngredientInfo.js';
|
|
3
|
+
import { converter } from '@jclind/ingredient-unit-converter';
|
|
3
4
|
const editIngredientString = (ingrStr) => {
|
|
4
5
|
// Get string after first comment in ingredient string
|
|
5
6
|
let ingr = ingrStr.split(',')[0]?.trim() ?? ingrStr;
|
|
@@ -14,6 +15,17 @@ const editIngredientString = (ingrStr) => {
|
|
|
14
15
|
.trim();
|
|
15
16
|
return { formattedIngrName, comment };
|
|
16
17
|
};
|
|
18
|
+
const calculatePrice = (quantity, unit, price) => {
|
|
19
|
+
if (!quantity)
|
|
20
|
+
return price;
|
|
21
|
+
if (!unit)
|
|
22
|
+
return price * quantity;
|
|
23
|
+
const convertedUnit = converter(quantity, unit);
|
|
24
|
+
if (convertedUnit.error)
|
|
25
|
+
return convertedUnit.error;
|
|
26
|
+
const convertedGrams = convertedUnit.quantity;
|
|
27
|
+
return convertedGrams * price;
|
|
28
|
+
};
|
|
17
29
|
const ingredientParser = async (ingrString, spoonacularAPIKey) => {
|
|
18
30
|
const parsedIngr = parse(ingrString, 'eng');
|
|
19
31
|
const formattedIngr = editIngredientString(parsedIngr.ingredient);
|
|
@@ -25,11 +37,16 @@ const ingredientParser = async (ingrString, spoonacularAPIKey) => {
|
|
|
25
37
|
};
|
|
26
38
|
if (formattedIngr) {
|
|
27
39
|
const ingrData = await getIngredientInfo(formattedIngrName, spoonacularAPIKey);
|
|
28
|
-
|
|
40
|
+
const totalPrice = calculatePrice(parsedIngr.quantity, parsedIngr.unit, ingrData.estimatedCost.value);
|
|
41
|
+
return {
|
|
42
|
+
ingredientData: { ...ingrData, totalPrice },
|
|
43
|
+
parsedIngredient: updatedParsedIngr,
|
|
44
|
+
};
|
|
29
45
|
}
|
|
30
46
|
else {
|
|
31
47
|
return {
|
|
32
48
|
error: 'Ingredient not formatted correctly. Please pass ingredient comments/instructions after a comma',
|
|
49
|
+
ingredientData: null,
|
|
33
50
|
parsedIngredient: updatedParsedIngr,
|
|
34
51
|
};
|
|
35
52
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ingredientParser.js","sourceRoot":"","sources":["../../src/funcs/ingredientParser.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,6BAA6B,CAAA;AAEnD,OAAO,EAAE,iBAAiB,EAAE,MAAM,wBAAwB,CAAA;
|
|
1
|
+
{"version":3,"file":"ingredientParser.js","sourceRoot":"","sources":["../../src/funcs/ingredientParser.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,6BAA6B,CAAA;AAEnD,OAAO,EAAE,iBAAiB,EAAE,MAAM,wBAAwB,CAAA;AAC1D,OAAO,EAAE,SAAS,EAAE,MAAM,mCAAmC,CAAA;AAE7D,MAAM,oBAAoB,GAAG,CAAC,OAAe,EAAE,EAAE;IAC/C,sDAAsD;IACtD,IAAI,IAAI,GAAG,OAAO,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,EAAE,IAAI,EAAE,IAAI,OAAO,CAAA;IACnD,IAAI,OAAO,GAAG,OAAO,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,EAAE,IAAI,EAAE,IAAI,IAAI,CAAA;IAEnD,MAAM,iBAAiB,GAAG,IAAI;SAC3B,IAAI,EAAE;SACN,OAAO,CAAC,SAAS,EAAE,GAAG,CAAC;SACvB,OAAO,CAAC,IAAI,EAAE,EAAE,CAAC;SACjB,OAAO,CAAC,WAAW,EAAE,EAAE,CAAC;SACxB,OAAO,CAAC,QAAQ,EAAE,EAAE,CAAC;SACrB,OAAO,CAAC,QAAQ,EAAE,EAAE,CAAC;SACrB,IAAI,EA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ACL,KAAK,EACH,gGAAgG;YAClG,cAAc,EAAE,IAAI;YACpB,gBAAgB,EAAE,iBAAiB;SACpC,CAAA;KACF;AACH,CAAC,CAAA;AAED,eAAe,gBAAgB,CAAA"}
|
package/dist/index.js
CHANGED
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,gBAAgB,MAAM,6BAA6B,CAAA;
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,gBAAgB,MAAM,6BAA6B,CAAA;AAM1D,OAAO,EAAE,gBAAgB,EAA4C,CAAA;AACrE,4EAA4E"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jclind/ingredient-parser",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.7",
|
|
4
4
|
"description": "Parses given sentence including ingredient information and attemps to return quantity, measurement and ingredient data",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -22,7 +22,9 @@
|
|
|
22
22
|
},
|
|
23
23
|
"homepage": "https://github.com/jclind/ingredient-parser#readme",
|
|
24
24
|
"dependencies": {
|
|
25
|
+
"@jclind/ingredient-unit-converter": "^1.0.2",
|
|
25
26
|
"@types/axios": "^0.14.0",
|
|
27
|
+
"convert-units": "^2.3.4",
|
|
26
28
|
"dotenv": "^16.0.3",
|
|
27
29
|
"parse-ingredient": "^0.6.0",
|
|
28
30
|
"recipe-ingredient-parser-v3": "^1.2.18",
|