Skip to content

Parses an ingredient list as a string into an array of objects

License

Notifications You must be signed in to change notification settings

jakeboone02/parse-ingredient

Repository files navigation

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

The parseIngredient function accepts a string (with newline-separated ingredients) or an array of strings (one ingredient per element).

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

includeMeta

When true, each ingredient object will include a meta property containing source metadata:

  • sourceText: The original text of the ingredient line before parsing.
  • sourceIndex: The zero-based line number in the original input (accounts for empty lines).
parseIngredient('1 cup flour\n\n2 tbsp sugar', { includeMeta: true });
// [
//   {
//     quantity: 1,
//     quantity2: null,
//     unitOfMeasure: 'cup',
//     unitOfMeasureID: 'cup',
//     description: 'flour',
//     isGroupHeader: false,
//     meta: { sourceText: '1 cup flour', sourceIndex: 0 },
//   },
//   {
//     quantity: 2,
//     quantity2: null,
//     unitOfMeasure: 'tbsp',
//     unitOfMeasureID: 'tablespoon',
//     description: 'sugar',
//     isGroupHeader: false,
//     meta: { sourceText: '2 tbsp sugar', sourceIndex: 2 },
//   },
// ]

Internationalization (i18n)

The library supports parsing ingredients in multiple languages through configurable keyword options. While unit names can be localized using additionalUOMs, the following options allow localization of parsing keywords and quantities.

decimalSeparator

The character used as a decimal separator in numeric quantities. Use ',' for European-style decimal commas (e.g., '1,5' for 1.5). Defaults to '.'.

parseIngredient('1,5 cups sugar', { decimalSeparator: ',' });
// [
//   {
//     quantity: 1.5,
//     quantity2: null,
//     unitOfMeasure: 'cups',
//     unitOfMeasureID: 'cup',
//     description: 'sugar',
//     isGroupHeader: false,
//   }
// ]

groupHeaderPatterns

Patterns to identify group headers (e.g., "For the icing:"). Strings are treated as prefix patterns matched at the start of the line followed by whitespace. RegExp patterns are used as-is for more complex matching. Defaults to ['For'].

// German group headers
parseIngredient('Für den Teig:\n2 cups flour', {
  groupHeaderPatterns: ['For', 'Für'],
});
// [
//   { description: 'Für den Teig:', isGroupHeader: true, ... },
//   { quantity: 2, unitOfMeasure: 'cups', description: 'flour', ... }
// ]

// French with regex pattern (matches "Pour la", "Pour le", "Pour un", etc.)
parseIngredient('Pour la pâte:', {
  groupHeaderPatterns: ['For', /^Pour\s/iu],
});

rangeSeparators

Words or patterns to identify ranges between quantities (e.g., "1 to 2", "1 or 2"). Dash characters (-, –, —) are always recognized. Defaults to ['to', 'or'].

// German range separators
parseIngredient('1 bis 2 cups flour', {
  rangeSeparators: ['to', 'or', 'bis', 'oder'],
});
// [{ quantity: 1, quantity2: 2, ... }]

// French range separator
parseIngredient('2 à 3 cups sugar', {
  rangeSeparators: ['to', 'or', 'à', 'ou'],
});

descriptionStripPrefixes

Words or patterns to strip from the beginning of ingredient descriptions. Commonly used to remove "of" from phrases like "1 cup of sugar". Strings are matched as whole words followed by whitespace. RegExp patterns are used as-is, which is useful for languages with contractions or elisions. Defaults to ['of'].

Note: This option is only applied when allowLeadingOf is false (the default). If allowLeadingOf is true, prefix stripping is disabled entirely and this option is ignored.

// Spanish "de" stripping
parseIngredient('2 tazas de azúcar', {
  descriptionStripPrefixes: ['of', 'de'],
});
// [{ description: 'azúcar', ... }]

// French with regex patterns for elisions/contractions
parseIngredient("2 tasses d'huile", {
  descriptionStripPrefixes: [/de\s+la\s+/iu, /de\s+l'/iu, /d'/iu, 'de'],
});
// [{ description: 'huile', ... }]

trailingQuantityContext

Words that indicate a trailing quantity extraction context, used to identify patterns like "Juice of 3 lemons". Defaults to ['from', 'of'].

// German context word
parseIngredient('Saft von 3 Zitronen', {
  trailingQuantityContext: ['from', 'of', 'von'],
});
// [{ quantity: 3, description: 'Saft von Zitronen', ... }]

Full i18n Example (German)

