Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

parse-ingredient

Package Overview
Dependencies
Maintainers
1
Versions
18
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

parse-ingredient

Recipe ingredient parser with support for mixed numbers and vulgar fractions

  • 0.4.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
397
decreased by-22.61%
Maintainers
1
Weekly downloads
 
Created
Source

parse-ingredient

npm version workflow status codecov.io downloads MIT License

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).

Demo

See demo here.

Installation

npm

# npm
npm i parse-ingredient

# yarn
yarn add parse-ingredient

Browser

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>

Usage

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,
 *   }
 * ]
 */

Options

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.

Example:

console.log(parseIngredient('1 c sugar', { normalizeUOM: true }));
/**
 * [
 *   {
 *     quantity: 1,
 *     quantity2: null,
 *     unitOfMeasure: 'cup',
 *     description: 'sugar',
 *     isGroupHeader: false,
 *   }
 * ]
 */

Keywords

FAQs

Package last updated on 23 Aug 2021

Did you know?

Socket

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc