Skip to content

Commit 1333349

Browse files
authoredAug 30, 2020
Fix binary and bits option being used together (#61)
1 parent 1000ebe commit 1333349

File tree

2 files changed

+27
-4
lines changed

2 files changed

+27
-4
lines changed
 

‎index.js

+15-1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,18 @@ const BIT_UNITS = [
3636
'Ybit'
3737
];
3838

39+
const BIBIT_UNITS = [
40+
'b',
41+
'kibit',
42+
'Mibit',
43+
'Gibit',
44+
'Tibit',
45+
'Pibit',
46+
'Eibit',
47+
'Zibit',
48+
'Yibit'
49+
];
50+
3951
/*
4052
Formats the given number using `Number#toLocaleString`.
4153
- If locale is a string, the value is expected to be a locale-key (for example: `de`).
@@ -59,7 +71,9 @@ module.exports = (number, options) => {
5971
}
6072

6173
options = Object.assign({bits: false, binary: false}, options);
62-
const UNITS = options.bits ? (options.binary ? BIBYTE_UNITS : BIT_UNITS) : BYTE_UNITS;
74+
const UNITS = options.bits ?
75+
(options.binary ? BIBIT_UNITS : BIT_UNITS) :
76+
(options.binary ? BIBYTE_UNITS : BYTE_UNITS);
6377

6478
if (options.signed && number === 0) {
6579
return ' 0 ' + UNITS[0];

‎test.js

+12-3
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,17 @@ test('binary option', t => {
9393
t.is(prettyBytes(10, {binary: true}), '10 B');
9494
t.is(prettyBytes(10.1, {binary: true}), '10.1 B');
9595
t.is(prettyBytes(999, {binary: true}), '999 B');
96-
t.is(prettyBytes(1025, {binary: true}), '1 kB');
96+
t.is(prettyBytes(1025, {binary: true}), '1 kiB');
9797
t.is(prettyBytes(1001, {binary: true}), '1000 B');
98-
t.is(prettyBytes(1e16, {binary: true}), '8.88 PB');
99-
t.is(prettyBytes(1e30, {binary: true}), '827000 YB');
98+
t.is(prettyBytes(1e16, {binary: true}), '8.88 PiB');
99+
t.is(prettyBytes(1e30, {binary: true}), '827000 YiB');
100+
});
101+
102+
test('bits and binary option', t => {
103+
t.is(prettyBytes(0, {bits: true, binary: true}), '0 b');
104+
t.is(prettyBytes(4, {bits: true, binary: true}), '4 b');
105+
t.is(prettyBytes(10, {bits: true, binary: true}), '10 b');
106+
t.is(prettyBytes(999, {bits: true, binary: true}), '999 b');
107+
t.is(prettyBytes(1025, {bits: true, binary: true}), '1 kibit');
108+
t.is(prettyBytes(1e6, {bits: true, binary: true}), '977 kibit');
100109
});

0 commit comments

Comments
 (0)
Please sign in to comment.