Skip to content

Convey size information in human friendly format to your js runtime.

License

Notifications You must be signed in to change notification settings

rmNULL/terrible-bytes

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

20 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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,

Value SI Unit
1000 k kilo
10002 M mega
10003 G giga
10004 T tera
10005 P peta
10006 E exa
10007 Z zetta
10008 Y yotta
Value IEC Unit
1000 Ki kibi
10002 Mi mebi
10003 Gi gibi
10004 Ti tebi
10005 Pi pebi
10006 Ei exbi
10007 Zi zebi
10008 Yi yobi

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