@jclind/ingredient-parser 1.0.42 → 1.1.1

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/index.js CHANGED
@@ -17,4 +17,12 @@ Object.defineProperty(exports, "__esModule", { value: true });
17
17
  exports.ingredientParser = void 0;
18
18
  const ingredientParser_1 = require("./src/funcs/ingredientParser");
19
19
  exports.ingredientParser = ingredientParser_1.default;
20
+ // const test = async () => {
21
+ // const ingrData = await ingredientParser(
22
+ // '1 lb boneless skinless chicken breast',
23
+ // 'e8064d74f9804ebd82a947fb7b86189b'
24
+ // )
25
+ // return console.log(ingrData)
26
+ // }
27
+ // test()
20
28
  __exportStar(require("./types"), exports);
@@ -1,7 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.calculatePrice = void 0;
4
- //@ts-ignore
5
4
  const ingredient_unit_converter_1 = require("@jclind/ingredient-unit-converter");
6
5
  const calculatePrice = (quantity, unit, price) => {
7
6
  if (!quantity)
@@ -15,7 +14,7 @@ const calculatePrice = (quantity, unit, price) => {
15
14
  catch (error) {
16
15
  return null;
17
16
  }
18
- if (!convertedUnit || convertedUnit.error)
17
+ if (!convertedUnit || 'error' in convertedUnit)
19
18
  return price.estimatedSingleUnitPrice * quantity;
20
19
  const convertedGrams = Number(convertedUnit.quantity);
21
20
  const total = convertedGrams * price.estimatedGramPrice;
@@ -1,9 +1,9 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.parseIngredientString = void 0;
4
- const recipe_ingredient_parser_v3_1 = require("recipe-ingredient-parser-v3");
5
4
  // import { ParsedIngredient } from '../../index.js'
6
5
  const convertFractions_js_1 = require("./convertFractions.js");
6
+ const parseStringConsecutiveTs_js_1 = require("./parseStringConsecutiveTs.js");
7
7
  const parseIngredientString = (ingrStr) => {
8
8
  var _a, _b;
9
9
  // Define regular expressions for text inside parentheses and text before the first comma
@@ -32,16 +32,22 @@ const parseIngredientString = (ingrStr) => {
32
32
  ingrText = (0, convertFractions_js_1.convertFractions)(ingrStr.replace(parenRegex, '').trim());
33
33
  comment = parenthesesStr.trim();
34
34
  }
35
- const prepIngrText = ingrText.replace(/\b(lb|lbs)\b/g, match => {
36
- // Replace lb or lbs with pound or pounds respectively
35
+ const prepIngrText = ingrText.replace(/\b(lb|lbs|tablespoon|tablespoon)\b/gi, match => {
37
36
  if (match === 'lb') {
38
37
  return 'pound';
39
38
  }
40
- else {
39
+ else if (match === 'lbs') {
41
40
  return 'pounds';
42
41
  }
42
+ else if (match === 'tablespoon') {
43
+ return 'tbsp';
44
+ }
45
+ else {
46
+ return 'tbsps';
47
+ }
43
48
  });
44
- const parsedIngrRes = (0, recipe_ingredient_parser_v3_1.parse)(prepIngrText, 'eng');
49
+ const parsedIngrRes = (0, parseStringConsecutiveTs_js_1.parseStringConsecutiveTs)(prepIngrText);
50
+ console.log(parsedIngrRes);
45
51
  if (!parsedIngrRes.ingredient) {
46
52
  return Object.assign(Object.assign({}, parsedIngrRes), { originalIngredientString: ingrStr, comment });
47
53
  }
@@ -0,0 +1,4 @@
1
+ import { ParsedIngredient } from '../../types';
2
+ type ParsedIngredientOmitType = Omit<ParsedIngredient, 'originalIngredientString' | 'comment'>;
3
+ export declare const parseStringConsecutiveTs: (ingrStr: string) => ParsedIngredientOmitType;
4
+ export {};
@@ -0,0 +1,43 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.parseStringConsecutiveTs = void 0;
4
+ const recipe_ingredient_parser_v3_1 = require("recipe-ingredient-parser-v3");
5
+ // returns words that have consecutive t's with those t's removed in a string array
6
+ const removeConsecutiveTs = (str) => {
7
+ const regex = /t{2,}/gi;
8
+ const words = str.split(' ');
9
+ const modifiedWords = words
10
+ .map(word => {
11
+ const modified = word.replace(regex, '');
12
+ return {
13
+ original: word,
14
+ modified: modified,
15
+ };
16
+ })
17
+ .filter(({ original, modified }) => original !== modified && modified !== '');
18
+ return modifiedWords;
19
+ };
20
+ const replaceModifiedWords = (words, str) => {
21
+ let updatedStr = str;
22
+ words.forEach(({ original, modified }) => {
23
+ updatedStr = updatedStr.replace(modified, original);
24
+ });
25
+ return updatedStr;
26
+ };
27
+ const parseStringConsecutiveTs = (ingrStr) => {
28
+ var _a;
29
+ // Fixes bug where words with 'tt' in them will replace
30
+ const removeTTsIngrName = ingrStr.replace(/t{2,}/g, '');
31
+ const modifiedWords = removeConsecutiveTs(ingrStr);
32
+ if (modifiedWords.length > 0) {
33
+ const parsedIngrNoTs = (0, recipe_ingredient_parser_v3_1.parse)(removeTTsIngrName, 'eng');
34
+ console.log('parsedIngrNoTs', parsedIngrNoTs);
35
+ const correctIngrStr = replaceModifiedWords(modifiedWords, (_a = parsedIngrNoTs.ingredient) !== null && _a !== void 0 ? _a : '');
36
+ const parsedIngr = Object.assign(Object.assign({}, parsedIngrNoTs), { ingredient: correctIngrStr });
37
+ return parsedIngr;
38
+ }
39
+ else {
40
+ return (0, recipe_ingredient_parser_v3_1.parse)(ingrStr, 'eng');
41
+ }
42
+ };
43
+ exports.parseStringConsecutiveTs = parseStringConsecutiveTs;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jclind/ingredient-parser",
3
- "version": "1.0.42",
3
+ "version": "1.1.1",
4
4
  "description": "Parses given sentence including ingredient information and attempts to return quantity, measurement and ingredient data",
5
5
  "keywords": [
6
6
  "recipe",
@@ -10,6 +10,7 @@
10
10
  ],
11
11
  "main": "dist/index.js",
12
12
  "types": "dist/index.d.ts",
13
+ "type": "commonjs",
13
14
  "files": [
14
15
  "dist"
15
16
  ],
@@ -27,7 +28,7 @@
27
28
  },
28
29
  "homepage": "https://github.com/jclind/ingredient-parser#readme",
29
30
  "dependencies": {
30
- "@jclind/ingredient-unit-converter": "^1.0.2",
31
+ "@jclind/ingredient-unit-converter": "^1.0.5",
31
32
  "@types/axios": "^0.14.0",
32
33
  "convert-units": "^2.3.4",
33
34
  "dotenv": "^16.0.3",