pretty-bytes
Advanced tools
Comparing version 5.5.0 to 5.6.0
@@ -38,3 +38,3 @@ declare namespace prettyBytes { | ||
/** | ||
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. | ||
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. | ||
@@ -55,2 +55,43 @@ @default false | ||
readonly binary?: boolean; | ||
/** | ||
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 the number with at least 3 fractional digits | ||
prettyBytes(1900, {minimumFractionDigits: 3}); | ||
//=> '1.900 kB' | ||
prettyBytes(1900); | ||
//=> '1.9 kB' | ||
``` | ||
*/ | ||
readonly minimumFractionDigits?: number; | ||
/** | ||
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 the number with at most 1 fractional digit | ||
prettyBytes(1920, {maximumFractionDigits: 1}); | ||
//=> '1.9 kB' | ||
prettyBytes(1920); | ||
//=> '1.92 kB' | ||
``` | ||
*/ | ||
readonly maximumFractionDigits?: number; | ||
} | ||
@@ -57,0 +98,0 @@ } |
29
index.js
@@ -57,8 +57,8 @@ 'use strict'; | ||
*/ | ||
const toLocaleString = (number, locale) => { | ||
const toLocaleString = (number, locale, options) => { | ||
let result = number; | ||
if (typeof locale === 'string' || Array.isArray(locale)) { | ||
result = number.toLocaleString(locale); | ||
} else if (locale === true) { | ||
result = number.toLocaleString(); | ||
result = number.toLocaleString(locale, options); | ||
} else if (locale === true || options !== undefined) { | ||
result = number.toLocaleString(undefined, options); | ||
} | ||
@@ -91,4 +91,14 @@ | ||
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, options.locale); | ||
const numberString = toLocaleString(number, options.locale, localeOptions); | ||
return prefix + numberString + ' ' + UNITS[0]; | ||
@@ -99,5 +109,10 @@ } | ||
// 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 (!localeOptions) { | ||
number = number.toPrecision(3); | ||
} | ||
const numberString = toLocaleString(Number(number), options.locale, localeOptions); | ||
const unit = UNITS[exponent]; | ||
@@ -104,0 +119,0 @@ |
{ | ||
"name": "pretty-bytes", | ||
"version": "5.5.0", | ||
"version": "5.6.0", | ||
"description": "Convert bytes to a human readable string: 1337 → 1.34 kB", | ||
@@ -5,0 +5,0 @@ "license": "MIT", |
@@ -73,3 +73,3 @@ # pretty-bytes | ||
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. | ||
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. | ||
@@ -89,2 +89,42 @@ ##### locale | ||
##### minimumFractionDigits | ||
Type: `number`\ | ||
Default: `undefined` | ||
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 the number with at least 3 fractional digits | ||
prettyBytes(1900, {minimumFractionDigits: 3}); | ||
//=> '1.900 kB' | ||
prettyBytes(1900); | ||
//=> '1.9 kB' | ||
``` | ||
##### maximumFractionDigits | ||
Type: `number`\ | ||
Default: `undefined` | ||
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 the number with at most 1 fractional digit | ||
prettyBytes(1920, {maximumFractionDigits: 1}); | ||
//=> '1.9 kB' | ||
prettyBytes(1920); | ||
//=> '1.92 kB' | ||
``` | ||
## Related | ||
@@ -91,0 +131,0 @@ |
11544
191
132