From 4b9db652a736efaa8297923a43a726c6a39ef3d9 Mon Sep 17 00:00:00 2001 From: Nathan Moinvaziri Date: Sun, 30 Aug 2020 13:31:50 -0700 Subject: [PATCH 01/11] Added digits option to control locale fraction options. --- index.d.ts | 17 +++++++++++++++++ index.js | 23 +++++++++++++++++------ readme.md | 7 +++++++ test.js | 14 ++++++++++++++ 4 files changed, 55 insertions(+), 6 deletions(-) diff --git a/index.d.ts b/index.d.ts index bdd7df4..0e92df5 100644 --- a/index.d.ts +++ b/index.d.ts @@ -51,6 +51,23 @@ declare namespace prettyBytes { ``` */ readonly binary?: boolean; + + /** + Number of fraction digits to show when displaying bytes or bits. + + @default undefined + + ``` + import prettyBytes = require('pretty-bytes'); + + prettyBytes(1911); + // => '1.91 kB' + + prettyBytes(1911, {digits: 1}); + //=> '1.9 kB' + ``` + */ + readonly digits?: number; } } diff --git a/index.js b/index.js index ee15bad..2df817d 100644 --- a/index.js +++ b/index.js @@ -54,12 +54,12 @@ Formats the given number using `Number#toLocaleString`. - If locale is true, the system default locale is used for translation. - If no value for locale is specified, the number is returned unmodified. */ -const toLocaleString = (number, locale) => { +const toLocaleString = (number, locale, options) => { let result = number; if (typeof locale === 'string' || Array.isArray(locale)) { - result = number.toLocaleString(locale); + result = number.toLocaleString(locale, options); } else if (locale === true) { - result = number.toLocaleString(); + result = number.toLocaleString(undefined, options); } return result; @@ -87,15 +87,26 @@ module.exports = (number, options) => { number = -number; } + const locale = options.digits && !options.locale ? true : options.locale; + const localeOptions = options.digits ? { + minimumFactionDigits: options.digits, + maximumFractionDigits: options.digits + } : undefined; + if (number < 1) { - const numberString = toLocaleString(number, options.locale); + const numberString = toLocaleString(number, locale, localeOptions); 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); // 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); + number /= Math.pow(options.binary ? 1024 : 1000, exponent); + + if (!options.digits) { + number = number.toPrecision(3); + } + + const numberString = toLocaleString(Number(number), locale, localeOptions); const unit = UNITS[exponent]; diff --git a/readme.md b/readme.md index 59e84ec..8bf743e 100644 --- a/readme.md +++ b/readme.md @@ -72,6 +72,13 @@ 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. +##### digits + +Type: `number`\ +Default: `undefined` + +Number of fraction digits to show when displaying bytes or bits. If locale is not set, the number will be automatically localized using the system default localization. + ##### locale Type: `boolean | string`\ diff --git a/test.js b/test.js index c024af3..1a29419 100644 --- a/test.js +++ b/test.js @@ -113,3 +113,17 @@ test('bits and binary option', t => { t.is(prettyBytes(1025, {bits: true, binary: true}), '1 kibit'); t.is(prettyBytes(1e6, {bits: true, binary: true}), '977 kibit'); }); + +test('digits option', t => { + t.is(prettyBytes(1001), '1 kB'); + t.is(prettyBytes(1111), '1.11 kB'); + t.is(prettyBytes(1911), '1.91 kB'); + t.is(prettyBytes(1900, {digits: 1}), '1.9 kB'); + t.is(prettyBytes(1911, {digits: 1}), '1.9 kB'); + t.is(prettyBytes(1111, {digits: 2}), '1.11 kB'); + t.is(prettyBytes(1019, {digits: 3}), '1.019 kB'); + t.is(prettyBytes(1001, {digits: 3}), '1.001 kB'); + t.is(prettyBytes(4001, {digits: 3, binary: true}), '3.907 kB'); + t.is(prettyBytes(18717, {digits: 2, binary: true}), '18.28 kB'); + t.is(prettyBytes(18717, {digits: 4, binary: true}), '18.2783 kB'); +}); From e19c3852d2c1e21a90be7694528d03c799cf2db6 Mon Sep 17 00:00:00 2001 From: Nathan Moinvaziri Date: Fri, 1 Jan 2021 20:20:29 -0800 Subject: [PATCH 02/11] Control number of fraction digits through localeOptions option. --- index.d.ts | 36 +++++++++++++++++++----------------- index.js | 12 ++++-------- readme.md | 26 +++++++++++++++++++------- test.js | 26 ++++++++++++-------------- 4 files changed, 54 insertions(+), 46 deletions(-) diff --git a/index.d.ts b/index.d.ts index 0e92df5..30ee756 100644 --- a/index.d.ts +++ b/index.d.ts @@ -19,6 +19,25 @@ declare namespace prettyBytes { */ readonly locale?: boolean | string | readonly string[]; + /** + [NumberFormat](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat/NumberFormat) international localization options. + + The options `minimumFractionDigits` and `maximumFractionDigits` can be used to control the number of fractional digits displayed. + + @default undefined + + ``` + import prettyBytes = require('pretty-bytes'); + + prettyBytes(1900, {localeOptions: {maximumFractionDigits: 1}}); + //=> '1.9 kB' + + prettyBytes(1900, {localeOptions: {minimumFractionDigits: 3}}); + //=> '1.900 kB' + ``` + */ + readonly localeOptions?: object; + /** 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). @@ -51,23 +70,6 @@ declare namespace prettyBytes { ``` */ readonly binary?: boolean; - - /** - Number of fraction digits to show when displaying bytes or bits. - - @default undefined - - ``` - import prettyBytes = require('pretty-bytes'); - - prettyBytes(1911); - // => '1.91 kB' - - prettyBytes(1911, {digits: 1}); - //=> '1.9 kB' - ``` - */ - readonly digits?: number; } } diff --git a/index.js b/index.js index 2df817d..8c3ea84 100644 --- a/index.js +++ b/index.js @@ -87,14 +87,10 @@ module.exports = (number, options) => { number = -number; } - const locale = options.digits && !options.locale ? true : options.locale; - const localeOptions = options.digits ? { - minimumFactionDigits: options.digits, - maximumFractionDigits: options.digits - } : undefined; + const locale = !options.locale && options.localeOptions ? true : options.locale; if (number < 1) { - const numberString = toLocaleString(number, locale, localeOptions); + const numberString = toLocaleString(number, locale, options.localeOptions); return prefix + numberString + ' ' + UNITS[0]; } @@ -102,11 +98,11 @@ module.exports = (number, options) => { // eslint-disable-next-line unicorn/prefer-exponentiation-operator number /= Math.pow(options.binary ? 1024 : 1000, exponent); - if (!options.digits) { + if (!options.localeOptions) { number = number.toPrecision(3); } - const numberString = toLocaleString(Number(number), locale, localeOptions); + const numberString = toLocaleString(Number(number), locale, options.localeOptions); const unit = UNITS[exponent]; diff --git a/readme.md b/readme.md index 8bf743e..b8793e3 100644 --- a/readme.md +++ b/readme.md @@ -72,13 +72,6 @@ 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. -##### digits - -Type: `number`\ -Default: `undefined` - -Number of fraction digits to show when displaying bytes or bits. If locale is not set, the number will be automatically localized using the system default localization. - ##### locale Type: `boolean | string`\ @@ -92,6 +85,25 @@ Default: `false` *(No localization)* **Note:** Localization should generally work in browsers. Node.js needs to be [built](https://github.com/nodejs/node/wiki/Intl) with `full-icu` or `system-icu`. Alternatively, the [`full-icu`](https://github.com/unicode-org/full-icu-npm) module can be used to provide support at runtime. [Node.js 13](https://nodejs.org/en/blog/release/v13.0.0/) and later ships with ICU by default. +##### localeOptions + +Type: `object`\ +Default: `undefined` + +[NumberFormat](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat/NumberFormat) international localization options. The options `minimumFractionDigits` and `maximumFractionDigits` can be used to control the number of fractional digits displayed. + +```js +const prettyBytes = require('pretty-bytes'); + +// Show number with at most 1 fractional digit +prettyBytes(1900, {localeOptions: {maximumFractionDigits: 1}}); +//=> '1.9 kB' + +// Show number with at least 3 fractional digits +prettyBytes(1900, {localeOptions: {minimumFractionDigits: 3}}); +//=> '1.900 kB' +``` + ## Related - [pretty-bytes-cli](https://github.com/sindresorhus/pretty-bytes-cli) - CLI for this module diff --git a/test.js b/test.js index 1a29419..a2b345b 100644 --- a/test.js +++ b/test.js @@ -74,6 +74,18 @@ test('locale option', t => { } }); +test('localeOptions option', t => { + t.is(prettyBytes(1900, {localeOptions: {maximumFractionDigits: 1}}), '1.9 kB'); + t.is(prettyBytes(1900, {localeOptions: {minimumFractionDigits: 3}}), '1.900 kB'); + t.is(prettyBytes(1911, {localeOptions: {maximumFractionDigits: 1}}), '1.9 kB'); + t.is(prettyBytes(1111, {localeOptions: {maximumFractionDigits: 2}}), '1.11 kB'); + t.is(prettyBytes(1019, {localeOptions: {maximumFractionDigits: 3}}), '1.019 kB'); + t.is(prettyBytes(1001, {localeOptions: {maximumFractionDigits: 3}}), '1.001 kB'); + t.is(prettyBytes(4001, {localeOptions: {maximumFractionDigits: 3}, binary: true}), '3.907 kiB'); + t.is(prettyBytes(18717, {localeOptions: {maximumFractionDigits: 2}, binary: true}), '18.28 kiB'); + t.is(prettyBytes(18717, {localeOptions: {maximumFractionDigits: 4}, binary: true}), '18.2783 kiB'); +}); + test('signed option', t => { t.is(prettyBytes(42, {signed: true}), '+42 B'); t.is(prettyBytes(-13, {signed: true}), '-13 B'); @@ -113,17 +125,3 @@ test('bits and binary option', t => { t.is(prettyBytes(1025, {bits: true, binary: true}), '1 kibit'); t.is(prettyBytes(1e6, {bits: true, binary: true}), '977 kibit'); }); - -test('digits option', t => { - t.is(prettyBytes(1001), '1 kB'); - t.is(prettyBytes(1111), '1.11 kB'); - t.is(prettyBytes(1911), '1.91 kB'); - t.is(prettyBytes(1900, {digits: 1}), '1.9 kB'); - t.is(prettyBytes(1911, {digits: 1}), '1.9 kB'); - t.is(prettyBytes(1111, {digits: 2}), '1.11 kB'); - t.is(prettyBytes(1019, {digits: 3}), '1.019 kB'); - t.is(prettyBytes(1001, {digits: 3}), '1.001 kB'); - t.is(prettyBytes(4001, {digits: 3, binary: true}), '3.907 kB'); - t.is(prettyBytes(18717, {digits: 2, binary: true}), '18.28 kB'); - t.is(prettyBytes(18717, {digits: 4, binary: true}), '18.2783 kB'); -}); From 5640ac73adf2b77bca7341940f26d4890abcefb9 Mon Sep 17 00:00:00 2001 From: Nathan Moinvaziri Date: Thu, 7 Jan 2021 17:18:09 -0800 Subject: [PATCH 03/11] Convert localeOptions to minimumFractionDigits and maximumFractionDigits. --- index.d.ts | 48 +++++++++++++++++++++++++++++------------------- index.js | 20 ++++++++++++++------ readme.md | 27 +++++++++++++++++++-------- test.js | 24 ++++++++++++------------ 4 files changed, 74 insertions(+), 45 deletions(-) diff --git a/index.d.ts b/index.d.ts index 30ee756..710679a 100644 --- a/index.d.ts +++ b/index.d.ts @@ -19,25 +19,6 @@ declare namespace prettyBytes { */ readonly locale?: boolean | string | readonly string[]; - /** - [NumberFormat](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat/NumberFormat) international localization options. - - The options `minimumFractionDigits` and `maximumFractionDigits` can be used to control the number of fractional digits displayed. - - @default undefined - - ``` - import prettyBytes = require('pretty-bytes'); - - prettyBytes(1900, {localeOptions: {maximumFractionDigits: 1}}); - //=> '1.9 kB' - - prettyBytes(1900, {localeOptions: {minimumFractionDigits: 3}}); - //=> '1.900 kB' - ``` - */ - readonly localeOptions?: object; - /** 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). @@ -70,6 +51,35 @@ declare namespace prettyBytes { ``` */ readonly binary?: boolean; + + /** + The minimum number of fraction digits to display. + + @default undefined + + ``` + import prettyBytes = require('pretty-bytes'); + + prettyBytes(1900, {minimumFractionDigits: 3}); + //=> '1.900 kB' + ``` + */ + readonly minimumFractionDigits?: number; + + + /** + The maximum number of fraction digits to display. + + @default undefined + + ``` + import prettyBytes = require('pretty-bytes'); + + prettyBytes(1900, {maximumFractionDigits: 1}); + //=> '1.9 kB' + ``` + */ + readonly maximumFractionDigits?: number; } } diff --git a/index.js b/index.js index 8c3ea84..ced86f0 100644 --- a/index.js +++ b/index.js @@ -58,7 +58,7 @@ const toLocaleString = (number, locale, options) => { let result = number; if (typeof locale === 'string' || Array.isArray(locale)) { result = number.toLocaleString(locale, options); - } else if (locale === true) { + } else if (locale === true || options !== undefined) { result = number.toLocaleString(undefined, options); } @@ -70,7 +70,7 @@ module.exports = (number, options) => { throw new TypeError(`Expected a finite number, got ${typeof number}: ${number}`); } - options = Object.assign({bits: false, binary: false}, options); + options = Object.assign({bits: false, binary: false, minimumFractionDigits: undefined, maximumFractionDigits: undefined}, options); const UNITS = options.bits ? (options.binary ? BIBIT_UNITS : BIT_UNITS) : @@ -87,10 +87,18 @@ module.exports = (number, options) => { number = -number; } - const locale = !options.locale && options.localeOptions ? true : options.locale; + let localeOptions; + + if (options.minimumFractionDigits !== undefined) { + localeOptions = {minimumFractionDigits: options.minimumFractionDigits}; + } + + if (options.maximumFractionDigits !== undefined) { + localeOptions = Object.assign({maximumFractionDigits: options.maximumFractionDigits}, localeOptions); + } if (number < 1) { - const numberString = toLocaleString(number, locale, options.localeOptions); + const numberString = toLocaleString(number, options.locale, localeOptions); return prefix + numberString + ' ' + UNITS[0]; } @@ -98,11 +106,11 @@ module.exports = (number, options) => { // eslint-disable-next-line unicorn/prefer-exponentiation-operator number /= Math.pow(options.binary ? 1024 : 1000, exponent); - if (!options.localeOptions) { + if (!localeOptions) { number = number.toPrecision(3); } - const numberString = toLocaleString(Number(number), locale, options.localeOptions); + const numberString = toLocaleString(Number(number), options.locale, localeOptions); const unit = UNITS[exponent]; diff --git a/readme.md b/readme.md index b8793e3..eed0773 100644 --- a/readme.md +++ b/readme.md @@ -85,25 +85,36 @@ Default: `false` *(No localization)* **Note:** Localization should generally work in browsers. Node.js needs to be [built](https://github.com/nodejs/node/wiki/Intl) with `full-icu` or `system-icu`. Alternatively, the [`full-icu`](https://github.com/unicode-org/full-icu-npm) module can be used to provide support at runtime. [Node.js 13](https://nodejs.org/en/blog/release/v13.0.0/) and later ships with ICU by default. -##### localeOptions +##### minimumFractionDigits -Type: `object`\ +Type: `number`\ Default: `undefined` -[NumberFormat](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat/NumberFormat) international localization options. The options `minimumFractionDigits` and `maximumFractionDigits` can be used to control the number of fractional digits displayed. +The minimum number of fraction digits to display. ```js const prettyBytes = require('pretty-bytes'); -// Show number with at most 1 fractional digit -prettyBytes(1900, {localeOptions: {maximumFractionDigits: 1}}); -//=> '1.9 kB' - // Show number with at least 3 fractional digits -prettyBytes(1900, {localeOptions: {minimumFractionDigits: 3}}); +prettyBytes(1900, {minimumFractionDigits: 3}); //=> '1.900 kB' ``` +##### maximumFractionDigits + +Type: `number`\ +Default: `undefined` + +The maximum number of fraction digits to display. + +```js +const prettyBytes = require('pretty-bytes'); + +// Show number with at most 1 fractional digit +prettyBytes(1900, {maximumFractionDigits: 1}); +//=> '1.9 kB' +``` + ## Related - [pretty-bytes-cli](https://github.com/sindresorhus/pretty-bytes-cli) - CLI for this module diff --git a/test.js b/test.js index a2b345b..ce6f710 100644 --- a/test.js +++ b/test.js @@ -74,18 +74,6 @@ test('locale option', t => { } }); -test('localeOptions option', t => { - t.is(prettyBytes(1900, {localeOptions: {maximumFractionDigits: 1}}), '1.9 kB'); - t.is(prettyBytes(1900, {localeOptions: {minimumFractionDigits: 3}}), '1.900 kB'); - t.is(prettyBytes(1911, {localeOptions: {maximumFractionDigits: 1}}), '1.9 kB'); - t.is(prettyBytes(1111, {localeOptions: {maximumFractionDigits: 2}}), '1.11 kB'); - t.is(prettyBytes(1019, {localeOptions: {maximumFractionDigits: 3}}), '1.019 kB'); - t.is(prettyBytes(1001, {localeOptions: {maximumFractionDigits: 3}}), '1.001 kB'); - t.is(prettyBytes(4001, {localeOptions: {maximumFractionDigits: 3}, binary: true}), '3.907 kiB'); - t.is(prettyBytes(18717, {localeOptions: {maximumFractionDigits: 2}, binary: true}), '18.28 kiB'); - t.is(prettyBytes(18717, {localeOptions: {maximumFractionDigits: 4}, binary: true}), '18.2783 kiB'); -}); - test('signed option', t => { t.is(prettyBytes(42, {signed: true}), '+42 B'); t.is(prettyBytes(-13, {signed: true}), '-13 B'); @@ -125,3 +113,15 @@ test('bits and binary option', t => { t.is(prettyBytes(1025, {bits: true, binary: true}), '1 kibit'); t.is(prettyBytes(1e6, {bits: true, binary: true}), '977 kibit'); }); + +test('fraction digits option', t => { + t.is(prettyBytes(1900, {maximumFractionDigits: 1}), '1.9 kB'); + t.is(prettyBytes(1900, {minimumFractionDigits: 3}), '1.900 kB'); + t.is(prettyBytes(1911, {maximumFractionDigits: 1}), '1.9 kB'); + t.is(prettyBytes(1111, {maximumFractionDigits: 2}), '1.11 kB'); + t.is(prettyBytes(1019, {maximumFractionDigits: 3}), '1.019 kB'); + t.is(prettyBytes(1001, {maximumFractionDigits: 3}), '1.001 kB'); + t.is(prettyBytes(4001, {maximumFractionDigits: 3, binary: true}), '3.907 kiB'); + t.is(prettyBytes(18717, {maximumFractionDigits: 2, binary: true}), '18.28 kiB'); + t.is(prettyBytes(18717, {maximumFractionDigits: 4, binary: true}), '18.2783 kiB'); +}); From 30358d141742e26bf5c308d662f5584132753603 Mon Sep 17 00:00:00 2001 From: Nathan Moinvaziri Date: Thu, 21 Jan 2021 19:42:04 -0800 Subject: [PATCH 04/11] Don't set default value for option with undefined default. --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index ced86f0..661c465 100644 --- a/index.js +++ b/index.js @@ -70,7 +70,7 @@ module.exports = (number, options) => { throw new TypeError(`Expected a finite number, got ${typeof number}: ${number}`); } - options = Object.assign({bits: false, binary: false, minimumFractionDigits: undefined, maximumFractionDigits: undefined}, options); + options = Object.assign({bits: false, binary: false}, options); const UNITS = options.bits ? (options.binary ? BIBIT_UNITS : BIT_UNITS) : From 2265783a4af22854f492b39db8e35589c3232d06 Mon Sep 17 00:00:00 2001 From: Nathan Moinvaziri Date: Thu, 21 Jan 2021 19:49:45 -0800 Subject: [PATCH 05/11] Add default behavior for fraction digit options. --- index.d.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/index.d.ts b/index.d.ts index 710679a..d05cd5f 100644 --- a/index.d.ts +++ b/index.d.ts @@ -53,7 +53,7 @@ declare namespace prettyBytes { readonly binary?: boolean; /** - The minimum number of fraction digits to display. + The minimum number of fraction digits to display. If neither `minimumFractionDigits` or `maximumFractionDigits` are set the default behavior is round to 3 significant digits. @default undefined @@ -68,7 +68,7 @@ declare namespace prettyBytes { /** - The maximum number of fraction digits to display. + The maximum number of fraction digits to display. If neither `minimumFractionDigits` or `maximumFractionDigits` are set the default behavior is round to 3 significant digits. @default undefined From df1c1696d63d3f8caaffd07c79feb5a799498cde Mon Sep 17 00:00:00 2001 From: Nathan Moinvaziri Date: Thu, 21 Jan 2021 19:51:46 -0800 Subject: [PATCH 06/11] Add example of not using fraction digit options. --- index.d.ts | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/index.d.ts b/index.d.ts index d05cd5f..74a8062 100644 --- a/index.d.ts +++ b/index.d.ts @@ -62,6 +62,8 @@ declare namespace prettyBytes { prettyBytes(1900, {minimumFractionDigits: 3}); //=> '1.900 kB' + prettyBytes(1900); + //=> '1.9 kB' ``` */ readonly minimumFractionDigits?: number; @@ -77,6 +79,8 @@ declare namespace prettyBytes { prettyBytes(1900, {maximumFractionDigits: 1}); //=> '1.9 kB' + prettyBytes(1900); + //=> '1.9 kB' ``` */ readonly maximumFractionDigits?: number; From fe9ca4151f8837c9049ded56d54f4b41319796c1 Mon Sep 17 00:00:00 2001 From: Nathan Moinvaziri Date: Thu, 21 Jan 2021 19:56:38 -0800 Subject: [PATCH 07/11] Sync readme and index changes. --- index.d.ts | 8 +++++--- readme.md | 11 ++++++++--- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/index.d.ts b/index.d.ts index 74a8062..6d521b9 100644 --- a/index.d.ts +++ b/index.d.ts @@ -60,6 +60,7 @@ declare namespace prettyBytes { ``` import prettyBytes = require('pretty-bytes'); + // Show number with at least 3 fractional digits prettyBytes(1900, {minimumFractionDigits: 3}); //=> '1.900 kB' prettyBytes(1900); @@ -77,10 +78,11 @@ declare namespace prettyBytes { ``` import prettyBytes = require('pretty-bytes'); - prettyBytes(1900, {maximumFractionDigits: 1}); - //=> '1.9 kB' - prettyBytes(1900); + // Show number with at most 1 fractional digit + prettyBytes(1920, {maximumFractionDigits: 1}); //=> '1.9 kB' + prettyBytes(1920); + //=> '1.92 kB' ``` */ readonly maximumFractionDigits?: number; diff --git a/readme.md b/readme.md index eed0773..f2305c6 100644 --- a/readme.md +++ b/readme.md @@ -90,7 +90,8 @@ Default: `false` *(No localization)* Type: `number`\ Default: `undefined` -The minimum number of fraction digits to display. +The minimum number of fraction digits to display. If neither `minimumFractionDigits` or `maximumFractionDigits` are set the default behavior is round to 3 significant digits. + ```js const prettyBytes = require('pretty-bytes'); @@ -98,6 +99,8 @@ const prettyBytes = require('pretty-bytes'); // Show number with at least 3 fractional digits prettyBytes(1900, {minimumFractionDigits: 3}); //=> '1.900 kB' +prettyBytes(1900); +//=> '1.9 kB' ``` ##### maximumFractionDigits @@ -105,14 +108,16 @@ prettyBytes(1900, {minimumFractionDigits: 3}); Type: `number`\ Default: `undefined` -The maximum number of fraction digits to display. +The maximum number of fraction digits to display. If neither `minimumFractionDigits` or `maximumFractionDigits` are set the default behavior is round to 3 significant digits. ```js const prettyBytes = require('pretty-bytes'); // Show number with at most 1 fractional digit -prettyBytes(1900, {maximumFractionDigits: 1}); +prettyBytes(1920, {maximumFractionDigits: 1}); //=> '1.9 kB' +prettyBytes(1920); +//=> '1.92 kB' ``` ## Related From a245d3f2b23c256f0e574fa4f8154527dbe8cf45 Mon Sep 17 00:00:00 2001 From: Nathan Moinvaziri Date: Thu, 21 Jan 2021 20:01:22 -0800 Subject: [PATCH 08/11] Added more fraction tests. --- test.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/test.js b/test.js index ce6f710..f4511d0 100644 --- a/test.js +++ b/test.js @@ -121,7 +121,11 @@ test('fraction digits option', t => { t.is(prettyBytes(1111, {maximumFractionDigits: 2}), '1.11 kB'); t.is(prettyBytes(1019, {maximumFractionDigits: 3}), '1.019 kB'); t.is(prettyBytes(1001, {maximumFractionDigits: 3}), '1.001 kB'); + t.is(prettyBytes(1000, {minimumFractionDigits: 1, maximumFractionDigits: 3}), '1.0 kB'); + t.is(prettyBytes(3942, {minimumFractionDigits: 1, maximumFractionDigits: 2}), '3.94 kB'); t.is(prettyBytes(4001, {maximumFractionDigits: 3, binary: true}), '3.907 kiB'); t.is(prettyBytes(18717, {maximumFractionDigits: 2, binary: true}), '18.28 kiB'); t.is(prettyBytes(18717, {maximumFractionDigits: 4, binary: true}), '18.2783 kiB'); + t.is(prettyBytes(32768, {minimumFractionDigits: 2, maximumFractionDigits: 3, binary: true}), '32.00 kiB'); + t.is(prettyBytes(65536, {minimumFractionDigits: 1, maximumFractionDigits: 3, binary: true}), '64.0 kiB'); }); From ab8dad7d21a7449efa594b4aecb10f279003957e Mon Sep 17 00:00:00 2001 From: Sindre Sorhus Date: Sun, 21 Feb 2021 20:47:58 +0700 Subject: [PATCH 09/11] Update readme.md --- readme.md | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/readme.md b/readme.md index f2305c6..4c5e305 100644 --- a/readme.md +++ b/readme.md @@ -90,15 +90,17 @@ Default: `false` *(No localization)* Type: `number`\ Default: `undefined` -The minimum number of fraction digits to display. If neither `minimumFractionDigits` or `maximumFractionDigits` are set the default behavior is round to 3 significant digits. +The minimum number of fraction digits to display. +If neither `minimumFractionDigits` or `maximumFractionDigits` are set, the default behavior is to round to 3 significant digits. ```js const prettyBytes = require('pretty-bytes'); -// Show number with at least 3 fractional digits +// Show the number with at least 3 fractional digits prettyBytes(1900, {minimumFractionDigits: 3}); //=> '1.900 kB' + prettyBytes(1900); //=> '1.9 kB' ``` @@ -108,14 +110,17 @@ prettyBytes(1900); Type: `number`\ Default: `undefined` -The maximum number of fraction digits to display. If neither `minimumFractionDigits` or `maximumFractionDigits` are set the default behavior is round to 3 significant digits. +The maximum number of fraction digits to display. + +If neither `minimumFractionDigits` or `maximumFractionDigits` are set, the default behavior is to round to 3 significant digits. ```js const prettyBytes = require('pretty-bytes'); -// Show number with at most 1 fractional digit +// Show the number with at most 1 fractional digit prettyBytes(1920, {maximumFractionDigits: 1}); //=> '1.9 kB' + prettyBytes(1920); //=> '1.92 kB' ``` From c7f55a7b75f32abdf12806605d370fa4fd038d0b Mon Sep 17 00:00:00 2001 From: Sindre Sorhus Date: Sun, 21 Feb 2021 20:48:32 +0700 Subject: [PATCH 10/11] Update test.js --- test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test.js b/test.js index f4511d0..e8b1a8e 100644 --- a/test.js +++ b/test.js @@ -114,7 +114,7 @@ test('bits and binary option', t => { t.is(prettyBytes(1e6, {bits: true, binary: true}), '977 kibit'); }); -test('fraction digits option', t => { +test('fractional digits options', t => { t.is(prettyBytes(1900, {maximumFractionDigits: 1}), '1.9 kB'); t.is(prettyBytes(1900, {minimumFractionDigits: 3}), '1.900 kB'); t.is(prettyBytes(1911, {maximumFractionDigits: 1}), '1.9 kB'); From 5efb81b3ad7834036cce9d350aaf0a481cf05ef0 Mon Sep 17 00:00:00 2001 From: Sindre Sorhus Date: Sun, 21 Feb 2021 20:50:27 +0700 Subject: [PATCH 11/11] Update index.d.ts --- index.d.ts | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/index.d.ts b/index.d.ts index 6d521b9..de264f1 100644 --- a/index.d.ts +++ b/index.d.ts @@ -53,16 +53,19 @@ declare namespace prettyBytes { readonly binary?: boolean; /** - The minimum number of fraction digits to display. If neither `minimumFractionDigits` or `maximumFractionDigits` are set the default behavior is round to 3 significant digits. + The minimum number of fraction digits to display. + + If neither `minimumFractionDigits` or `maximumFractionDigits` are set, the default behavior is to round to 3 significant digits. @default undefined ``` import prettyBytes = require('pretty-bytes'); - // Show number with at least 3 fractional digits + // Show the number with at least 3 fractional digits prettyBytes(1900, {minimumFractionDigits: 3}); //=> '1.900 kB' + prettyBytes(1900); //=> '1.9 kB' ``` @@ -71,16 +74,19 @@ declare namespace prettyBytes { /** - The maximum number of fraction digits to display. If neither `minimumFractionDigits` or `maximumFractionDigits` are set the default behavior is round to 3 significant digits. + The maximum number of fraction digits to display. + + If neither `minimumFractionDigits` or `maximumFractionDigits` are set, the default behavior is to round to 3 significant digits. @default undefined ``` import prettyBytes = require('pretty-bytes'); - // Show number with at most 1 fractional digit + // Show the number with at most 1 fractional digit prettyBytes(1920, {maximumFractionDigits: 1}); //=> '1.9 kB' + prettyBytes(1920); //=> '1.92 kB' ```