From ae082c6d069fb0471fd7de097db82d05afe3d84c Mon Sep 17 00:00:00 2001 From: SBPLtdProgrammers <55082201+SBPLtdProgrammers@users.noreply.github.com> Date: Mon, 9 Sep 2019 08:21:40 -0700 Subject: [PATCH 1/8] Added binary option to enable 1024 based counting. --- index.d.ts | 16 ++++++++++++++++ index.js | 20 ++++++++++++++++---- 2 files changed, 32 insertions(+), 4 deletions(-) diff --git a/index.d.ts b/index.d.ts index 9a64626..0a58250 100644 --- a/index.d.ts +++ b/index.d.ts @@ -31,6 +31,22 @@ declare namespace prettyBytes { ``` */ readonly bits?: boolean; + + /** + Format the number using the [Binary Prefix](https://en.wikipedia.org/wiki/Binary_prefix) instead of the [SI Prefix](https://en.wikipedia.org/wiki/SI_Prefix). This can be useful as memory is much better represented by powers of 2. + + @default false + + ``` + import prettyBytes = require('pretty-bytes'); + + prettyBytes(1000, {binary: true}); + //=> '1000 bit' + prettyBytes(1024, {binary: true}); + //=> '1 kiB' + ``` + */ + readonly binary?: boolean; } } diff --git a/index.js b/index.js index b7a0bac..3b806e5 100644 --- a/index.js +++ b/index.js @@ -12,6 +12,18 @@ const BYTE_UNITS = [ 'YB' ]; +const BIBYTE_UNITS = [ + 'B', + 'kiB', + 'MiB', + 'GiB', + 'TiB', + 'PiB', + 'EiB', + 'ZiB', + 'YiB' +]; + const BIT_UNITS = [ 'b', 'kbit', @@ -46,8 +58,8 @@ module.exports = (number, options) => { throw new TypeError(`Expected a finite number, got ${typeof number}: ${number}`); } - options = Object.assign({bits: false}, options); - const UNITS = options.bits ? BIT_UNITS : BYTE_UNITS; + options = Object.assign({bits: false, binary: false}, options); + const UNITS = options.bits ? BIT_UNITS : options.binary ? BIBYTE_UNITS : BYTE_UNITS; if (options.signed && number === 0) { return ' 0 ' + UNITS[0]; @@ -65,9 +77,9 @@ module.exports = (number, options) => { return prefix + numberString + ' ' + UNITS[0]; } - const exponent = Math.min(Math.floor(Math.log10(number) / 3), UNITS.length - 1); + const exponent = Math.min(Math.floor(Math.log(number) / Math.log(options.binary ? 1024 : 1000)), UNITS.length - 1); // eslint-disable-next-line unicorn/prefer-exponentiation-operator - number = Number((number / Math.pow(1000, exponent)).toPrecision(3)); + number = Number((number / Math.pow(options.binary ? 1024 : 1000, exponent)).toPrecision(3)); const numberString = toLocaleString(number, options.locale); const unit = UNITS[exponent]; From aa2bfb5ff5dc5b6a734ff1c5439dc20dd4b63f02 Mon Sep 17 00:00:00 2001 From: Nathan Moinvaziri Date: Tue, 11 Aug 2020 14:33:17 -0700 Subject: [PATCH 2/8] Fixed units selection when options.binary is true. --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index 3b806e5..8c1639e 100644 --- a/index.js +++ b/index.js @@ -59,7 +59,7 @@ module.exports = (number, options) => { } options = Object.assign({bits: false, binary: false}, options); - const UNITS = options.bits ? BIT_UNITS : options.binary ? BIBYTE_UNITS : BYTE_UNITS; + const UNITS = (options.bits) ? ((options.binary) ? BIBYTE_UNITS : BIT_UNITS) : BYTE_UNITS; if (options.signed && number === 0) { return ' 0 ' + UNITS[0]; From 8f3bfe32a244d9e6223d2b50b9094caa3781e7e1 Mon Sep 17 00:00:00 2001 From: Nathan Moinvaziri Date: Tue, 11 Aug 2020 14:55:52 -0700 Subject: [PATCH 3/8] Added unit tests for binary option. --- index.test-d.ts | 1 + test.js | 12 ++++++++++++ 2 files changed, 13 insertions(+) diff --git a/index.test-d.ts b/index.test-d.ts index cfbd1c9..f7c94a7 100644 --- a/index.test-d.ts +++ b/index.test-d.ts @@ -8,3 +8,4 @@ expectType(prettyBytes(42, {signed: true})); expectType(prettyBytes(1337, {locale: 'de'})); expectType(prettyBytes(1337, {locale: true})); expectType(prettyBytes(1337, {bits: true})); +expectType(prettyBytes(1337, {binary: true})); diff --git a/test.js b/test.js index 2753d54..ff61e77 100644 --- a/test.js +++ b/test.js @@ -82,3 +82,15 @@ test('bits option', t => { t.is(prettyBytes(1e16, {bits: true}), '10 Pbit'); t.is(prettyBytes(1e30, {bits: true}), '1000000 Ybit'); }); + +test('binary option', t => { + t.is(prettyBytes(0, {binary: true}), '0 B'); + t.is(prettyBytes(4, {binary: true}), '4 B'); + t.is(prettyBytes(10, {binary: true}), '10 B'); + t.is(prettyBytes(10.1, {binary: true}), '10.1 B'); + t.is(prettyBytes(999, {binary: true}), '999 B'); + t.is(prettyBytes(1025, {binary: true}), '1 kB'); + t.is(prettyBytes(1001, {binary: true}), '1000 B'); + t.is(prettyBytes(1e16, {binary: true}), '8.88 PB'); + t.is(prettyBytes(1e30, {binary: true}), '827000 YB'); +}); From fdee1c97778ae96e3760fec4d5c2d885dda65eaa Mon Sep 17 00:00:00 2001 From: Nathan Moinvaziri Date: Thu, 13 Aug 2020 21:05:59 -0700 Subject: [PATCH 4/8] Improved comments for binary option. --- index.d.ts | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/index.d.ts b/index.d.ts index 0a58250..6beb74f 100644 --- a/index.d.ts +++ b/index.d.ts @@ -20,9 +20,9 @@ declare namespace prettyBytes { /** 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'); @@ -31,12 +31,12 @@ declare namespace prettyBytes { ``` */ readonly bits?: boolean; - + /** - Format the number using the [Binary Prefix](https://en.wikipedia.org/wiki/Binary_prefix) instead of the [SI Prefix](https://en.wikipedia.org/wiki/SI_Prefix). This can be useful as memory is much better represented by powers of 2. - + Format the number using the [Binary Prefix](https://en.wikipedia.org/wiki/Binary_prefix) instead of the [SI Prefix](https://en.wikipedia.org/wiki/SI_Prefix). This can be useful as memory is typically represented by powers of 2. On macOS and Linux file sizes are represented by powers of 10, however on Windows they are represented by powers of 2. + @default false - + ``` import prettyBytes = require('pretty-bytes'); From 119ebb90649487402e5fe651ff8658666fa10194 Mon Sep 17 00:00:00 2001 From: Nathan Moinvaziri Date: Thu, 13 Aug 2020 21:06:45 -0700 Subject: [PATCH 5/8] Use math.log10 calculation for non-binary case. Remove unnecessary parentheses. --- index.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 8c1639e..12676be 100644 --- a/index.js +++ b/index.js @@ -59,7 +59,7 @@ module.exports = (number, options) => { } options = Object.assign({bits: false, binary: false}, options); - const UNITS = (options.bits) ? ((options.binary) ? BIBYTE_UNITS : BIT_UNITS) : BYTE_UNITS; + const UNITS = options.bits ? (options.binary ? BIBYTE_UNITS : BIT_UNITS) : BYTE_UNITS; if (options.signed && number === 0) { return ' 0 ' + UNITS[0]; @@ -77,7 +77,7 @@ module.exports = (number, options) => { return prefix + numberString + ' ' + UNITS[0]; } - const exponent = Math.min(Math.floor(Math.log(number) / Math.log(options.binary ? 1024 : 1000)), UNITS.length - 1); + const exponent = Math.min(Math.floor(options.binary ? Math.log(number) / Math.log(1024) : Math.log10(number) / 3 ), UNITS.length - 1); // eslint-disable-next-line unicorn/prefer-exponentiation-operator number = Number((number / Math.pow(options.binary ? 1024 : 1000, exponent)).toPrecision(3)); const numberString = toLocaleString(number, options.locale); From 83d70198ccfda17acfd41a63abce8b2238aa2f07 Mon Sep 17 00:00:00 2001 From: Sindre Sorhus Date: Sun, 16 Aug 2020 12:13:17 +0200 Subject: [PATCH 6/8] Update index.d.ts --- index.d.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/index.d.ts b/index.d.ts index 6beb74f..681287b 100644 --- a/index.d.ts +++ b/index.d.ts @@ -33,7 +33,7 @@ declare namespace prettyBytes { readonly bits?: boolean; /** - Format the number using the [Binary Prefix](https://en.wikipedia.org/wiki/Binary_prefix) instead of the [SI Prefix](https://en.wikipedia.org/wiki/SI_Prefix). This can be useful as memory is typically represented by powers of 2. On macOS and Linux file sizes are represented by powers of 10, however on Windows they are represented by powers of 2. + Format the number using the [Binary Prefix](https://en.wikipedia.org/wiki/Binary_prefix) instead of the [SI Prefix](https://en.wikipedia.org/wiki/SI_Prefix). This can be useful for presenting memory amounts. However, this should not be used for presenting file sizes. @default false @@ -42,6 +42,7 @@ declare namespace prettyBytes { prettyBytes(1000, {binary: true}); //=> '1000 bit' + prettyBytes(1024, {binary: true}); //=> '1 kiB' ``` From 087e40653685318bfcf89abd15073b1386123b87 Mon Sep 17 00:00:00 2001 From: Nathan Moinvaziri Date: Sun, 23 Aug 2020 10:27:55 -0700 Subject: [PATCH 7/8] Fixed formatting in exponent calculation. --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index 12676be..d88fd8b 100644 --- a/index.js +++ b/index.js @@ -77,7 +77,7 @@ module.exports = (number, options) => { return prefix + numberString + ' ' + UNITS[0]; } - const exponent = Math.min(Math.floor(options.binary ? Math.log(number) / Math.log(1024) : Math.log10(number) / 3 ), UNITS.length - 1); + const exponent = Math.min(Math.floor(options.binary ? Math.log(number) / Math.log(1024) : Math.log10(number) / 3), UNITS.length - 1); // eslint-disable-next-line unicorn/prefer-exponentiation-operator number = Number((number / Math.pow(options.binary ? 1024 : 1000, exponent)).toPrecision(3)); const numberString = toLocaleString(number, options.locale); From 74f5e1a1dddb95225724c46072de6f59525d256a Mon Sep 17 00:00:00 2001 From: Nathan Moinvaziri Date: Sun, 23 Aug 2020 10:28:10 -0700 Subject: [PATCH 8/8] Added binary option to readme. --- readme.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/readme.md b/readme.md index ea10506..717377a 100644 --- a/readme.md +++ b/readme.md @@ -68,6 +68,13 @@ 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). +##### binary + +Type: `boolean`\ +Default: `false` + +Format the number using the [Binary Prefix](https://en.wikipedia.org/wiki/Binary_prefix) instead of the [SI Prefix](https://en.wikipedia.org/wiki/SI_Prefix). This can be useful for presenting memory amounts. However, this should not be used for presenting file sizes. + ##### locale Type: `boolean | string`\