This package is compliant to IEC standard units. see Units Table for supported units
Convert any value from a supported unit to an another supported unit.
Compare any value or unit to another one.
Sum, Subtract, Multiply and Divide two value with any supported unit.
Use the autoScale function for scale a given value and unit to te best unit for representing the value.
yarn add @wtfcode/byte-converter
or
npm i @wtfcode/byte-converter
- class ByteConverter
- class Unit
- class UnitValue
- class ByteConverter
- const b: Unit
- const B: Unit
| from | to | description |
|---|---|---|
| 2.0.1 | 2.0.5 | Added Arithmetic method to UnitValue |
| 2.0.1 | Rewrited the byteConverter implementation in typescript and heavily simplified the algorithm | |
| 1.7.10 | 1.7.11 | autoScale now choose between 1000 and 1024 if is decimal type or binary type, the autoscaling is now better |
ES6 syntax:
import {
ByteConverter,
Unit,
UnitValue,
B as Byte,
} from "@wtfcode/byte-converter";
console.log(ByteConverter.convert(Byte.value(1), "b"));
// will output UnitValue {value: 8, unit: Unit { unit: "b" ... } }
console.log(ByteConverter.convert(ByteConverter.value(1024, "MiB"), "GiB"));
// will output UnitValue {value: 1, unit: Unit { unit: "GiB" ... } }
console.log(new UnitValue(1024, "B").autoScale());
// the function return a UnitValue scaled
// will output: UnitValue {value: 1, unit: Unit { unit: "KiB", type: "binary", unitOrder: 1, name: "kibibyte" ... } }ES5 syntax:
const ByteConverter = require("@wtfcode/byte-converter").default;
console.log(ByteConverter.convert(ByteConverter.value(1024, "MiB"), "GiB"));
// will output UnitValue {value: 1, unit: Unit { unit: "GiB" ... } }
console.log(ByteConverter.autoScale(ByteConverter.value(0.7, "GB")));
// the function accept a third paramater: an option object
// will output: UnitValue {value: 700, unit: Unit { unit: "MB", type: "decimal", unitOrder: 2, name: "megabyte" ... } }| unit | bit | byte | binary | decimal |
|---|---|---|---|---|
| b | ✅ | ✅ | ||
| B | ✅ | ✅ | ||
| kb | ✅ | ✅ | ||
| kB | ✅ | ✅ | ||
| Kib | ✅ | ✅ | ||
| KiB | ✅ | ✅ | ||
| Mb | ✅ | ✅ | ||
| MB | ✅ | ✅ | ||
| Mib | ✅ | ✅ | ||
| MiB | ✅ | ✅ | ||
| Gb | ✅ | ✅ | ||
| GB | ✅ | ✅ | ||
| Gib | ✅ | ✅ | ||
| GiB | ✅ | ✅ | ||
| Tb | ✅ | ✅ | ||
| TB | ✅ | ✅ | ||
| Tib | ✅ | ✅ | ||
| TiB | ✅ | ✅ | ||
| Pb | ✅ | ✅ | ||
| PB | ✅ | ✅ | ||
| Pib | ✅ | ✅ | ||
| PiB | ✅ | ✅ | ||
| Eb | ✅ | ✅ | ||
| EB | ✅ | ✅ | ||
| Eib | ✅ | ✅ | ||
| EiB | ✅ | ✅ | ||
| Zb | ✅ | ✅ | ||
| ZB | ✅ | ✅ | ||
| Zib | ✅ | ✅ | ||
| ZiB | ✅ | ✅ | ||
| Yb | ✅ | ✅ | ||
| YB | ✅ | ✅ | ||
| Yib | ✅ | ✅ | ||
| YiB | ✅ | ✅ |