@quantojs/anything
v0.6.0
Published
One quanto codec that reads anything: dates, money, every quantity, dimensions, coordinates, sizes, CSS colors, pitches, numbers, ratios and odds, with ranges and approximate values, in a curated order.
Maintainers
Readme
@quantojs/anything
One quanto codec that reads anything the first-party packages can: dates, times, money, every quantity, dimensions, coordinates, ring and shoe sizes, CSS colors, pitches, numbers, ratios and odds, ranges of them, infinities, and all of it approximate. It's the big field on quanto's site, as a package.
import { anything } from '@quantojs/anything';
import { phoneNumber } from '@quantojs/anything/phone';
anything().parse('24x36in'); // { value: { codec: 'dimensions(length)', value: [{ value: 24, unit: 'in' }, …] }, approximate: false }
anything().parse('about 6 ft'); // { value: { codec: 'length', value: { value: 6, unit: 'ft' } }, approximate: true }
anything().parse('$10-20k'); // { value: { codec: 'range(money)', value: { start: …, end: … } }, approximate: false }
anything().parse('next fri'); // { value: { codec: 'date', value: '2026-10-02' }, approximate: false }
const withPhones = anything({ include: [phoneNumber()] });
withPhones.parse('(415) 555-2671'); // { value: { codec: 'phoneNumber', value: '+14155552671' }, approximate: false }- The value is tagged with the id of the codec that read it: switch on
value.codecto know whatvalue.valueis.formatprints it with that codec. - The order settles conflicts. The first codec that reads the text wins, and the others' readings are its alternatives. Dates and times come first, then dimensions, length, mass, duration and temperature, money, pitch, the other quantities, coordinates, sizes and CSS colors, then plain numbers, ratios and odds, then ranges. So
24 × 36 inis dimensions,455hza pitch,3 ggrams,11/4a date,25 bpsa data rate,US 10a ring size,2700 Ka temperature (with the color temperature as an alternative) and70a number. CSS colors read withoutbare, sobadand0 170 255aren't colors. A trailingoddsclaims any of them for odds:11/4 odds,70 odds. The fixtures pin every conflict. - Infinities are tagged with the member their unit or kind names:
unlimited GBis{ codec: 'dataSize', value: { infinite: 1, unit: 'GB' } },infinite lengthis{ codec: 'length', value: { infinite: 1 } }, and a bare∞is{ codec: 'infinite', value: { infinite: 1 } }. Testvalue.valuewithisInfinitefromquanto. - Phone numbers are opt-in with
include, since libphonenumber-js's metadata is about 80 kB.phoneNumberis re-exported from@quantojs/anything/phonefor convenience, on its own subpath so the main entry never brings the metadata, whatever the bundler. To load it later,await import('@quantojs/anything/phone')and build the codec again with it. includetakes any codecs: they're read after the built-in ones and before the catch-alls (numbers, ratios, odds).namesadds month and weekday names for dates, asdate({ names })does.- Failures are tidied: the first issue of each informative code (
unknown_unit,ambiguous, …), or oneunparseable. - Not included: addresses (an external codec),
percent(proportionreads%),textand time signatures (6/8is a date). - Still evolving. The order changes as codecs are added, so what a text reads as can change between releases.
quanto is a peer dependency.
