Skip to content

Convert a human-readable string to bytes: '1 GiB' → 1073741824

License

Notifications You must be signed in to change notification settings

rivea0/unpretty-bytes

Repository files navigation

unpretty-bytes

Convert a human-readable string to bytes: 1 GiB1073741824

Similar to pretty-bytes, but in reverse. Partly based on python-humanfriendly.

It uses base-10 (e.g., kilobyte), but the option for binary (base-2) can be given.

Install

To install as a dependency from GitHub, run:

npm install github:rivea0/unpretty-bytes

Or, to use locally, clone the repository:

git clone [email protected]:rivea0/unpretty-bytes.git

cd into it:

cd unpretty-bytes

Install dependencies:

npm install

Compile TypeScript to create dist folder:

npm run build

Usage

import unprettyBytes from 'unpretty-bytes';

unprettyBytes('42');
// -> 42

unprettyBytes('1 kB');
// -> 1000

// With binary option true
unprettyBytes('1 kB', true);
// -> 1024

// Works with expanded unit strings
unprettyBytes('1 kilobyte');
// -> 1000

// Works with base-2 units
unprettyBytes('1 KiB');
// -> 1024

Note

When you pass a base-2 ("bibyte") unit, it's by default a binary unit.

So, doing this:

unprettyBytes('1 KiB');
// -> 1024

is the same as:

unprettyBytes('1 KiB', true);
// -> 1024

However, you can pass in a byte unit like this:

unprettyBytes('1 kB');
// -> 1000

And, also demand it to be in binary:

unprettyBytes('1 kB', true);
// -> 1024

The reason is to avoid confusion, as base-10 units are seemingly used more commonly, especially in everyday speech.

Note about "kB"

In the SI system, a lower case k represents a thousand, a capital M represents a million, a capital G represents a billion, etc.

The gist is that case sensitivity matters, for example, "1 kb" is "1 kilobit." So, "1 KB" is essentially meaningless as "K" refers to the degree Kelvin.

License

MIT

About

Convert a human-readable string to bytes: '1 GiB' → 1073741824

Resources

License

Stars

Watchers

Forks

Packages

No packages published