Parse
Turn text into a canonical, validated value.
Prefer the live demos? Open this section in the interactive docs.
Turn any text into a canonical, validated value, then read it back. Use lingo() for flexible fields; use shaped helpers for fixed contracts.
import { lingo, parseQuantity, parseRange } from "@pascal-app/lingo"
const height = parseQuantity("5'11\"", { kind: "length" })
if (height.ok) {
height.quantity.to("m").value
height.quantity.format({ compound: ["ft", "in"] })
}
const any = lingo("72 in to cm")
const range = parseRange("between 5 and 10 kg", { kind: "mass" })Captions:
- Universal parse: Conversion requests return target units without losing original spans.
- Parse readout: Warnings can succeed; only errors block the value.
- System and number format variants: System picks the gallon family; numberFormat resolves separator ambiguity.
Canonical examples from the public docs:
2 ftparses as a length quantity with base0.6096 m.5'11"parses as approximately1.8034 mwith feet/inches parts.72 in to cmparses as a conversion with converted value182.88 cm.between 5 and 10 kgparses as a mass range.a few minutesparses as an approximate duration range.it's hotparses as a temperature fuzzy range whenkind: "temperature"is supplied.
Find values in text
findQuantities(text, opts?) scans free text and returns every quantity, range, and conversion it finds, each with a span pointing at the exact characters in the original string.
import { findQuantities } from "@pascal-app/lingo"
const found = findQuantities("ship 2 boxes at 5 kg each by friday")
// [{ result, span: { start, end } }, ...] — offsets into the original text
for (const { result, span } of found) {
if (result.type === "quantity") highlight(span, result.quantity)
}