npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

parse-ingredient

v1.2.1

Published

Recipe ingredient parser with support for mixed numbers and vulgar fractions

Downloads

1,918

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

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