Skip to content

Commit 4ef01f4

Browse files
nmoinvazsindresorhus
andauthoredAug 29, 2020
Add binary option (#60)
Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>
1 parent 4a2eb76 commit 4ef01f4

File tree

5 files changed

+55
-6
lines changed

5 files changed

+55
-6
lines changed
 

Diff for: ‎index.d.ts

+19-2
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ declare namespace prettyBytes {
2020

2121
/**
2222
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).
23-
23+
2424
@default false
25-
25+
2626
```
2727
import prettyBytes = require('pretty-bytes');
2828
@@ -31,6 +31,23 @@ declare namespace prettyBytes {
3131
```
3232
*/
3333
readonly bits?: boolean;
34+
35+
/**
36+
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.
37+
38+
@default false
39+
40+
```
41+
import prettyBytes = require('pretty-bytes');
42+
43+
prettyBytes(1000, {binary: true});
44+
//=> '1000 bit'
45+
46+
prettyBytes(1024, {binary: true});
47+
//=> '1 kiB'
48+
```
49+
*/
50+
readonly binary?: boolean;
3451
}
3552
}
3653

Diff for: ‎index.js

+16-4
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,18 @@ const BYTE_UNITS = [
1212
'YB'
1313
];
1414

15+
const BIBYTE_UNITS = [
16+
'B',
17+
'kiB',
18+
'MiB',
19+
'GiB',
20+
'TiB',
21+
'PiB',
22+
'EiB',
23+
'ZiB',
24+
'YiB'
25+
];
26+
1527
const BIT_UNITS = [
1628
'b',
1729
'kbit',
@@ -46,8 +58,8 @@ module.exports = (number, options) => {
4658
throw new TypeError(`Expected a finite number, got ${typeof number}: ${number}`);
4759
}
4860

49-
options = Object.assign({bits: false}, options);
50-
const UNITS = options.bits ? BIT_UNITS : BYTE_UNITS;
61+
options = Object.assign({bits: false, binary: false}, options);
62+
const UNITS = options.bits ? (options.binary ? BIBYTE_UNITS : BIT_UNITS) : BYTE_UNITS;
5163

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

68-
const exponent = Math.min(Math.floor(Math.log10(number) / 3), UNITS.length - 1);
80+
const exponent = Math.min(Math.floor(options.binary ? Math.log(number) / Math.log(1024) : Math.log10(number) / 3), UNITS.length - 1);
6981
// eslint-disable-next-line unicorn/prefer-exponentiation-operator
70-
number = Number((number / Math.pow(1000, exponent)).toPrecision(3));
82+
number = Number((number / Math.pow(options.binary ? 1024 : 1000, exponent)).toPrecision(3));
7183
const numberString = toLocaleString(number, options.locale);
7284

7385
const unit = UNITS[exponent];

Diff for: ‎index.test-d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@ expectType<string>(prettyBytes(42, {signed: true}));
88
expectType<string>(prettyBytes(1337, {locale: 'de'}));
99
expectType<string>(prettyBytes(1337, {locale: true}));
1010
expectType<string>(prettyBytes(1337, {bits: true}));
11+
expectType<string>(prettyBytes(1337, {binary: true}));

Diff for: ‎readme.md

+7
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,13 @@ Default: `false`
6868

6969
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).
7070

71+
##### binary
72+
73+
Type: `boolean`\
74+
Default: `false`
75+
76+
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.
77+
7178
##### locale
7279

7380
Type: `boolean | string`\

Diff for: ‎test.js

+12
Original file line numberDiff line numberDiff line change
@@ -82,3 +82,15 @@ test('bits option', t => {
8282
t.is(prettyBytes(1e16, {bits: true}), '10 Pbit');
8383
t.is(prettyBytes(1e30, {bits: true}), '1000000 Ybit');
8484
});
85+
86+
test('binary option', t => {
87+
t.is(prettyBytes(0, {binary: true}), '0 B');
88+
t.is(prettyBytes(4, {binary: true}), '4 B');
89+
t.is(prettyBytes(10, {binary: true}), '10 B');
90+
t.is(prettyBytes(10.1, {binary: true}), '10.1 B');
91+
t.is(prettyBytes(999, {binary: true}), '999 B');
92+
t.is(prettyBytes(1025, {binary: true}), '1 kB');
93+
t.is(prettyBytes(1001, {binary: true}), '1000 B');
94+
t.is(prettyBytes(1e16, {binary: true}), '8.88 PB');
95+
t.is(prettyBytes(1e30, {binary: true}), '827000 YB');
96+
});

0 commit comments

Comments
 (0)
Please sign in to comment.