@smartput/datarate
v0.2.0
Published
bit/s to Gbit/s, bridging data size and duration.
Downloads
29
Maintainers
Readme
@smartput/datarate
bit/s to Gbit/s, bridging data size and duration.
Canonical in bits per second. datasize's canonical is the byte and this
one's is the bit, so every bridge between them carries the factor of eight
explicitly rather than hiding it in a ratio.
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/datarateExample
import { parseDatarate, asDatarate, formatDatarate } from "@smartput/datarate/validate";
import { Datarate } from "@smartput/datarate/class";
parseDatarate("100 mbps") // {"ok":true,"value":100,"unit":"mbps","raw":"100"}
asDatarate("100 mbps", "bps") // {"ok":true,"value":100000000,"unit":"bps","raw":"100000000"}
formatDatarate(parseDatarate("100 mbps")) // "100mbps"
String(Datarate.parse("100 mbps")) // "100mbps"
// …and the same table through the engine:
engine.evaluate("1 gbps in mbps").formatted // "1,000 mbps"
engine.evaluate("100 mbps + 50 mbps").formatted // "150 mbps"parse and as never throw — they answer { ok: true, … } or
{ ok: false, code, input }. The value class throws on bad input and
Datarate.tryParse does not. All three read the one UnitTable below.
Entry points
| Import | Contents |
| --- | --- |
| @smartput/datarate | The package root. |
| @smartput/datarate/units | The UnitTable: ratios and aliases, with no engine and no Decimal. |
| @smartput/datarate/validate | Free functions over JS numbers. Ok \| Err, never a throw. |
| @smartput/datarate/class | The immutable value class. |
| @smartput/datarate/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.
DATARATE_UNITS
| Unit | Ratio to bps | Aliases |
| --- | --- | --- |
| bps | 1 | bps |
| kbps | 1000 | kbps |
| mbps | 1000000 | mbps |
| gbps | 1000000000 | gbps |
| tbps | 1000000000000 | tbps |
Runtime exports
Type-only exports are erased and do not appear here.
DATARATE_UNITS · datarate
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 | | --- | --- | --- | | datarate/validate parseDatarate only | ≤ 1.2 kB | ≤ 650 B | | datarate/class | ≤ 4.7 kB | ≤ 1.9 kB |
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/datarate.md.
