diff --git a/index.d.ts b/index.d.ts index c281c14..9a64626 100644 --- a/index.d.ts +++ b/index.d.ts @@ -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; } } diff --git a/index.js b/index.js index de1c4b4..b7a0bac 100644 --- a/index.js +++ b/index.js @@ -1,6 +1,6 @@ 'use strict'; -const UNITS = [ +const BYTE_UNITS = [ 'B', 'kB', 'MB', @@ -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`). @@ -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; @@ -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); diff --git a/index.test-d.ts b/index.test-d.ts index d46cb81..cfbd1c9 100644 --- a/index.test-d.ts +++ b/index.test-d.ts @@ -7,3 +7,4 @@ expectType(prettyBytes(1337)); expectType(prettyBytes(42, {signed: true})); expectType(prettyBytes(1337, {locale: 'de'})); expectType(prettyBytes(1337, {locale: true})); +expectType(prettyBytes(1337, {bits: true})); diff --git a/readme.md b/readme.md index 6b52016..0f77498 100644 --- a/readme.md +++ b/readme.md @@ -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' @@ -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`
+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 diff --git a/test.js b/test.js index 1eafd22..2753d54 100644 --- a/test.js +++ b/test.js @@ -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'); +});