# @wtfcode/byte-converter

> Convert any value from a bit/byte unit measure to an another bit/byte measure, this package is compliant to IEC standard. EX: you can convert from KiB to kB or to Kib, Use the autoScale function for scale a given value and dataFormat to te best dataFormat

Latest version **2.0.8** (published 2022-06-14) · MIT license · 0 weekly downloads

## Install

```sh
npm install @wtfcode/byte-converter
pnpm add @wtfcode/byte-converter
yarn add @wtfcode/byte-converter
bun add @wtfcode/byte-converter
```

## Health

**Score 40/100 (D)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

Negative: abandoned.

## Facts

| | |
|---|---|
| Version | 2.0.8 |
| Published | 2022-06-14 |
| First published | 2020-01-16 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 177.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Author | bomdia |
| Maintainers | bomdia |
| Keywords | plus, minus, divide, multiply, byte math, bit math, byte, bit, conversion, byte-converter, autoconvert, byte converter, comparison, binary, decimal, kilobit, megabit, megabyte, mebibyte |

## Links

- npm: https://www.npmjs.com/package/@wtfcode/byte-converter
- Repository: https://github.com/bomdia/byte-converter
- Homepage: https://bomdia.github.io/byte-converter/#/
- Issues: https://github.com/bomdia/byte-converter/issues
- npm.io page: https://npm.io/package/@wtfcode/byte-converter

## Alternatives

- [random-seedable](https://npm.io/package/random-seedable.md) — 27.9K weekly downloads
- [n2words](https://npm.io/package/n2words.md) — 22.2K weekly downloads
- [@stdlib/math-base-special-factorialln](https://npm.io/package/@stdlib/math-base-special-factorialln.md) — 5.7K weekly downloads
- [@stdlib/math-base-special-abs2](https://npm.io/package/@stdlib/math-base-special-abs2.md) — 1.7K weekly downloads
- [commons-math-interpolation](https://npm.io/package/commons-math-interpolation.md) — 1.4K weekly downloads

## Recent versions

- 2.0.8 (latest) — 2022-06-14
- 2.0.7 — 2021-08-18
- 2.0.6 — 2021-08-18
- 2.0.5 — 2021-08-17
- 2.0.2 — 2021-08-17
- 2.0.1 — 2021-08-17
- 1.7.11 — 2020-02-23
- 1.7.10 — 2020-02-03
- 1.7.9 — 2020-01-31
- 1.7.8 — 2020-01-31
- 1.7.7 — 2020-01-31
- 1.7.5 — 2020-01-31
- 1.7.2 — 2020-01-31
- 1.7.1 — 2020-01-31
- 1.6.3 — 2020-01-22
- … 6 more at https://npm.io/package/@wtfcode/byte-converter/versions

## README

# [Full Documentation](https://bomdia.github.io/byte-converter/#/)

# Getting Started

**This package is compliant to IEC standard units.** see [Units Table](#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

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

## Example

ES6 syntax:

```js
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:

```js
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

| 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** |     | ✅   | ✅     |         |

---
_Source: https://npm.io/package/@wtfcode/byte-converter · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
