Skip to content
lingo

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 ft parses as a length quantity with base 0.6096 m.
  • 5'11" parses as approximately 1.8034 m with feet/inches parts.
  • 72 in to cm parses as a conversion with converted value 182.88 cm.
  • between 5 and 10 kg parses as a mass range.
  • a few minutes parses as an approximate duration range.
  • it's hot parses as a temperature fuzzy range when kind: "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)
}