2.0.8 • Published 2 years ago

@wtfcode/byte-converter v2.0.8

Weekly downloads
8
License
MIT
Repository
github
Last release
2 years ago

Full Documentation

Getting Started

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.

Install

  • yarn add @wtfcode/byte-converter

or

  • npm i @wtfcode/byte-converter

Exports

Default

  • class ByteConverter

Named

  • class Unit
  • class UnitValue
  • class ByteConverter
  • const b: Unit
  • const B: Unit

Changelog

fromtodescription
2.0.12.0.5Added Arithmetic method to UnitValue
2.0.1Rewrited the byteConverter implementation in typescript and heavily simplified the algorithm
1.7.101.7.11autoScale now choose between 1000 and 1024 if is decimal type or binary type, the autoscaling is now better

Example

ES6 syntax:

import {
  ByteConverter,
  Unit,
  UnitValue,
  B as Byte,
} from "@wtfcode/byte-converter";

console.log(ByteConverter.convert(Byte.value(1), "b")); //will output 8

console.log(ByteConverter.convert(ByteConverter.value(1024, "MiB"), "GiB")); //will output 1

console.log(new UnitValue(1024, "B").autoScale());

//will output: UnitValue {value: 1, unit: Unit { unit: "KiB", type: "binary", unitOrder: 1, name: "kibibyte" ... } } // the function return a UnitValue scaled

ES5 syntax:

const ByteConverter = require("@wtfcode/byte-converter").default;

console.log(ByteConverter.convert(ByteConverter.value(1024, "MiB"), "GiB")); //will output 1

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" ... } }

Units Table

unitbitbytebinarydecimal
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
2.0.8

2 years ago

2.0.2

3 years ago

2.0.5

3 years ago

2.0.7

3 years ago

2.0.6

3 years ago

2.0.1

3 years ago

1.7.11

4 years ago

1.7.10

4 years ago

1.7.2

4 years ago

1.7.1

4 years ago

1.7.0

4 years ago

1.7.9

4 years ago

1.7.8

4 years ago

1.6.9

4 years ago

1.7.7

4 years ago

1.7.5

4 years ago

1.6.3

4 years ago

1.6.2

4 years ago

1.6.1

4 years ago

1.6.0

4 years ago

1.5.0

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago