Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

parse-ingredient

jakeboone022.4kMIT1.3.0TypeScript support: included

Recipe ingredient parser with support for mixed numbers and vulgar fractions

parse, recipe, ingredient, quantity, number

readme

npm workflow status codecov.io downloads MIT License All Contributors

Parses a string, which can include mixed numbers or vulgar fractions (thanks to numeric-quantity), into an array of recipe ingredient objects.

Full documentation

Demo

Ingredient objects have 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 identifier (see `unitsOfMeasure`)
   */
  unitOfMeasureID: string | 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 ':'.

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 and/or or submit a pull request to add new units to this library's default list.

For a complimentary library that handles the inverse operation, displaying numeric values as imperial measurements (e.g. '1 1/2' instead of 1.5), see format-quantity.

Installation

npm

npm i parse-ingredient
# OR yarn add / pnpm add / bun add

Browser

In the browser, all exports including the parseIngredient function are available on the global object ParseIngredient.

<script src="https://unpkg.com/parse-ingredient"></script>
<script>
  ParseIngredient.parseIngredient('1 1/2 cups sugar');
  // [
  //   {
  //     quantity: 1.5,
  //     quantity2: null,
  //     unitOfMeasure: 'cups',
  //     unitOfMeasureID: 'cup',
  //     description: 'sugar',
  //     isGroupHeader: false,
  //   }
  // ]
</script>

Usage

import { parseIngredient } from 'parse-ingredient';

parseIngredient('1-2 pears');
// [
//   {
//     quantity: 1,
//     quantity2: 2,
//     unitOfMeasure: null,
//     unitOfMeasureID: null,
//     description: 'pears',
//     isGroupHeader: false,
//   }
// ]

parseIngredient(
  `2/3 cup flour
1 tsp baking powder`
);
// [
//   {
//     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,
//   },
// ]
parseIngredient('For cake:');
// [
//   {
//     quantity: null,
//     quantity2: null,
//     unitOfMeasure: null,
//     unitOfMeasureID: null,
//     description: 'For cake:',
//     isGroupHeader: true,
//   }
// ]
parseIngredient('Ripe tomato x2');
// [
//   {
//     quantity: 2,
//     quantity2: null,
//     unitOfMeasure: null,
//     unitOfMeasureID: null,
//     description: 'Ripe tomato',
//     isGroupHeader: false,
//   }
// ]

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. In most cases, this option will make unitOfMeasure equivalent to unitOfMeasureID.

parseIngredient('1 c sugar', { normalizeUOM: true });
// [
//   {
//     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.

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

parseIngredient('1 cup of sugar', { allowLeadingOf: true });
// [
//   {
//     quantity: 1,
//     quantity2: null,
//     unitOfMeasure: 'cup',
//     unitOfMeasureID: 'cup',
//     description: 'of sugar',
//     isGroupHeader: false,
//   }
// ]

ignoreUOMs

An array of strings to ignore as units of measure when parsing ingredients.

parseIngredient('2 large eggs', { ignoreUOMs: ['large'] });
// [
//   {
//     quantity: 2,
//     quantity2: null,
//     unitOfMeasure: null,
//     unitOfMeasureID: null,
//     description: 'large eggs',
//     isGroupHeader: false,
//   }
// ]

Contributors ✨

Thanks goes to these wonderful people (emoji key):

Jake Boone
Jake Boone

💻 📖 💡 🚧 ⚠️
Stefan van der Weide
Stefan van der Weide

💻 ⚠️
Roger
Roger

💻
Tyler
Tyler

💻 ⚠️

This project follows the all-contributors specification. Contributions of any kind welcome!

changelog

Changelog

All notable changes to this project will be documented in this file.

The format is based on Keep a Changelog and this project adheres to Semantic Versioning.

Unreleased

  • N/A

v1.3.0 - 2025-05-06

Added

  • #26 Support for the pattern "Extract of/from N things", e.g. "Juice of 1 lemon".

v1.2.1 - 2024-07-06

Fixed

  • #20 Several missing alternate abbreviations were added.

v1.2.0 - 2024-01-18

Added

  • #18 New option ignoreUOMs, an array of strings that will not be considered units of measure.

v1.1.1 - 2024-01-15

Fixed

  • Corrected links in package.json to distributed type definition files.

v1.1.0 - 2023-12-01

Changed

  • #14 compactStringArray has been removed. (array.filter(Boolean) is functionally equivalent to compactStringArray(array).)

Added

  • #14 If no quantity is detected at the beginning of an ingredient line, the end of the line will be scanned for quantity/range and unit of measure.

v1.0.1 - 2023-07-26

Changed

  • compactArray is now compactStringArray and only works for arrays of actual strings.

Fixed

  • Properly handles CRLF line endings.
  • Minor performance improvements.

v1.0.0 - 2023-06-18

Changed

  • #8 Upgrade numeric-quantity to v2.0.0.

v0.6.0 - 2022-10-15

Changed

  • Added "or" as range indicator/separator.

Added

  • #7 Alternate tablespoon UOM.

v0.5.0 - 2022-04-16

Changed

  • Removed redundant UOM versions and renamed to alternates.

Added

  • additionalUOMs and allowLeadingOf options.
  • Additional units of measure.

v0.4.0 - 2021-08-23

Fixed

  • Updated dependencies.

v0.3.0 - 2021-02-15

Added

  • normalizeUOM option.
  • Additional units of measure.

Fixed

  • Use regex for UOM.
  • Use latest versions in demo.

v0.2.4 - 2021-02-15

Added

  • Support for the word "to" to indicate a range.

v0.2.3 - 2021-02-15

Fixed

  • Minor documentation updates.

v0.2.2 - 2021-02-12

Fixed

  • Better IE11 compatibility.

v0.2.1 - 2021-02-12

Fixed

  • IE11 compatibility.

Added

  • Demo.

v0.2.0 - 2021-02-12

Fixed

  • Minor code and documentation updates.
  • Further compact multi-line input

Added

  • #1 Codecov to GHA workflows.

v0.1.2 - 2021-02-11

Changed

  • Ignore blank lines in multi-line input.

Fixed

  • Updated package.json to include URLs.
  • Increased test coverage to 100%.

v0.1.1 - 2021-02-11

added

  • Initial release.