Skip to content

Commit

Permalink
Control number of fraction digits through localeOptions option.
Browse files Browse the repository at this point in the history
  • Loading branch information
nmoinvaz committed Jan 2, 2021
1 parent f972737 commit d75314f
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 46 deletions.
36 changes: 19 additions & 17 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down Expand Up @@ -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;
}
}

Expand Down
12 changes: 4 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,26 +87,22 @@ 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];
}

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 /= 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];

Expand Down
26 changes: 19 additions & 7 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`\
Expand All @@ -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
Expand Down
26 changes: 12 additions & 14 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 kB');
t.is(prettyBytes(18717, {localeOptions: {maximumFractionDigits: 2}, binary: true}), '18.28 kB');
t.is(prettyBytes(18717, {localeOptions: {maximumFractionDigits: 4}, binary: true}), '18.2783 kB');
});

test('signed option', t => {
t.is(prettyBytes(42, {signed: true}), '+42 B');
t.is(prettyBytes(-13, {signed: true}), '-13 B');
Expand Down Expand Up @@ -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');
});

0 comments on commit d75314f

Please sign in to comment.