Skip to content

Commit cb93e5b

Browse files
Monty Andersonsindresorhus
Monty Anderson
andcommitted
Add bits option (sindresorhus#53)
Co-authored-by: Sindre Sorhus <[email protected]>
1 parent cdc6ea7 commit cb93e5b

File tree

5 files changed

+55
-4
lines changed

5 files changed

+55
-4
lines changed

index.d.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,20 @@ declare namespace prettyBytes {
1717
@default false
1818
*/
1919
readonly locale?: boolean | string;
20+
21+
/**
22+
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+
24+
@default false
25+
26+
```
27+
import prettyBytes = require('pretty-bytes');
28+
29+
prettyBytes(1337, {bits: true});
30+
//=> '1.34 kbit'
31+
```
32+
*/
33+
readonly bits?: boolean;
2034
}
2135
}
2236

index.js

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict';
22

3-
const UNITS = [
3+
const BYTE_UNITS = [
44
'B',
55
'kB',
66
'MB',
@@ -12,6 +12,18 @@ const UNITS = [
1212
'YB'
1313
];
1414

15+
const BIT_UNITS = [
16+
'b',
17+
'kbit',
18+
'Mbit',
19+
'Gbit',
20+
'Tbit',
21+
'Pbit',
22+
'Ebit',
23+
'Zbit',
24+
'Ybit'
25+
];
26+
1527
/*
1628
Formats the given number using `Number#toLocaleString`.
1729
- If locale is a string, the value is expected to be a locale-key (for example: `de`).
@@ -34,10 +46,11 @@ module.exports = (number, options) => {
3446
throw new TypeError(`Expected a finite number, got ${typeof number}: ${number}`);
3547
}
3648

37-
options = Object.assign({}, options);
49+
options = Object.assign({bits: false}, options);
50+
const UNITS = options.bits ? BIT_UNITS : BYTE_UNITS;
3851

3952
if (options.signed && number === 0) {
40-
return ' 0 B';
53+
return ' 0 ' + UNITS[0];
4154
}
4255

4356
const isNegative = number < 0;
@@ -49,7 +62,7 @@ module.exports = (number, options) => {
4962

5063
if (number < 1) {
5164
const numberString = toLocaleString(number, options.locale);
52-
return prefix + numberString + ' B';
65+
return prefix + numberString + ' ' + UNITS[0];
5366
}
5467

5568
const exponent = Math.min(Math.floor(Math.log10(number) / 3), UNITS.length - 1);

index.test-d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@ expectType<string>(prettyBytes(1337));
77
expectType<string>(prettyBytes(42, {signed: true}));
88
expectType<string>(prettyBytes(1337, {locale: 'de'}));
99
expectType<string>(prettyBytes(1337, {locale: true}));
10+
expectType<string>(prettyBytes(1337, {bits: true}));

readme.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ prettyBytes(1337);
2626
prettyBytes(100);
2727
//=> '100 B'
2828

29+
// Display with units of bits
30+
prettyBytes(1337, {bits: true});
31+
//=> '1.34 kbit'
32+
2933
// Display file size differences
3034
prettyBytes(42, {signed: true});
3135
//=> '+42 B'
@@ -57,6 +61,12 @@ Default: `false`
5761

5862
Include plus sign for positive numbers. If the difference is exactly zero a space character will be prepended instead for better alignment.
5963

64+
##### bits
65+
66+
Type: `boolean`<br>
67+
Default: `false`
68+
69+
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).
6070

6171
##### locale
6272

test.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,3 +69,16 @@ test('signed option', t => {
6969
t.is(prettyBytes(-13, {signed: true}), '-13 B');
7070
t.is(prettyBytes(0, {signed: true}), ' 0 B');
7171
});
72+
73+
test('bits option', t => {
74+
t.is(prettyBytes(0, {bits: true}), '0 b');
75+
t.is(prettyBytes(0.4, {bits: true}), '0.4 b');
76+
t.is(prettyBytes(0.7, {bits: true}), '0.7 b');
77+
t.is(prettyBytes(10, {bits: true}), '10 b');
78+
t.is(prettyBytes(10.1, {bits: true}), '10.1 b');
79+
t.is(prettyBytes(999, {bits: true}), '999 b');
80+
t.is(prettyBytes(1001, {bits: true}), '1 kbit');
81+
t.is(prettyBytes(1001, {bits: true}), '1 kbit');
82+
t.is(prettyBytes(1e16, {bits: true}), '10 Pbit');
83+
t.is(prettyBytes(1e30, {bits: true}), '1000000 Ybit');
84+
});

0 commit comments

Comments
 (0)