parseIngredient(
  `Für den Kuchen:
2 bis 3 Tassen Mehl
1 Tasse Zucker`,
  {
    groupHeaderPatterns: ['For', 'Für'],
    rangeSeparators: ['to', 'or', 'bis', 'oder'],
    decimalSeparator: ',',
    additionalUOMs: {
      tasse: {
        short: 'T',
        plural: 'Tassen',
        alternates: ['Tasse'],
      },
    },
  }
);
// [
//   { description: 'Für den Kuchen:', isGroupHeader: true, ... },
//   { quantity: 2, quantity2: 3, unitOfMeasure: 'Tassen', description: 'Mehl', ... },
//   { quantity: 1, unitOfMeasure: 'Tasse', description: 'Zucker', ... }
// ]

partialUnitMatching

When true, if normal whitespace-based parsing fails to identify a unit of measure, the parser scans the description for known UOM strings registered via additionalUOMs. This is useful for CJK languages (Japanese, Chinese, Korean) where words are not separated by spaces.

parseIngredient('砂糖大さじ2', {
  partialUnitMatching: true,
  additionalUOMs: {
    '大さじ': { short: '大さじ', plural: '大さじ', alternates: [] },
  },
});
// [
//   {
//     quantity: 2,
//     quantity2: null,
//     unitOfMeasure: '大さじ',
//     unitOfMeasureID: '大さじ',
//     description: '砂糖',
//     isGroupHeader: false,
//   }
// ]

The scan also works with Latin UOMs already known to the library (e.g., g, ml) and mixed-language ingredient lists:

parseIngredient('砂糖大さじ2\nバター10g\n1 cup flour', {
  partialUnitMatching: true,
  additionalUOMs: {
    '大さじ': { short: '大さじ', plural: '大さじ', alternates: [] },
  },
});
// [
//   { quantity: 2, unitOfMeasure: '大さじ', description: '砂糖', ... },
//   { quantity: 10, unitOfMeasure: 'g', description: 'バター', ... },
//   { quantity: 1, unitOfMeasure: 'cup', description: 'flour', ... },
// ]

When multiple UOM strings could match, the longest match wins (e.g., 大さじ is preferred over ).

Unit Conversion

convertUnit

Converts a numeric value from one unit of measure to another. Accepts unit IDs, short forms, plurals, or alternate spellings (e.g., 'cup', 'c', 'cups', 'C'). Returns the converted value, or null if conversion is not possible (incompatible types, missing conversion factors, or unknown units).

import { convertUnit } from 'parse-ingredient';

convertUnit(1, 'cup', 'milliliter'); // ~236.588 (US)
convertUnit(1, 'cups', 'ml'); // ~236.588 (same as above)
convertUnit(1, 'pound', 'gram'); // ~453.592
convertUnit(1, 'lbs', 'g'); // ~453.592 (same as above)
convertUnit(1, 'inch', 'centimeter'); // ~2.54
convertUnit(1, 'cup', 'gram'); // null (incompatible types: volume vs mass)

Options

  • fromSystem: The measurement system to use for the source unit ('us', 'imperial', or 'metric'). Defaults to 'us'.
  • toSystem: The measurement system to use for the target unit. Defaults to 'us'.
  • additionalUOMs: Additional unit definitions to use for conversion (merged with the default unitsOfMeasure).
// Convert using different measurement systems
convertUnit(1, 'cup', 'milliliter', { fromSystem: 'imperial' }); // ~284.131
convertUnit(1, 'cup', 'cup', { fromSystem: 'us', toSystem: 'imperial' }); // ~0.833

// Use custom unit definitions
convertUnit(1, 'bucket', 'liter', {
  additionalUOMs: {
    bucket: {
      short: 'bkt',
      plural: 'buckets',
      alternates: [],
      type: 'volume',
      conversionFactor: 10000, // 10000 ml = 10 liters
    },
  },
}); // 10

conversionFactor

The conversionFactor property in unit definitions enables the convertUnit function. Units with the same type (e.g., 'volume', 'mass', 'length') can be converted between each other.

  • Number: A single conversion factor applies to all measurement systems.
  • Object: Different factors for us, imperial, and/or metric systems.
// Single factor (same for all systems)
gram: {
  short: 'g',
  plural: 'grams',
  type: 'mass',
  conversionFactor: 1, // base unit for mass
}

// Multi-system factors
cup: {
  short: 'c',
  plural: 'cups',
  type: 'volume',
  conversionFactor: { us: 236.588, imperial: 284.131, metric: 250 },
}

Supported unit types: volume, mass, length. Units without a conversionFactor or type (such as pinch, clove, or count-based units like bag) cannot be converted.

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

💻 ⚠️
AfoxDesignz
AfoxDesignz

💻 ⚠️

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

About

Parses an ingredient list as a string into an array of objects

Topics

Resources

License

Stars

Watchers

Forks

Sponsor this project

 

Contributors 7

Languages