⚠️ This is pre-release documentation for v3. For stable docs, visit v2.
Skip to content

@tmlmt/cooklang-parser / parseQuantityWithUnit

Function: parseQuantityWithUnit()

parseQuantityWithUnit(input): object

Parses a quantity string with unit separated by % (e.g. "500%g"). If no % is present, the entire string is treated as a value with no unit.

Parameters

input

string

The quantity string to parse.

Returns

object

An object with parsed value and optional unit.

unit?

optional unit?: string

value

value: FixedValue | Range

Example

typescript
parseQuantityWithUnit("500%g"); // { value: { type: "fixed", decimal: 500 }, unit: "g" }
parseQuantityWithUnit("2"); // { value: { type: "fixed", decimal: 2 } }