Research
Security News
Quasar RAT Disguised as an npm Package for Detecting Vulnerabilities in Ethereum Smart Contracts
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
parse-ingredient
Advanced tools
Recipe ingredient parser with support for mixed numbers and vulgar fractions
Parses a string, which can include mixed numbers or vulgar fractions (thanks to numeric-quantity), into an array of recipe ingredient objects with the following signature:
interface Ingredient {
/**
* The primary quantity (the lower quantity in a range, if applicable)
*/
quantity: number | null;
/**
* The secondary quantity (the upper quantity in a range, or `null` if not applicable)
*/
quantity2: number | null;
/**
* The unit of measure
*/
unitOfMeasure: string | null;
/**
* The description
*/
description: string;
/**
* Whether the "ingredient" is actually a group header, e.g. "For icing:"
*/
isGroupHeader: boolean;
}
For the isGroupHeader
attribute to be true
, the ingredient string must not start with a number, and must either start with 'For '
or end with ':'
.
This library pairs nicely with format-quantity which can display numeric values as imperial measurements (e.g. '1 1/2'
instead of 1.5
).
# npm
npm i parse-ingredient
# yarn
yarn add parse-ingredient
In the browser, available as a global function parseIngredient
. Remember to first include numeric-quantity
.
<script src="https://unpkg.com/numeric-quantity"></script>
<script src="https://unpkg.com/parse-ingredient"></script>
<script>
console.log(parseIngredient('1 1/2 cups sugar'));
/**
* [
* {
* quantity: 1.5,
* quantity2: null,
* unitOfMeasure: 'cups',
* description: 'sugar',
* isGroupHeader: false,
* }
* ]
*/
</script>
import parseIngredient from 'parse-ingredient';
console.log(parseIngredient('1-2 pears'));
/**
* [
* {
* quantity: 1,
* quantity2: 2,
* unitOfMeasure: null,
* description: 'pears',
* isGroupHeader: false,
* }
* ]
*/
console.log(
parseIngredient(
`2/3 cup flour
1 tsp baking powder`
)
);
/**
* [
* {
* quantity: 0.667,
* quantity2: null,
* unitOfMeasure: 'cup',
* description: 'flour',
* isGroupHeader: false,
* },
* {
* quantity: 1,
* quantity2: null,
* unitOfMeasure: 'tsp',
* description: 'baking powder',
* isGroupHeader: false,
* },
* ]
*/
console.log(parseIngredient('For cake:'));
/**
* [
* {
* quantity: null,
* quantity2: null,
* unitOfMeasure: null,
* description: 'For cake:',
* isGroupHeader: true,
* }
* ]
*/
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.
Example:
console.log(parseIngredient('1 c sugar', { normalizeUOM: true }));
/**
* [
* {
* quantity: 1,
* quantity2: null,
* unitOfMeasure: 'cup',
* description: 'sugar',
* isGroupHeader: false,
* }
* ]
*/
[v0.4.0] - 2021-08-23
FAQs
Recipe ingredient parser with support for mixed numbers and vulgar fractions
The npm package parse-ingredient receives a total of 333 weekly downloads. As such, parse-ingredient popularity was classified as not popular.
We found that parse-ingredient demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
Security News
Research
A supply chain attack on Rspack's npm packages injected cryptomining malware, potentially impacting thousands of developers.
Research
Security News
Socket researchers discovered a malware campaign on npm delivering the Skuld infostealer via typosquatted packages, exposing sensitive data.