parse-ingredient - v2.0.1
    Preparing search index...

    Function identifyUnit

    • Identifies a unit of measure from a string, returning the canonical unit ID. Matches against the unit ID, short form, plural form, and all alternates. Case-sensitive matches are tried first (e.g., 'T' = tablespoon, 't' = teaspoon), then falls back to case-insensitive matching.

      Parameters

      • unit: string

        The unit string to identify (e.g., 'cups', 'c', 'C', 'cup').

      • options: IdentifyUnitOptions = {}

        Options for unit identification.

      Returns string | null

      The canonical unit ID (e.g., 'cup'), or null if the unit is not recognized or is in the ignoreUOMs list.

      identifyUnit('cups') // 'cup'
      identifyUnit('c') // 'cup'
      identifyUnit('T') // 'tablespoon'
      identifyUnit('t') // 'teaspoon'
      identifyUnit('tbsp') // 'tablespoon'
      identifyUnit('unknown') // null
      identifyUnit('large', { ignoreUOMs: ['large'] }) // null