From 5a4036dbbd77e6529f9f17ddace3f7b8e1801a5e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Levente=20Krizs=C3=A1n?= Date: Tue, 24 Jan 2023 18:29:38 +0100 Subject: [PATCH 01/14] Add missing example tags to existing examples --- index.d.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/index.d.ts b/index.d.ts index efcfd14..8ff9423 100644 --- a/index.d.ts +++ b/index.d.ts @@ -56,6 +56,7 @@ export interface Options { @default undefined + @example ``` import prettyBytes from 'pretty-bytes'; @@ -76,6 +77,7 @@ export interface Options { @default undefined + @example ``` import prettyBytes from 'pretty-bytes'; From 7fd0bd006d7f09506c7bd011bcb53186992f90cb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Levente=20Krizs=C3=A1n?= Date: Tue, 24 Jan 2023 18:32:33 +0100 Subject: [PATCH 02/14] Add type for `spaces` option --- index.d.ts | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/index.d.ts b/index.d.ts index 8ff9423..1533fe0 100644 --- a/index.d.ts +++ b/index.d.ts @@ -90,6 +90,27 @@ export interface Options { ``` */ readonly maximumFractionDigits?: number; + + /** + The number of spaces to put between the number and unit. + + If it is not set, the default value is to display 1 space between the number and unit. + + If it is set to a negative number, the number of spaces between the number and unit will be 0. + + @default 1 + + @example + ``` + import prettyBytes from 'pretty-bytes'; + prettyBytes(1920, {spaces: 0}); + //=> '1.9kB' + + prettyBytes(1920); + //=> '1.92 kB' + ``` + */ + readonly spaces?: number; } /** From 6016460a28fc090cf4c8ad19d68770c4f55cb78a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Levente=20Krizs=C3=A1n?= Date: Tue, 24 Jan 2023 18:32:59 +0100 Subject: [PATCH 03/14] Implement logic for `spaces` option --- index.js | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index ca4fd3f..ba5fc03 100644 --- a/index.js +++ b/index.js @@ -74,12 +74,14 @@ export default function prettyBytes(number, options) { ...options, }; + const separator = ' '.repeat(options.spaces === undefined ? 1 : Math.max(options.spaces, 0)); + const UNITS = options.bits ? (options.binary ? BIBIT_UNITS : BIT_UNITS) : (options.binary ? BIBYTE_UNITS : BYTE_UNITS); if (options.signed && number === 0) { - return ` 0 ${UNITS[0]}`; + return ` 0${separator}${UNITS[0]}`; } const isNegative = number < 0; @@ -101,7 +103,7 @@ export default function prettyBytes(number, options) { if (number < 1) { const numberString = toLocaleString(number, options.locale, localeOptions); - return prefix + numberString + ' ' + UNITS[0]; + return prefix + numberString + separator + UNITS[0]; } const exponent = Math.min(Math.floor(options.binary ? Math.log(number) / Math.log(1024) : Math.log10(number) / 3), UNITS.length - 1); @@ -115,5 +117,5 @@ export default function prettyBytes(number, options) { const unit = UNITS[exponent]; - return prefix + numberString + ' ' + unit; + return prefix + numberString + separator + unit; } From 500a6408e27ce136b02a5e58fb00e88f9336e3ae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Levente=20Krizs=C3=A1n?= Date: Tue, 24 Jan 2023 18:33:31 +0100 Subject: [PATCH 04/14] Add test for `spaces` option --- test.js | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/test.js b/test.js index f263d98..382ca58 100644 --- a/test.js +++ b/test.js @@ -146,3 +146,14 @@ test('fractional digits options', t => { t.is(prettyBytes(32_768, {minimumFractionDigits: 2, maximumFractionDigits: 3, binary: true}), '32.00 kiB'); t.is(prettyBytes(65_536, {minimumFractionDigits: 1, maximumFractionDigits: 3, binary: true}), '64.0 kiB'); }); + +test('spaces option', t => { + t.is(prettyBytes(0), '0 B'); + t.is(prettyBytes(0, {spaces: 0}), '0B'); + t.is(prettyBytes(999, {spaces: 0}), '999B'); + t.is(prettyBytes(999, {spaces: 2}), '999 B'); + t.is(prettyBytes(-13, {signed: true, spaces: 0}), '-13B'); + t.is(prettyBytes(-13, {signed: true, spaces: 3}), '-13 B'); + t.is(prettyBytes(42, {signed: true, spaces: 0}), '+42B'); + t.is(prettyBytes(42, {signed: true, spaces: 2}), '+42 B'); +}); From a4d5c5481378d259ee5891a420dc14aebd41840b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Levente=20Krizs=C3=A1n?= Date: Tue, 24 Jan 2023 18:34:00 +0100 Subject: [PATCH 05/14] Update readme for `spaces` option --- readme.md | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/readme.md b/readme.md index 133c592..7247920 100644 --- a/readme.md +++ b/readme.md @@ -123,6 +123,26 @@ prettyBytes(1920); //=> '1.92 kB' ``` +##### spaces + +Type: `number`\ +Default: `1` + +The number of spaces to put between the number and unit. + +If it is not set, the default value is to display 1 space between the number and unit. + +If it is set to a negative number, the number of spaces between the number and unit will be 0. + +```js +import prettyBytes from 'pretty-bytes'; +prettyBytes(1920, {spaces: 0}); +//=> '1.9kB' + +prettyBytes(1920); +//=> '1.92 kB' +``` + ## Related - [pretty-bytes-cli](https://github.com/sindresorhus/pretty-bytes-cli) - CLI for this module From 4d4ecbdb60009b83a8b0d843167c077a73ce336b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Levente=20Krizs=C3=A1n?= Date: Tue, 24 Jan 2023 18:34:34 +0100 Subject: [PATCH 06/14] Add type for `uppercaseKilo` option --- index.d.ts | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/index.d.ts b/index.d.ts index 1533fe0..8c552e9 100644 --- a/index.d.ts +++ b/index.d.ts @@ -111,6 +111,25 @@ export interface Options { ``` */ readonly spaces?: number; + + /** + Display an uppercase K for units with the "kilo" prefix. + + If not set, the default behavior is to display a lowercase "k" for units with the "kilo" prefix. + + @default undefined + + @example + ``` + import prettyBytes from 'pretty-bytes'; + prettyBytes(1920, {uppercaseK: true}); + //=> '1.9 KB' + + prettyBytes(1920); + //=> '1.92 kB' + ``` + */ + readonly uppercaseKilo?: boolean; } /** From 8693397dabeb32a38b441df923ba47251a4deabe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Levente=20Krizs=C3=A1n?= Date: Tue, 24 Jan 2023 18:34:50 +0100 Subject: [PATCH 07/14] Add logic for `uppercaseKilo` option --- index.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index ba5fc03..6ebdbeb 100644 --- a/index.js +++ b/index.js @@ -115,7 +115,10 @@ export default function prettyBytes(number, options) { const numberString = toLocaleString(Number(number), options.locale, localeOptions); - const unit = UNITS[exponent]; + let unit = UNITS[exponent]; + if (options.uppercaseKilo && exponent === 1) { + unit = 'K' + unit.slice(1); + } return prefix + numberString + separator + unit; } From 2c8451fa2e7fb22416860d3c852f1b814515b294 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Levente=20Krizs=C3=A1n?= Date: Tue, 24 Jan 2023 18:35:04 +0100 Subject: [PATCH 08/14] Add tests for `uppercaseKilo` option --- test.js | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/test.js b/test.js index 382ca58..3ee5403 100644 --- a/test.js +++ b/test.js @@ -157,3 +157,14 @@ test('spaces option', t => { t.is(prettyBytes(42, {signed: true, spaces: 0}), '+42B'); t.is(prettyBytes(42, {signed: true, spaces: 2}), '+42 B'); }); + +test('uppercaseKilo option', t => { + t.is(prettyBytes(1001), '1 kB'); + t.is(prettyBytes(1025, {binary: true}), '1 kiB'); + t.is(prettyBytes(1001, {bits: true}), '1 kbit'); + t.is(prettyBytes(1025, {bits: true, binary: true}), '1 kibit'); + t.is(prettyBytes(1001, {uppercaseKilo: true}), '1 KB'); + t.is(prettyBytes(1025, {binary: true, uppercaseKilo: true}), '1 KiB'); + t.is(prettyBytes(1001, {bits: true, uppercaseKilo: true}), '1 Kbit'); + t.is(prettyBytes(1025, {bits: true, binary: true, uppercaseKilo: true}), '1 Kibit'); +}); From a1be8c6466401c1d5b034ee447a46690b1ceac08 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Levente=20Krizs=C3=A1n?= Date: Tue, 24 Jan 2023 18:35:21 +0100 Subject: [PATCH 09/14] Add docs for `uppercaseKilo` option --- readme.md | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/readme.md b/readme.md index 7247920..89d3cd9 100644 --- a/readme.md +++ b/readme.md @@ -143,6 +143,24 @@ prettyBytes(1920); //=> '1.92 kB' ``` +##### uppercaseKilo + +Type: `boolean`\ +Default: `false` + +Display an uppercase K for units with the "kilo" prefix. + +If not set, the default behavior is to display a lowercase "k" for units with the "kilo" prefix. + +```js +import prettyBytes from 'pretty-bytes'; +prettyBytes(1920, {uppercaseK: true}); +//=> '1.9 KB' + +prettyBytes(1920); +//=> '1.92 kB' +``` + ## Related - [pretty-bytes-cli](https://github.com/sindresorhus/pretty-bytes-cli) - CLI for this module From d0d958f8360fc309603784ba84e1c77bf95c5c5e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Levente=20Krizs=C3=A1n?= Date: Tue, 24 Jan 2023 18:35:40 +0100 Subject: [PATCH 10/14] Add type tests for `uppercaseKilo` option --- index.test-d.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/index.test-d.ts b/index.test-d.ts index b98f89a..6e891a8 100644 --- a/index.test-d.ts +++ b/index.test-d.ts @@ -7,3 +7,5 @@ expectType(prettyBytes(1337, {locale: 'de'})); expectType(prettyBytes(1337, {locale: true})); expectType(prettyBytes(1337, {bits: true})); expectType(prettyBytes(1337, {binary: true})); +expectType(prettyBytes(1337, {spaces: 0})); +expectType(prettyBytes(1337, {uppercaseKilo: true})); From 634f5110bf64b1bbcb17cdd45210e176d2376ef0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Levente=20Krizs=C3=A1n?= Date: Tue, 24 Jan 2023 18:44:40 +0100 Subject: [PATCH 11/14] Add missing newlines in doc examples --- readme.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/readme.md b/readme.md index 89d3cd9..1755abd 100644 --- a/readme.md +++ b/readme.md @@ -136,6 +136,7 @@ If it is set to a negative number, the number of spaces between the number and u ```js import prettyBytes from 'pretty-bytes'; + prettyBytes(1920, {spaces: 0}); //=> '1.9kB' @@ -154,6 +155,7 @@ If not set, the default behavior is to display a lowercase "k" for units with th ```js import prettyBytes from 'pretty-bytes'; + prettyBytes(1920, {uppercaseK: true}); //=> '1.9 KB' From 89ce96617d6a61d1f8554d84bc1e8492bbb9d307 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Levente=20Krizs=C3=A1n?= Date: Thu, 26 Jan 2023 12:44:19 +0100 Subject: [PATCH 12/14] Refactor new params based on suggestions --- index.d.ts | 30 +++++++++++++----------------- index.js | 37 +++++++++++++++++++++++++++++-------- index.test-d.ts | 4 ++-- readme.md | 26 ++++++++++---------------- test.js | 26 +++++++++++++------------- 5 files changed, 67 insertions(+), 56 deletions(-) diff --git a/index.d.ts b/index.d.ts index 8c552e9..7ea62fe 100644 --- a/index.d.ts +++ b/index.d.ts @@ -92,44 +92,40 @@ export interface Options { readonly maximumFractionDigits?: number; /** - The number of spaces to put between the number and unit. + Do not put a space between the number and unit. - If it is not set, the default value is to display 1 space between the number and unit. - - If it is set to a negative number, the number of spaces between the number and unit will be 0. - - @default 1 + @default false @example ``` import prettyBytes from 'pretty-bytes'; - prettyBytes(1920, {spaces: 0}); + + prettyBytes(1920, {noSpace: true}); //=> '1.9kB' prettyBytes(1920); //=> '1.92 kB' ``` */ - readonly spaces?: number; + readonly noSpace?: boolean; /** - Display an uppercase K for units with the "kilo" prefix. - - If not set, the default behavior is to display a lowercase "k" for units with the "kilo" prefix. + Use [JEDEC units](https://en.wikipedia.org/wiki/JEDEC_memory_standards#Unit_prefixes_for_semiconductor_storage_capacity) when `binary` is set. Only for displaying bytes (`bits` not set). - @default undefined + @default false @example ``` import prettyBytes from 'pretty-bytes'; - prettyBytes(1920, {uppercaseK: true}); - //=> '1.9 KB' - prettyBytes(1920); - //=> '1.92 kB' + prettyBytes(1920, {binary: true, legacyBinaryByteUnits: true}); + //=> '1.88 KB' + + prettyBytes(1920, {binary: true}); + //=> '1.88 kiB' ``` */ - readonly uppercaseKilo?: boolean; + readonly legacyBinaryByteUnits?: boolean; } /** diff --git a/index.js b/index.js index 6ebdbeb..ab0777f 100644 --- a/index.js +++ b/index.js @@ -22,6 +22,18 @@ const BIBYTE_UNITS = [ 'YiB', ]; +const LEGACY_BIBYTE_UNITS = [ + 'B', + 'KB', + 'MB', + 'GB', + 'TB', + 'PB', + 'EB', + 'ZB', + 'YB', +]; + const BIT_UNITS = [ 'b', 'kbit', @@ -63,6 +75,18 @@ const toLocaleString = (number, locale, options) => { return result; }; +const determineUnit = options => { + if (options.bits) { + return options.binary ? BIBIT_UNITS : BIT_UNITS; + } + + if (options.binary) { + return options.legacyBinaryByteUnits ? LEGACY_BIBYTE_UNITS : BIBYTE_UNITS; + } + + return BYTE_UNITS; +}; + export default function prettyBytes(number, options) { if (!Number.isFinite(number)) { throw new TypeError(`Expected a finite number, got ${typeof number}: ${number}`); @@ -71,14 +95,14 @@ export default function prettyBytes(number, options) { options = { bits: false, binary: false, + legacyBinaryByteUnits: false, + noSpace: false, ...options, }; - const separator = ' '.repeat(options.spaces === undefined ? 1 : Math.max(options.spaces, 0)); + const UNITS = determineUnit(options); - const UNITS = options.bits - ? (options.binary ? BIBIT_UNITS : BIT_UNITS) - : (options.binary ? BIBYTE_UNITS : BYTE_UNITS); + const separator = options.noSpace ? '' : ' '; if (options.signed && number === 0) { return ` 0${separator}${UNITS[0]}`; @@ -115,10 +139,7 @@ export default function prettyBytes(number, options) { const numberString = toLocaleString(Number(number), options.locale, localeOptions); - let unit = UNITS[exponent]; - if (options.uppercaseKilo && exponent === 1) { - unit = 'K' + unit.slice(1); - } + const unit = UNITS[exponent]; return prefix + numberString + separator + unit; } diff --git a/index.test-d.ts b/index.test-d.ts index 6e891a8..66fe51e 100644 --- a/index.test-d.ts +++ b/index.test-d.ts @@ -7,5 +7,5 @@ expectType(prettyBytes(1337, {locale: 'de'})); expectType(prettyBytes(1337, {locale: true})); expectType(prettyBytes(1337, {bits: true})); expectType(prettyBytes(1337, {binary: true})); -expectType(prettyBytes(1337, {spaces: 0})); -expectType(prettyBytes(1337, {uppercaseKilo: true})); +expectType(prettyBytes(1337, {noSpace: true})); +expectType(prettyBytes(1337, {legacyBinaryByteUnits: true})); diff --git a/readme.md b/readme.md index 1755abd..251f159 100644 --- a/readme.md +++ b/readme.md @@ -123,44 +123,38 @@ prettyBytes(1920); //=> '1.92 kB' ``` -##### spaces +##### noSpace Type: `number`\ -Default: `1` - -The number of spaces to put between the number and unit. - -If it is not set, the default value is to display 1 space between the number and unit. +Default: `false` -If it is set to a negative number, the number of spaces between the number and unit will be 0. +Do not put a space between the number and unit. ```js import prettyBytes from 'pretty-bytes'; -prettyBytes(1920, {spaces: 0}); +prettyBytes(1920, {noSpace: true}); //=> '1.9kB' prettyBytes(1920); //=> '1.92 kB' ``` -##### uppercaseKilo +##### legacyBinaryByteUnits Type: `boolean`\ Default: `false` -Display an uppercase K for units with the "kilo" prefix. - -If not set, the default behavior is to display a lowercase "k" for units with the "kilo" prefix. +Use [JEDEC units](https://en.wikipedia.org/wiki/JEDEC_memory_standards#Unit_prefixes_for_semiconductor_storage_capacity) when `binary` is set. Only for displaying bytes (`bits` not set). ```js import prettyBytes from 'pretty-bytes'; -prettyBytes(1920, {uppercaseK: true}); -//=> '1.9 KB' +prettyBytes(1920, {binary: true, legacyBinaryByteUnits: true}); +//=> '1.88 KB' -prettyBytes(1920); -//=> '1.92 kB' +prettyBytes(1920, {binary: true}); +//=> '1.88 kiB' ``` ## Related diff --git a/test.js b/test.js index 3ee5403..dc1fcde 100644 --- a/test.js +++ b/test.js @@ -147,24 +147,24 @@ test('fractional digits options', t => { t.is(prettyBytes(65_536, {minimumFractionDigits: 1, maximumFractionDigits: 3, binary: true}), '64.0 kiB'); }); -test('spaces option', t => { +test('noSpace option', t => { t.is(prettyBytes(0), '0 B'); - t.is(prettyBytes(0, {spaces: 0}), '0B'); - t.is(prettyBytes(999, {spaces: 0}), '999B'); - t.is(prettyBytes(999, {spaces: 2}), '999 B'); - t.is(prettyBytes(-13, {signed: true, spaces: 0}), '-13B'); - t.is(prettyBytes(-13, {signed: true, spaces: 3}), '-13 B'); - t.is(prettyBytes(42, {signed: true, spaces: 0}), '+42B'); - t.is(prettyBytes(42, {signed: true, spaces: 2}), '+42 B'); + t.is(prettyBytes(0, {noSpace: true}), '0B'); + t.is(prettyBytes(999), '999 B'); + t.is(prettyBytes(999, {noSpace: true}), '999B'); + t.is(prettyBytes(-13, {signed: true}), '-13 B'); + t.is(prettyBytes(-13, {signed: true, noSpace: true}), '-13B'); + t.is(prettyBytes(42, {signed: true}), '+42 B'); + t.is(prettyBytes(42, {signed: true, noSpace: true}), '+42B'); }); -test('uppercaseKilo option', t => { +test('legacyBinaryByteUnits option', t => { t.is(prettyBytes(1001), '1 kB'); t.is(prettyBytes(1025, {binary: true}), '1 kiB'); t.is(prettyBytes(1001, {bits: true}), '1 kbit'); t.is(prettyBytes(1025, {bits: true, binary: true}), '1 kibit'); - t.is(prettyBytes(1001, {uppercaseKilo: true}), '1 KB'); - t.is(prettyBytes(1025, {binary: true, uppercaseKilo: true}), '1 KiB'); - t.is(prettyBytes(1001, {bits: true, uppercaseKilo: true}), '1 Kbit'); - t.is(prettyBytes(1025, {bits: true, binary: true, uppercaseKilo: true}), '1 Kibit'); + t.is(prettyBytes(1001, {legacyBinaryByteUnits: true}), '1 kB'); + t.is(prettyBytes(1025, {binary: true, legacyBinaryByteUnits: true}), '1 KB'); + t.is(prettyBytes(1001, {bits: true, legacyBinaryByteUnits: true}), '1 kbit'); + t.is(prettyBytes(1025, {bits: true, binary: true, legacyBinaryByteUnits: true}), '1 kibit'); }); From 1ced573cce90ae814ebd1fbc53ea6f81ed985b43 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Levente=20Krizs=C3=A1n?= Date: Thu, 26 Jan 2023 13:21:16 +0100 Subject: [PATCH 13/14] Change `noSpace` to `space` --- index.d.ts | 8 ++++---- index.js | 4 ++-- index.test-d.ts | 2 +- readme.md | 10 +++++----- test.js | 10 +++++----- 5 files changed, 17 insertions(+), 17 deletions(-) diff --git a/index.d.ts b/index.d.ts index 7ea62fe..796b3a1 100644 --- a/index.d.ts +++ b/index.d.ts @@ -92,22 +92,22 @@ export interface Options { readonly maximumFractionDigits?: number; /** - Do not put a space between the number and unit. + Put a space between the number and unit. - @default false + @default true @example ``` import prettyBytes from 'pretty-bytes'; - prettyBytes(1920, {noSpace: true}); + prettyBytes(1920, {space: false}); //=> '1.9kB' prettyBytes(1920); //=> '1.92 kB' ``` */ - readonly noSpace?: boolean; + readonly space?: boolean; /** Use [JEDEC units](https://en.wikipedia.org/wiki/JEDEC_memory_standards#Unit_prefixes_for_semiconductor_storage_capacity) when `binary` is set. Only for displaying bytes (`bits` not set). diff --git a/index.js b/index.js index ab0777f..8fb5eb9 100644 --- a/index.js +++ b/index.js @@ -96,13 +96,13 @@ export default function prettyBytes(number, options) { bits: false, binary: false, legacyBinaryByteUnits: false, - noSpace: false, + space: true, ...options, }; const UNITS = determineUnit(options); - const separator = options.noSpace ? '' : ' '; + const separator = options.space ? ' ' : ''; if (options.signed && number === 0) { return ` 0${separator}${UNITS[0]}`; diff --git a/index.test-d.ts b/index.test-d.ts index 66fe51e..f72feea 100644 --- a/index.test-d.ts +++ b/index.test-d.ts @@ -7,5 +7,5 @@ expectType(prettyBytes(1337, {locale: 'de'})); expectType(prettyBytes(1337, {locale: true})); expectType(prettyBytes(1337, {bits: true})); expectType(prettyBytes(1337, {binary: true})); -expectType(prettyBytes(1337, {noSpace: true})); +expectType(prettyBytes(1337, {space: true})); expectType(prettyBytes(1337, {legacyBinaryByteUnits: true})); diff --git a/readme.md b/readme.md index 251f159..dbb33f2 100644 --- a/readme.md +++ b/readme.md @@ -123,17 +123,17 @@ prettyBytes(1920); //=> '1.92 kB' ``` -##### noSpace +##### space -Type: `number`\ -Default: `false` +Type: `boolean`\ +Default: `true` -Do not put a space between the number and unit. +Put a space between the number and unit. ```js import prettyBytes from 'pretty-bytes'; -prettyBytes(1920, {noSpace: true}); +prettyBytes(1920, {space: false}); //=> '1.9kB' prettyBytes(1920); diff --git a/test.js b/test.js index dc1fcde..f55fc53 100644 --- a/test.js +++ b/test.js @@ -147,15 +147,15 @@ test('fractional digits options', t => { t.is(prettyBytes(65_536, {minimumFractionDigits: 1, maximumFractionDigits: 3, binary: true}), '64.0 kiB'); }); -test('noSpace option', t => { +test('space option', t => { t.is(prettyBytes(0), '0 B'); - t.is(prettyBytes(0, {noSpace: true}), '0B'); + t.is(prettyBytes(0, {space: false}), '0B'); t.is(prettyBytes(999), '999 B'); - t.is(prettyBytes(999, {noSpace: true}), '999B'); + t.is(prettyBytes(999, {space: false}), '999B'); t.is(prettyBytes(-13, {signed: true}), '-13 B'); - t.is(prettyBytes(-13, {signed: true, noSpace: true}), '-13B'); + t.is(prettyBytes(-13, {signed: true, space: false}), '-13B'); t.is(prettyBytes(42, {signed: true}), '+42 B'); - t.is(prettyBytes(42, {signed: true, noSpace: true}), '+42B'); + t.is(prettyBytes(42, {signed: true, space: false}), '+42B'); }); test('legacyBinaryByteUnits option', t => { From 32adff50e1cd95bb21695ec3f5592b9048c72b35 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Levente=20Krizs=C3=A1n?= Date: Wed, 1 Feb 2023 18:36:34 +0100 Subject: [PATCH 14/14] Remove `legacyBinaryUnits` option --- index.d.ts | 18 ------------------ index.js | 29 +++-------------------------- index.test-d.ts | 1 - readme.md | 17 ----------------- test.js | 11 ----------- 5 files changed, 3 insertions(+), 73 deletions(-) diff --git a/index.d.ts b/index.d.ts index 796b3a1..e7283cb 100644 --- a/index.d.ts +++ b/index.d.ts @@ -108,24 +108,6 @@ export interface Options { ``` */ readonly space?: boolean; - - /** - Use [JEDEC units](https://en.wikipedia.org/wiki/JEDEC_memory_standards#Unit_prefixes_for_semiconductor_storage_capacity) when `binary` is set. Only for displaying bytes (`bits` not set). - - @default false - - @example - ``` - import prettyBytes from 'pretty-bytes'; - - prettyBytes(1920, {binary: true, legacyBinaryByteUnits: true}); - //=> '1.88 KB' - - prettyBytes(1920, {binary: true}); - //=> '1.88 kiB' - ``` - */ - readonly legacyBinaryByteUnits?: boolean; } /** diff --git a/index.js b/index.js index 8fb5eb9..ae6f4d8 100644 --- a/index.js +++ b/index.js @@ -22,18 +22,6 @@ const BIBYTE_UNITS = [ 'YiB', ]; -const LEGACY_BIBYTE_UNITS = [ - 'B', - 'KB', - 'MB', - 'GB', - 'TB', - 'PB', - 'EB', - 'ZB', - 'YB', -]; - const BIT_UNITS = [ 'b', 'kbit', @@ -75,18 +63,6 @@ const toLocaleString = (number, locale, options) => { return result; }; -const determineUnit = options => { - if (options.bits) { - return options.binary ? BIBIT_UNITS : BIT_UNITS; - } - - if (options.binary) { - return options.legacyBinaryByteUnits ? LEGACY_BIBYTE_UNITS : BIBYTE_UNITS; - } - - return BYTE_UNITS; -}; - export default function prettyBytes(number, options) { if (!Number.isFinite(number)) { throw new TypeError(`Expected a finite number, got ${typeof number}: ${number}`); @@ -95,12 +71,13 @@ export default function prettyBytes(number, options) { options = { bits: false, binary: false, - legacyBinaryByteUnits: false, space: true, ...options, }; - const UNITS = determineUnit(options); + const UNITS = options.bits + ? (options.binary ? BIBIT_UNITS : BIT_UNITS) + : (options.binary ? BIBYTE_UNITS : BYTE_UNITS); const separator = options.space ? ' ' : ''; diff --git a/index.test-d.ts b/index.test-d.ts index f72feea..7f56f9c 100644 --- a/index.test-d.ts +++ b/index.test-d.ts @@ -8,4 +8,3 @@ expectType(prettyBytes(1337, {locale: true})); expectType(prettyBytes(1337, {bits: true})); expectType(prettyBytes(1337, {binary: true})); expectType(prettyBytes(1337, {space: true})); -expectType(prettyBytes(1337, {legacyBinaryByteUnits: true})); diff --git a/readme.md b/readme.md index dbb33f2..e9cba34 100644 --- a/readme.md +++ b/readme.md @@ -140,23 +140,6 @@ prettyBytes(1920); //=> '1.92 kB' ``` -##### legacyBinaryByteUnits - -Type: `boolean`\ -Default: `false` - -Use [JEDEC units](https://en.wikipedia.org/wiki/JEDEC_memory_standards#Unit_prefixes_for_semiconductor_storage_capacity) when `binary` is set. Only for displaying bytes (`bits` not set). - -```js -import prettyBytes from 'pretty-bytes'; - -prettyBytes(1920, {binary: true, legacyBinaryByteUnits: true}); -//=> '1.88 KB' - -prettyBytes(1920, {binary: true}); -//=> '1.88 kiB' -``` - ## Related - [pretty-bytes-cli](https://github.com/sindresorhus/pretty-bytes-cli) - CLI for this module diff --git a/test.js b/test.js index f55fc53..bd27450 100644 --- a/test.js +++ b/test.js @@ -157,14 +157,3 @@ test('space option', t => { t.is(prettyBytes(42, {signed: true}), '+42 B'); t.is(prettyBytes(42, {signed: true, space: false}), '+42B'); }); - -test('legacyBinaryByteUnits option', t => { - t.is(prettyBytes(1001), '1 kB'); - t.is(prettyBytes(1025, {binary: true}), '1 kiB'); - t.is(prettyBytes(1001, {bits: true}), '1 kbit'); - t.is(prettyBytes(1025, {bits: true, binary: true}), '1 kibit'); - t.is(prettyBytes(1001, {legacyBinaryByteUnits: true}), '1 kB'); - t.is(prettyBytes(1025, {binary: true, legacyBinaryByteUnits: true}), '1 KB'); - t.is(prettyBytes(1001, {bits: true, legacyBinaryByteUnits: true}), '1 kbit'); - t.is(prettyBytes(1025, {bits: true, binary: true, legacyBinaryByteUnits: true}), '1 kibit'); -});