0.1.1 • Published 2 years ago

terrible-bytes v0.1.1

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

Terrible-Bytes 🍰

NPM

Convey size information in human friendly format to your js runtime.
Bid adieu to those numbers without units 🥳.

Simple to use, Easy to Understand

import {b} from 'terrible-bytes';

const uploadLimitBytes = b`8MB`
const xInBytes = b`5G` + b`640M` // approx. 5.64Gigabytes
const yInBytes = b`5.64G`       // approximately similar to above

Interpolation Support

const filesize = 256;
const unit = 'MiB';

const lol = b`${filesize}${unit}`;
const xox = b`468 ${unit}`;

Let's get rid of those KB, MB, GB constants. And stop doing size=8*MB

Installation

npm install terrible-bytes

Supported Units

Only S.I and ISO/IEC standard unit prefixes are allowed.
In the case of K we make an exception and treat it as kilobyte.
However, a warning is consoled when K is provided as the unit.

The ISO and IEC table are given below for the sake of convenience,

ValueSI Unit
1000kkilo
10002Mmega
10003Ggiga
10004Ttera
10005Ppeta
10006Eexa
10007Zzetta
10008Yyotta
ValueIECUnit
1000Kikibi
10002Mimebi
10003Gigibi
10004Titebi
10005Pipebi
10006Eiexbi
10007Zizebi
10008Yiyobi

Apart from the values in the table, 'B' can be used to mean byte. Here are some valid strings

[ 
  b`4`, 
  b`4B`,
  b`4M`,
  b`4MB`,
  b`4Mi`, 
  b`4MiB`,
]

You can read more about unit prefixes in wikipedia

What Does this have that other libs don't?

1) I haven't seen the use of tagged template for specifying sizes before(its probably because i haven't looked hard enough) 2) Does this question have to be strawmaned every time someone reinvents the wheel :| 3) Idk, go use the other lib if you like that better, no one has the authority to stop you from doing that champ. May the 🪄 help in your journey.

License

Libre Expat(MIT) License