Skip to content

Commit

Permalink
Add bits option (#53)
Browse files Browse the repository at this point in the history
Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>
  • Loading branch information
Monty Anderson and sindresorhus committed Jul 26, 2019
1 parent cdc6ea7 commit cb93e5b
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 4 deletions.
14 changes: 14 additions & 0 deletions index.d.ts
Expand Up @@ -17,6 +17,20 @@ declare namespace prettyBytes {
@default false
*/
readonly locale?: boolean | string;

/**
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');
prettyBytes(1337, {bits: true});
//=> '1.34 kbit'
```
*/
readonly bits?: boolean;
}
}

Expand Down
21 changes: 17 additions & 4 deletions index.js
@@ -1,6 +1,6 @@
'use strict';

const UNITS = [
const BYTE_UNITS = [
'B',
'kB',
'MB',
Expand All @@ -12,6 +12,18 @@ const UNITS = [
'YB'
];

const BIT_UNITS = [
'b',
'kbit',
'Mbit',
'Gbit',
'Tbit',
'Pbit',
'Ebit',
'Zbit',
'Ybit'
];

/*
Formats the given number using `Number#toLocaleString`.
- If locale is a string, the value is expected to be a locale-key (for example: `de`).
Expand All @@ -34,10 +46,11 @@ module.exports = (number, options) => {
throw new TypeError(`Expected a finite number, got ${typeof number}: ${number}`);
}

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

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

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

if (number < 1) {
const numberString = toLocaleString(number, options.locale);
return prefix + numberString + ' B';
return prefix + numberString + ' ' + UNITS[0];
}

const exponent = Math.min(Math.floor(Math.log10(number) / 3), UNITS.length - 1);
Expand Down
1 change: 1 addition & 0 deletions index.test-d.ts
Expand Up @@ -7,3 +7,4 @@ expectType<string>(prettyBytes(1337));
expectType<string>(prettyBytes(42, {signed: true}));
expectType<string>(prettyBytes(1337, {locale: 'de'}));
expectType<string>(prettyBytes(1337, {locale: true}));
expectType<string>(prettyBytes(1337, {bits: true}));
10 changes: 10 additions & 0 deletions readme.md
Expand Up @@ -26,6 +26,10 @@ prettyBytes(1337);
prettyBytes(100);
//=> '100 B'

// Display with units of bits
prettyBytes(1337, {bits: true});
//=> '1.34 kbit'

// Display file size differences
prettyBytes(42, {signed: true});
//=> '+42 B'
Expand Down Expand Up @@ -57,6 +61,12 @@ Default: `false`

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

##### bits

Type: `boolean`<br>
Default: `false`

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

##### locale

Expand Down
13 changes: 13 additions & 0 deletions test.js
Expand Up @@ -69,3 +69,16 @@ test('signed option', t => {
t.is(prettyBytes(-13, {signed: true}), '-13 B');
t.is(prettyBytes(0, {signed: true}), ' 0 B');
});

test('bits option', t => {
t.is(prettyBytes(0, {bits: true}), '0 b');
t.is(prettyBytes(0.4, {bits: true}), '0.4 b');
t.is(prettyBytes(0.7, {bits: true}), '0.7 b');
t.is(prettyBytes(10, {bits: true}), '10 b');
t.is(prettyBytes(10.1, {bits: true}), '10.1 b');
t.is(prettyBytes(999, {bits: true}), '999 b');
t.is(prettyBytes(1001, {bits: true}), '1 kbit');
t.is(prettyBytes(1001, {bits: true}), '1 kbit');
t.is(prettyBytes(1e16, {bits: true}), '10 Pbit');
t.is(prettyBytes(1e30, {bits: true}), '1000000 Ybit');
});

0 comments on commit cb93e5b

Please sign in to comment.