From 07d827854e94260dd19169cbfa910186ea7f48a9 Mon Sep 17 00:00:00 2001 From: Monty Anderson Date: Wed, 10 Jul 2019 13:07:56 +0100 Subject: [PATCH 01/10] add bit units --- index.js | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index de1c4b4..88d7f43 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`). @@ -40,6 +52,8 @@ module.exports = (number, options) => { return ' 0 B'; } + const UNITS = options.bits !== true ? BYTE_UNITS : BIT_UNITS; + const isNegative = number < 0; const prefix = isNegative ? '-' : (options.signed ? '+' : ''); From e7f9925fea186aceb1407741dea5e46936901af4 Mon Sep 17 00:00:00 2001 From: Monty Anderson Date: Wed, 10 Jul 2019 13:23:49 +0100 Subject: [PATCH 02/10] add tests and docs --- index.d.ts | 12 ++++++++++++ index.js | 7 +++---- index.test-d.ts | 1 + readme.md | 10 ++++++++++ test.js | 13 +++++++++++++ 5 files changed, 39 insertions(+), 4 deletions(-) diff --git a/index.d.ts b/index.d.ts index c281c14..1824748 100644 --- a/index.d.ts +++ b/index.d.ts @@ -17,6 +17,14 @@ declare namespace prettyBytes { @default false */ readonly locale?: boolean | string; + + /** + Display proper units for bits. + + @default false + */ + + readonly bits?: boolean; } } @@ -42,6 +50,10 @@ prettyBytes(42, {signed: true}); // Localized output using German locale prettyBytes(1337, {locale: 'de'}); //=> '1,34 kB' + +// Display with units of bits +prettyBytes(1337, {bits: true}); +// => '1,34 kbit' ``` */ declare function prettyBytes( diff --git a/index.js b/index.js index 88d7f43..5328acb 100644 --- a/index.js +++ b/index.js @@ -47,13 +47,12 @@ module.exports = (number, options) => { } options = Object.assign({}, options); + const UNITS = options.bits === true ? BIT_UNITS : BYTE_UNITS; if (options.signed && number === 0) { - return ' 0 B'; + return ' 0 ' + UNITS[0]; } - const UNITS = options.bits !== true ? BYTE_UNITS : BIT_UNITS; - const isNegative = number < 0; const prefix = isNegative ? '-' : (options.signed ? '+' : ''); @@ -63,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..dc0bd1c 100644 --- a/readme.md +++ b/readme.md @@ -33,6 +33,10 @@ prettyBytes(42, {signed: true}); // Localized output using German locale prettyBytes(1337, {locale: 'de'}); //=> '1,34 kB' + +// Display with units of bits +prettyBytes(1337, {bits: true}); +// => '1,34 kbit' ``` @@ -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` + +Treat input as bits and use proper units respectively. ##### 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'); +}); From c0bd0023259aa25d21647621a1620f933bd34896 Mon Sep 17 00:00:00 2001 From: Monty Anderson Date: Wed, 10 Jul 2019 13:28:28 +0100 Subject: [PATCH 03/10] fix typos in docs --- index.d.ts | 6 +++++- readme.md | 6 +++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/index.d.ts b/index.d.ts index 1824748..cead6f0 100644 --- a/index.d.ts +++ b/index.d.ts @@ -43,6 +43,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' @@ -53,7 +57,7 @@ prettyBytes(1337, {locale: 'de'}); // Display with units of bits prettyBytes(1337, {bits: true}); -// => '1,34 kbit' +// => '1,34 kb' ``` */ declare function prettyBytes( diff --git a/readme.md b/readme.md index dc0bd1c..cddf73f 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' @@ -36,7 +40,7 @@ prettyBytes(1337, {locale: 'de'}); // Display with units of bits prettyBytes(1337, {bits: true}); -// => '1,34 kbit' +// => '1,34 kb' ``` From bf25220869dd18b71f7a69709040b7940e7609bb Mon Sep 17 00:00:00 2001 From: Shawn Wilkinson Date: Wed, 10 Jul 2019 08:30:12 -0400 Subject: [PATCH 04/10] remove duplicate example --- readme.md | 4 ---- 1 file changed, 4 deletions(-) diff --git a/readme.md b/readme.md index cddf73f..519c802 100644 --- a/readme.md +++ b/readme.md @@ -37,10 +37,6 @@ prettyBytes(42, {signed: true}); // Localized output using German locale prettyBytes(1337, {locale: 'de'}); //=> '1,34 kB' - -// Display with units of bits -prettyBytes(1337, {bits: true}); -// => '1,34 kb' ``` From ec7447aeb50e49e3444aa2910d2d7da4c739e69f Mon Sep 17 00:00:00 2001 From: Monty Anderson Date: Fri, 12 Jul 2019 13:15:34 +0100 Subject: [PATCH 05/10] implement sindresorhus' requests --- index.d.ts | 4 ---- index.js | 4 ++-- readme.md | 2 +- 3 files changed, 3 insertions(+), 7 deletions(-) diff --git a/index.d.ts b/index.d.ts index cead6f0..347634e 100644 --- a/index.d.ts +++ b/index.d.ts @@ -54,10 +54,6 @@ prettyBytes(42, {signed: true}); // Localized output using German locale prettyBytes(1337, {locale: 'de'}); //=> '1,34 kB' - -// Display with units of bits -prettyBytes(1337, {bits: true}); -// => '1,34 kb' ``` */ declare function prettyBytes( diff --git a/index.js b/index.js index 5328acb..b7a0bac 100644 --- a/index.js +++ b/index.js @@ -46,8 +46,8 @@ module.exports = (number, options) => { throw new TypeError(`Expected a finite number, got ${typeof number}: ${number}`); } - options = Object.assign({}, options); - const UNITS = options.bits === true ? BIT_UNITS : BYTE_UNITS; + options = Object.assign({bits: false}, options); + const UNITS = options.bits ? BIT_UNITS : BYTE_UNITS; if (options.signed && number === 0) { return ' 0 ' + UNITS[0]; diff --git a/readme.md b/readme.md index 519c802..1d28c19 100644 --- a/readme.md +++ b/readme.md @@ -66,7 +66,7 @@ Include plus sign for positive numbers. If the difference is exactly zero a spac Type: `boolean`
Default: `false` -Treat input as bits and use proper units respectively. +Treat input as [bits](https://en.wikipedia.org/wiki/Bit) and use proper units respectively. ##### locale From 562d2795b245eda769e63fc24c486e273835eeb2 Mon Sep 17 00:00:00 2001 From: Monty Anderson Date: Fri, 12 Jul 2019 13:19:03 +0100 Subject: [PATCH 06/10] move example to option definition --- index.d.ts | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/index.d.ts b/index.d.ts index 347634e..bb5c040 100644 --- a/index.d.ts +++ b/index.d.ts @@ -19,11 +19,12 @@ declare namespace prettyBytes { readonly locale?: boolean | string; /** - Display proper units for bits. + Display with units of bits + prettyBytes(1337, {bits: true}); + => '1.34 kbit' @default false */ - readonly bits?: boolean; } } @@ -43,10 +44,6 @@ 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' From 01fe13a601f7a48d14c92cdf76ca4aa1f7cd619d Mon Sep 17 00:00:00 2001 From: Monty Anderson Date: Fri, 12 Jul 2019 13:31:19 +0100 Subject: [PATCH 07/10] add 'bit rate' usage example --- readme.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/readme.md b/readme.md index 1d28c19..846de5f 100644 --- a/readme.md +++ b/readme.md @@ -66,7 +66,7 @@ Include plus sign for positive numbers. If the difference is exactly zero a spac Type: `boolean`
Default: `false` -Treat input as [bits](https://en.wikipedia.org/wiki/Bit) and use proper units respectively. +Treat input as [bits](https://en.wikipedia.org/wiki/Bit) instead of [bytes](https://en.wikipedia.org/wiki/Byte) and use proper units respectively. This is useful when referring to [bit rate](https://en.wikipedia.org/wiki/Bit_rate). ##### locale From 3b5041b7c5cd80253a2e6add2e25382853c07256 Mon Sep 17 00:00:00 2001 From: Sindre Sorhus Date: Fri, 26 Jul 2019 22:14:28 +0200 Subject: [PATCH 08/10] Update readme.md --- readme.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/readme.md b/readme.md index 846de5f..e642b85 100644 --- a/readme.md +++ b/readme.md @@ -66,7 +66,7 @@ Include plus sign for positive numbers. If the difference is exactly zero a spac Type: `boolean`
Default: `false` -Treat input as [bits](https://en.wikipedia.org/wiki/Bit) instead of [bytes](https://en.wikipedia.org/wiki/Byte) and use proper units respectively. This is useful when referring to [bit rate](https://en.wikipedia.org/wiki/Bit_rate). +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 From 6360e33a84086002279c170910509813d0c591ea Mon Sep 17 00:00:00 2001 From: Sindre Sorhus Date: Fri, 26 Jul 2019 22:15:37 +0200 Subject: [PATCH 09/10] Update index.d.ts --- index.d.ts | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/index.d.ts b/index.d.ts index bb5c040..9a64626 100644 --- a/index.d.ts +++ b/index.d.ts @@ -19,11 +19,16 @@ declare namespace prettyBytes { readonly locale?: boolean | string; /** - Display with units of bits - prettyBytes(1337, {bits: true}); - => '1.34 kbit' - + 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; } From 26c32c863fe3d0e4ca418bde3d365a0d28fd56ca Mon Sep 17 00:00:00 2001 From: Sindre Sorhus Date: Fri, 26 Jul 2019 22:16:55 +0200 Subject: [PATCH 10/10] Update readme.md --- readme.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/readme.md b/readme.md index e642b85..0f77498 100644 --- a/readme.md +++ b/readme.md @@ -28,7 +28,7 @@ prettyBytes(100); // Display with units of bits prettyBytes(1337, {bits: true}); -// => '1.34 kbit' +//=> '1.34 kbit' // Display file size differences prettyBytes(42, {signed: true});