parse-ingredient

Recipe ingredient parser with support for mixed numbers and vulgar fractions

Usage no npm install needed!

<script type="module">
  import parseIngredient from 'https://cdn.skypack.dev/parse-ingredient';
</script>

README

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