jscheiny / safe-units

Type-safe TypeScript units of measure 👷📏
https://jscheiny.github.io/safe-units
MIT License
261 stars 14 forks source link

Measure.of(<value>, bytes) yields value in bits #168

Closed tymokvo closed 4 months ago

tymokvo commented 3 years ago

Hello!

From the readme, I expected:

console.log(Measure.of(72, bytes).toString());

to print 72 B

but, the result is 576 b as the underlying value created by Measure.of is in bits.

This is a bit confusing as other higher order units behave as I would expect them to, e.g.:

console.log(Measure.of(72, kilograms).toString());

yields 72 kg rather than 72000 g.

Using v1.1.0 with Typescript v4.3.5.

Darkhogg commented 2 years ago

your kilogram example works because kilograms is the base unit of mass, but if you were to use pounds instead

console.log(Measure.of(72, pounds).toString());
// prints: 32.658650640000005 kg

you should use .in instead to print in a particular unit:

console.log(Measure.of(72, bytes).in(bytes));
tymokvo commented 2 years ago

Makes sense! But still confusing. Maybe a reference to the SI base units in the documentation would help? For those of us (like me :grimacing: ) who haven't taken physics for a while?

NIST has a good reference here, though it is US-centric. The ISO standard is paywalled, unfortunately.

jscheiny commented 4 months ago

I know I'm a little late on this (😬), but there is a reference to this in the documentation here: https://jscheiny.github.io/safe-units/builtin.html#base-units