Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ declare namespace prettyBytes {

/**
Format the number as [bits](https://en.wikipedia.org/wiki/Bit) instead of [bytes](https://en.wikipedia.org/wiki/Byte). This can be useful when, for example, referring to [bit rate](https://en.wikipedia.org/wiki/Bit_rate).

@default false

```
import prettyBytes = require('pretty-bytes');

Expand All @@ -31,6 +31,23 @@ declare namespace prettyBytes {
```
*/
readonly bits?: boolean;

/**
Format the number using the [Binary Prefix](https://en.wikipedia.org/wiki/Binary_prefix) instead of the [SI Prefix](https://en.wikipedia.org/wiki/SI_Prefix). This can be useful for presenting memory amounts. However, this should not be used for presenting file sizes.

@default false

```
import prettyBytes = require('pretty-bytes');

prettyBytes(1000, {binary: true});
//=> '1000 bit'

prettyBytes(1024, {binary: true});
//=> '1 kiB'
```
*/
readonly binary?: boolean;
Comment thread
nmoinvaz marked this conversation as resolved.
}
}

Expand Down
20 changes: 16 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,18 @@ const BYTE_UNITS = [
'YB'
];

const BIBYTE_UNITS = [
'B',
'kiB',
'MiB',
'GiB',
'TiB',
'PiB',
'EiB',
'ZiB',
'YiB'
];

const BIT_UNITS = [
'b',
'kbit',
Expand Down Expand Up @@ -46,8 +58,8 @@ module.exports = (number, options) => {
throw new TypeError(`Expected a finite number, got ${typeof number}: ${number}`);
}

options = Object.assign({bits: false}, options);
const UNITS = options.bits ? BIT_UNITS : BYTE_UNITS;
options = Object.assign({bits: false, binary: false}, options);
const UNITS = options.bits ? (options.binary ? BIBYTE_UNITS : BIT_UNITS) : BYTE_UNITS;

if (options.signed && number === 0) {
return ' 0 ' + UNITS[0];
Expand All @@ -65,9 +77,9 @@ module.exports = (number, options) => {
return prefix + numberString + ' ' + UNITS[0];
}

const exponent = Math.min(Math.floor(Math.log10(number) / 3), UNITS.length - 1);
const exponent = Math.min(Math.floor(options.binary ? Math.log(number) / Math.log(1024) : Math.log10(number) / 3 ), UNITS.length - 1);
Comment thread
nmoinvaz marked this conversation as resolved.
Outdated
// eslint-disable-next-line unicorn/prefer-exponentiation-operator
number = Number((number / Math.pow(1000, exponent)).toPrecision(3));
number = Number((number / Math.pow(options.binary ? 1024 : 1000, exponent)).toPrecision(3));
Comment thread
nmoinvaz marked this conversation as resolved.
const numberString = toLocaleString(number, options.locale);

const unit = UNITS[exponent];
Expand Down
1 change: 1 addition & 0 deletions index.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ expectType<string>(prettyBytes(42, {signed: true}));
expectType<string>(prettyBytes(1337, {locale: 'de'}));
expectType<string>(prettyBytes(1337, {locale: true}));
expectType<string>(prettyBytes(1337, {bits: true}));
expectType<string>(prettyBytes(1337, {binary: true}));
12 changes: 12 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,15 @@ test('bits option', t => {
t.is(prettyBytes(1e16, {bits: true}), '10 Pbit');
t.is(prettyBytes(1e30, {bits: true}), '1000000 Ybit');
});

test('binary option', t => {
t.is(prettyBytes(0, {binary: true}), '0 B');
t.is(prettyBytes(4, {binary: true}), '4 B');
t.is(prettyBytes(10, {binary: true}), '10 B');
t.is(prettyBytes(10.1, {binary: true}), '10.1 B');
t.is(prettyBytes(999, {binary: true}), '999 B');
t.is(prettyBytes(1025, {binary: true}), '1 kB');
Comment thread
nmoinvaz marked this conversation as resolved.
t.is(prettyBytes(1001, {binary: true}), '1000 B');
t.is(prettyBytes(1e16, {binary: true}), '8.88 PB');
t.is(prettyBytes(1e30, {binary: true}), '827000 YB');
});