@smartput/speed
v0.2.0
Published
m/s, km/h, mph, knots.
Maintainers
Readme
@smartput/speed
m/s, km/h, mph, knots.
Canonical in metres per second. The knot ratio is the full 28-digit value:
0.514444 was the true number truncated, and a wrong constant is not a
smaller one.
Three doors, one table. The kind descriptor is for the
engine, /validate is free functions over JS numbers, /class is an
immutable value class — and all three read the same UnitTable, so a unit
added once is added everywhere. See Validating without the
engine.
Setup
npm add @smartput/speedExample
import { parseSpeed, asSpeed, formatSpeed } from "@smartput/speed/validate";
import { Speed } from "@smartput/speed/class";
parseSpeed("80 kph") // {"ok":true,"value":80,"unit":"kph","raw":"80"}
asSpeed("80 kph", "mps") // {"ok":true,"value":22.22222222222222,"unit":"mps","raw":"22.22222222222222"}
formatSpeed(parseSpeed("80 kph")) // "80kph"
String(Speed.parse("80 kph")) // "80kph"
// …and the same table through the engine:
engine.evaluate("100 kph in mph").formatted // "62.1371 mph"
engine.evaluate("10 knot in kph").formatted // "18.52 km/h"
engine.evaluate("60 mph").formatted // "60 mph"parse and as never throw — they answer { ok: true, … } or
{ ok: false, code, input }. The value class throws on bad input and
Speed.tryParse does not. All three read the one UnitTable below.
Entry points
| Import | Contents |
| --- | --- |
| @smartput/speed | The package root. |
| @smartput/speed/units | The UnitTable: ratios and aliases, with no engine and no Decimal. |
| @smartput/speed/validate | Free functions over JS numbers. Ok \| Err, never a throw. |
| @smartput/speed/class | The immutable value class. |
| @smartput/speed/locale/<id> | One language's words for this kind. 17 ship: en, de, fr, es, pt, it, nl, zh, ja, ar, ru, pl, tr, hi, ko, id, uk. |
Units
Read from the table itself, not typed out — a unit added to
the source appears here on the next bun run docs:packages. Ratios are decimal
strings, which is what lets the engine widen them to Decimal without a float
in between.
SPEED_UNITS
| Unit | Ratio to mps | Aliases |
| --- | --- | --- |
| mps | 1 | mps |
| kph | 0.2777777777777777777777777778 | kmh kph |
| mph | 0.44704 | mph |
| knot | 0.5144444444444444444444444444 | kt knot knots |
Runtime exports
Type-only exports are erased and do not appear here.
SPEED_UNITS · speed
Dependencies
What it costs
Ceilings, not measurements. bun run check-size bundles each entry with
bun build --minify and fails if a row crosses its ceiling or drops more
than 30 % below it — a budget that is only an upper bound reports a vanished
graph as a triumph.
| Import | Minified | Gzipped | | --- | --- | --- | | speed/validate parseSpeed only | ≤ 1.3 kB | ≤ 700 B |
Generated by scripts/gen-readmes.ts — run bun run docs:readmes. Every
output above was produced by running the line beside it. The full page, with
live demos, is docs/packages/speed.md.
