parse-ingredient
Advanced tools
Comparing version 0.4.0 to 0.5.0
@@ -11,2 +11,6 @@ export interface Ingredient { | ||
/** | ||
* The unit of measure identifier | ||
*/ | ||
unitOfMeasureID: string | null; | ||
/** | ||
* The unit of measure | ||
@@ -24,10 +28,34 @@ */ | ||
} | ||
export interface UnitOfMeasure { | ||
short: string; | ||
plural: string; | ||
alternates: string[]; | ||
} | ||
export declare type UnitOfMeasureDefinitions = Record<string, UnitOfMeasure>; | ||
export interface ParseIngredientOptions { | ||
/** | ||
* Converts the unit of measure (`unitOfMeasure` property) of each | ||
* ingredient to its long, singular form. For example, "ml" becomes | ||
* "milliliter" and "cups" becomes "cup". | ||
*/ | ||
normalizeUOM?: boolean; | ||
/** | ||
* An object that matches the format of `unitsOfMeasure`. Keys that | ||
* match any in `unitsOfMeasure` will be used instead of the default, | ||
* and any others will be added to the list of known units of measure | ||
* when parsing ingredients. | ||
*/ | ||
additionalUOMs?: UnitOfMeasureDefinitions; | ||
/** | ||
* If `true`, ingredient descriptions that start with "of " will not be | ||
* modified. (By default, a leading "of " will be removed all descriptions.) | ||
*/ | ||
allowLeadingOf?: boolean; | ||
} | ||
export declare const unitsOfMeasure: UnitOfMeasureDefinitions; | ||
/** | ||
* Parses a string into an array of recipe ingredient objects | ||
* @param ingText The ingredient text | ||
* @param options Configuration options | ||
*/ | ||
declare const parseIngredient: (ingText: string, options?: ParseIngredientOptions | undefined) => Ingredient[]; | ||
export default parseIngredient; | ||
export declare const parseIngredient: (ingText: string, options?: ParseIngredientOptions | undefined) => Ingredient[]; |
{ | ||
"version": "0.4.0", | ||
"name": "parse-ingredient", | ||
"author": "Jake Boone", | ||
"version": "0.5.0", | ||
"license": "MIT", | ||
"description": "Recipe ingredient parser with support for mixed numbers and vulgar fractions", | ||
"main": "dist/index.js", | ||
"files": [ | ||
"dist" | ||
], | ||
"main": "./dist/parse-ingredient.cjs.js", | ||
"module": "./dist/parse-ingredient.es.js", | ||
"unpkg": "./dist/parse-ingredient.umd.js", | ||
"typings": "dist/index.d.ts", | ||
@@ -22,5 +29,2 @@ "keywords": [ | ||
}, | ||
"files": [ | ||
"dist" | ||
], | ||
"engines": { | ||
@@ -30,7 +34,5 @@ "node": ">=10" | ||
"scripts": { | ||
"start": "tsdx watch --format cjs,esm,system,umd", | ||
"build": "tsdx build --format cjs,esm,system,umd", | ||
"test": "tsdx test", | ||
"lint": "tsdx lint", | ||
"prepare": "npm run build", | ||
"start": "vite", | ||
"build": "vite build && tsc", | ||
"test": "jest --coverage", | ||
"publish:npm": "np", | ||
@@ -40,20 +42,13 @@ "publish:demo": "node gh-pages.publish.js", | ||
}, | ||
"prettier": { | ||
"printWidth": 80, | ||
"semi": true, | ||
"singleQuote": true, | ||
"trailingComma": "es5", | ||
"arrowParens": "avoid", | ||
"tabWidth": 2 | ||
}, | ||
"name": "parse-ingredient", | ||
"author": "Jake Boone", | ||
"module": "dist/parse-ingredient.esm.js", | ||
"unpkg": "dist/parse-ingredient.umd.production.min.js", | ||
"devDependencies": { | ||
"@babel/core": "^7.17.8", | ||
"@babel/preset-env": "^7.16.11", | ||
"@babel/preset-typescript": "^7.16.7", | ||
"@types/jest": "^27.4.1", | ||
"gh-pages": "^3.1.0", | ||
"jest": "^27.5.1", | ||
"np": "^7.3.0", | ||
"tsdx": "^0.14.1", | ||
"tslib": "^2.1.0", | ||
"typescript": "^4.1.5" | ||
"prettier": "^2.6.0", | ||
"typescript": "^4.1.5", | ||
"vite": "^2.8.6" | ||
}, | ||
@@ -60,0 +55,0 @@ "dependencies": { |
186
README.md
@@ -22,2 +22,6 @@ # parse-ingredient | ||
/** | ||
* The unit of measure identifier (see `unitsOfMeasure`) | ||
*/ | ||
unitOfMeasureID: string | null; | ||
/** | ||
* The unit of measure | ||
@@ -41,2 +45,4 @@ */ | ||
If present (i.e. not `null`), the `unitOfMeasureID` property corresponds to a key from the exported `unitsOfMeasure` object which defines short, plural, and other alternate versions of known units of measure. To extend the list of units, use the [`additionalUOMs` option](#additionaluoms) and/or or submit a [pull request](https://github.com/jakeboone02/parse-ingredient/pulls) to add new units to this library's default list. | ||
## Demo | ||
@@ -48,3 +54,3 @@ | ||
### npm | ||
### npm/yarn | ||
@@ -67,14 +73,13 @@ ```shell | ||
<script> | ||
console.log(parseIngredient('1 1/2 cups sugar')); | ||
/** | ||
* [ | ||
* { | ||
* quantity: 1.5, | ||
* quantity2: null, | ||
* unitOfMeasure: 'cups', | ||
* description: 'sugar', | ||
* isGroupHeader: false, | ||
* } | ||
* ] | ||
*/ | ||
console.log(ParseIngredient.parseIngredient('1 1/2 cups sugar')); | ||
// [ | ||
// { | ||
// quantity: 1.5, | ||
// quantity2: null, | ||
// unitOfMeasure: 'cups', | ||
// unitOfMeasureID: 'cup', | ||
// description: 'sugar', | ||
// isGroupHeader: false, | ||
// } | ||
// ] | ||
</script> | ||
@@ -86,16 +91,15 @@ ``` | ||
```js | ||
import parseIngredient from 'parse-ingredient'; | ||
import { parseIngredient } from 'parse-ingredient'; | ||
console.log(parseIngredient('1-2 pears')); | ||
/** | ||
* [ | ||
* { | ||
* quantity: 1, | ||
* quantity2: 2, | ||
* unitOfMeasure: null, | ||
* description: 'pears', | ||
* isGroupHeader: false, | ||
* } | ||
* ] | ||
*/ | ||
// [ | ||
// { | ||
// quantity: 1, | ||
// quantity2: 2, | ||
// unitOfMeasure: null, | ||
// unitOfMeasureID: null, | ||
// description: 'pears', | ||
// isGroupHeader: false, | ||
// } | ||
// ] | ||
console.log( | ||
@@ -107,32 +111,31 @@ parseIngredient( | ||
); | ||
/** | ||
* [ | ||
* { | ||
* quantity: 0.667, | ||
* quantity2: null, | ||
* unitOfMeasure: 'cup', | ||
* description: 'flour', | ||
* isGroupHeader: false, | ||
* }, | ||
* { | ||
* quantity: 1, | ||
* quantity2: null, | ||
* unitOfMeasure: 'tsp', | ||
* description: 'baking powder', | ||
* isGroupHeader: false, | ||
* }, | ||
* ] | ||
*/ | ||
// [ | ||
// { | ||
// quantity: 0.667, | ||
// quantity2: null, | ||
// unitOfMeasure: 'cup', | ||
// unitOfMeasureID: 'cup', | ||
// description: 'flour', | ||
// isGroupHeader: false, | ||
// }, | ||
// { | ||
// quantity: 1, | ||
// quantity2: null, | ||
// unitOfMeasure: 'tsp', | ||
// unitOfMeasureID: 'teaspoon', | ||
// description: 'baking powder', | ||
// isGroupHeader: false, | ||
// }, | ||
// ] | ||
console.log(parseIngredient('For cake:')); | ||
/** | ||
* [ | ||
* { | ||
* quantity: null, | ||
* quantity2: null, | ||
* unitOfMeasure: null, | ||
* description: 'For cake:', | ||
* isGroupHeader: true, | ||
* } | ||
* ] | ||
*/ | ||
// [ | ||
// { | ||
// quantity: null, | ||
// quantity2: null, | ||
// unitOfMeasure: null, | ||
// unitOfMeasureID: null, | ||
// description: 'For cake:', | ||
// isGroupHeader: true, | ||
// } | ||
// ] | ||
``` | ||
@@ -142,5 +145,5 @@ | ||
### normalizeUOM | ||
### `normalizeUOM` | ||
Pass `true` to convert units of measure to their long, singular form, e.g. "ml" becomes "milliliter" and "cups" becomes "cup". This can help normalize the units of measure for processing. | ||
Pass `true` to convert units of measure to their long, singular form, e.g. "ml" becomes "milliliter" and "cups" becomes "cup". This can help normalize the units of measure for processing. In most cases, this option will make `unitOfMeasure` equivalent to `unitOfMeasureID`. | ||
@@ -151,13 +154,62 @@ Example: | ||
console.log(parseIngredient('1 c sugar', { normalizeUOM: true })); | ||
/** | ||
* [ | ||
* { | ||
* quantity: 1, | ||
* quantity2: null, | ||
* unitOfMeasure: 'cup', | ||
* description: 'sugar', | ||
* isGroupHeader: false, | ||
* } | ||
* ] | ||
*/ | ||
// [ | ||
// { | ||
// quantity: 1, | ||
// quantity2: null, | ||
// unitOfMeasure: 'cup', | ||
// unitOfMeasureID: 'cup', | ||
// description: 'sugar', | ||
// isGroupHeader: false, | ||
// } | ||
// ] | ||
``` | ||
### `additionalUOMs` | ||
Pass an object that matches the format of the exported `unitsOfMeasure` object. Keys that match any in the exported object will be used instead of the default, and any others will be added to the list of known units of measure when parsing ingredients. | ||
Example: | ||
```js | ||
console.log( | ||
parseIngredient('2 buckets of widgets', { | ||
additionalUOMs: { | ||
bucket: { | ||
short: 'bkt', | ||
plural: 'buckets', | ||
versions: ['bk'], | ||
}, | ||
}, | ||
}) | ||
); | ||
// [ | ||
// { | ||
// quantity: 2, | ||
// quantity2: null, | ||
// unitOfMeasureID: 'bucket', | ||
// unitOfMeasure: 'buckets', | ||
// description: 'widgets', | ||
// isGroupHeader: false, | ||
// }, | ||
// ] | ||
``` | ||
### `allowLeadingOf` | ||
When `true`, ingredient descriptions that start with "of " will not be modified. (By default, a leading "of " will be removed from all descriptions.) | ||
Example: | ||
```js | ||
console.log(parseIngredient('1 cup of sugar', { allowLeadingOf: true })); | ||
// [ | ||
// { | ||
// quantity: 1, | ||
// quantity2: null, | ||
// unitOfMeasure: 'cup', | ||
// unitOfMeasureID: 'cup', | ||
// description: 'of sugar', | ||
// isGroupHeader: false, | ||
// } | ||
// ] | ||
``` |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
Found 1 instance in 1 package
208
0
70739
10
11
259
1