update
This commit is contained in:
+64
-3
@@ -11,18 +11,20 @@ declare namespace prettyBytes {
|
||||
- If `false`: Output won't be localized.
|
||||
- If `true`: Localize the output using the system/browser locale.
|
||||
- If `string`: Expects a [BCP 47 language tag](https://en.wikipedia.org/wiki/IETF_language_tag) (For example: `en`, `de`, …)
|
||||
- If `string[]`: Expects a list of [BCP 47 language tags](https://en.wikipedia.org/wiki/IETF_language_tag) (For example: `en`, `de`, …)
|
||||
|
||||
__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.
|
||||
|
||||
@default false
|
||||
*/
|
||||
readonly locale?: boolean | string;
|
||||
readonly locale?: boolean | string | readonly string[];
|
||||
|
||||
/**
|
||||
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
|
||||
|
||||
|
||||
@example
|
||||
```
|
||||
import prettyBytes = require('pretty-bytes');
|
||||
|
||||
@@ -31,6 +33,65 @@ 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 for presenting memory amounts. However, this should not be used for presenting file sizes.
|
||||
|
||||
@default false
|
||||
|
||||
@example
|
||||
```
|
||||
import prettyBytes = require('pretty-bytes');
|
||||
|
||||
prettyBytes(1000, {binary: true});
|
||||
//=> '1000 bit'
|
||||
|
||||
prettyBytes(1024, {binary: true});
|
||||
//=> '1 kiB'
|
||||
```
|
||||
*/
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+54
-12
@@ -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',
|
||||
@@ -24,18 +36,30 @@ const BIT_UNITS = [
|
||||
'Ybit'
|
||||
];
|
||||
|
||||
const BIBIT_UNITS = [
|
||||
'b',
|
||||
'kibit',
|
||||
'Mibit',
|
||||
'Gibit',
|
||||
'Tibit',
|
||||
'Pibit',
|
||||
'Eibit',
|
||||
'Zibit',
|
||||
'Yibit'
|
||||
];
|
||||
|
||||
/*
|
||||
Formats the given number using `Number#toLocaleString`.
|
||||
- If locale is a string, the value is expected to be a locale-key (for example: `de`).
|
||||
- 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') {
|
||||
result = number.toLocaleString(locale);
|
||||
} else if (locale === true) {
|
||||
result = number.toLocaleString();
|
||||
if (typeof locale === 'string' || Array.isArray(locale)) {
|
||||
result = number.toLocaleString(locale, options);
|
||||
} else if (locale === true || options !== undefined) {
|
||||
result = number.toLocaleString(undefined, options);
|
||||
}
|
||||
|
||||
return result;
|
||||
@@ -46,11 +70,14 @@ 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 ?
|
||||
(options.binary ? BIBIT_UNITS : BIT_UNITS) :
|
||||
(options.binary ? BIBYTE_UNITS : BYTE_UNITS);
|
||||
|
||||
if (options.signed && number === 0) {
|
||||
return ' 0 ' + UNITS[0];
|
||||
return ` 0 ${UNITS[0]}`;
|
||||
}
|
||||
|
||||
const isNegative = number < 0;
|
||||
@@ -60,15 +87,30 @@ module.exports = (number, options) => {
|
||||
number = -number;
|
||||
}
|
||||
|
||||
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];
|
||||
}
|
||||
|
||||
const exponent = Math.min(Math.floor(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(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];
|
||||
|
||||
|
||||
+1
-1
@@ -1,6 +1,6 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (sindresorhus.com)
|
||||
Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (https://sindresorhus.com)
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
||||
|
||||
|
||||
+15
-15
@@ -1,36 +1,36 @@
|
||||
{
|
||||
"_args": [
|
||||
[
|
||||
"pretty-bytes@5.3.0",
|
||||
"/mnt/Foxconn/Digitalent/Deverloper/liff-push_2series"
|
||||
"pretty-bytes@5.6.0",
|
||||
"/home/node/nuxt"
|
||||
]
|
||||
],
|
||||
"_from": "pretty-bytes@5.3.0",
|
||||
"_id": "pretty-bytes@5.3.0",
|
||||
"_from": "pretty-bytes@5.6.0",
|
||||
"_id": "pretty-bytes@5.6.0",
|
||||
"_inBundle": false,
|
||||
"_integrity": "sha512-hjGrh+P926p4R4WbaB6OckyRtO0F0/lQBiT+0gnxjV+5kjPBrfVBFCsCLbMqVQeydvIoouYTCmmEURiH3R1Bdg==",
|
||||
"_integrity": "sha512-FFw039TmrBqFK8ma/7OL3sDz/VytdtJr044/QUJtH0wK9lb9jLq9tJyIxUwtQJHwar2BqtiA4iCWSwo9JLkzFg==",
|
||||
"_location": "/pretty-bytes",
|
||||
"_phantomChildren": {},
|
||||
"_requested": {
|
||||
"type": "version",
|
||||
"registry": true,
|
||||
"raw": "pretty-bytes@5.3.0",
|
||||
"raw": "pretty-bytes@5.6.0",
|
||||
"name": "pretty-bytes",
|
||||
"escapedName": "pretty-bytes",
|
||||
"rawSpec": "5.3.0",
|
||||
"rawSpec": "5.6.0",
|
||||
"saveSpec": null,
|
||||
"fetchSpec": "5.3.0"
|
||||
"fetchSpec": "5.6.0"
|
||||
},
|
||||
"_requiredBy": [
|
||||
"/@nuxt/cli"
|
||||
],
|
||||
"_resolved": "https://registry.npmjs.org/pretty-bytes/-/pretty-bytes-5.3.0.tgz",
|
||||
"_spec": "5.3.0",
|
||||
"_where": "/mnt/Foxconn/Digitalent/Deverloper/liff-push_2series",
|
||||
"_resolved": "https://registry.npmjs.org/pretty-bytes/-/pretty-bytes-5.6.0.tgz",
|
||||
"_spec": "5.6.0",
|
||||
"_where": "/home/node/nuxt",
|
||||
"author": {
|
||||
"name": "Sindre Sorhus",
|
||||
"email": "sindresorhus@gmail.com",
|
||||
"url": "sindresorhus.com"
|
||||
"url": "https://sindresorhus.com"
|
||||
},
|
||||
"bugs": {
|
||||
"url": "https://github.com/sindresorhus/pretty-bytes/issues"
|
||||
@@ -38,7 +38,6 @@
|
||||
"description": "Convert bytes to a human readable string: 1337 → 1.34 kB",
|
||||
"devDependencies": {
|
||||
"ava": "^1.4.1",
|
||||
"full-icu": "^1.2.1",
|
||||
"tsd": "^0.7.2",
|
||||
"xo": "^0.24.0"
|
||||
},
|
||||
@@ -49,6 +48,7 @@
|
||||
"index.js",
|
||||
"index.d.ts"
|
||||
],
|
||||
"funding": "https://github.com/sponsors/sindresorhus",
|
||||
"homepage": "https://github.com/sindresorhus/pretty-bytes#readme",
|
||||
"keywords": [
|
||||
"pretty",
|
||||
@@ -73,7 +73,7 @@
|
||||
"url": "git+https://github.com/sindresorhus/pretty-bytes.git"
|
||||
},
|
||||
"scripts": {
|
||||
"test": "xo && NODE_ICU_DATA=node_modules/full-icu ava && tsd"
|
||||
"test": "xo && ava && tsd"
|
||||
},
|
||||
"version": "5.3.0"
|
||||
"version": "5.6.0"
|
||||
}
|
||||
|
||||
+56
-14
@@ -1,4 +1,4 @@
|
||||
# pretty-bytes [](https://travis-ci.org/sindresorhus/pretty-bytes)
|
||||
# pretty-bytes
|
||||
|
||||
> Convert bytes to a human readable string: `1337` → `1.34 kB`
|
||||
|
||||
@@ -7,14 +7,12 @@ Useful for displaying file sizes for humans.
|
||||
*Note that it uses base-10 (e.g. kilobyte).
|
||||
[Read about the difference between kilobyte and kibibyte.](https://web.archive.org/web/20150324153922/https://pacoup.com/2009/05/26/kb-kb-kib-whats-up-with-that/)*
|
||||
|
||||
|
||||
## Install
|
||||
|
||||
```
|
||||
$ npm install pretty-bytes
|
||||
```
|
||||
|
||||
|
||||
## Usage
|
||||
|
||||
```js
|
||||
@@ -39,10 +37,9 @@ prettyBytes(1337, {locale: 'de'});
|
||||
//=> '1,34 kB'
|
||||
```
|
||||
|
||||
|
||||
## API
|
||||
|
||||
### prettyBytes(number, [options])
|
||||
### prettyBytes(number, options?)
|
||||
|
||||
#### number
|
||||
|
||||
@@ -56,34 +53,79 @@ Type: `object`
|
||||
|
||||
##### signed
|
||||
|
||||
Type: `boolean`<br>
|
||||
Type: `boolean`\
|
||||
Default: `false`
|
||||
|
||||
Include plus sign for positive numbers. If the difference is exactly zero a space character will be prepended instead for better alignment.
|
||||
|
||||
##### bits
|
||||
|
||||
Type: `boolean`<br>
|
||||
Type: `boolean`\
|
||||
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`<br>
|
||||
Type: `boolean | string`\
|
||||
Default: `false` *(No localization)*
|
||||
|
||||
**Important:** Only the number and decimal separator are localized. The unit title is not and will not be localized.
|
||||
|
||||
- If `true`: Localize the output using the system/browser locale.
|
||||
- If `string`: Expects a [BCP 47 language tag](https://en.wikipedia.org/wiki/IETF_language_tag) (For example: `en`, `de`, …)
|
||||
- If `string[]`: Expects a list of [BCP 47 language tags](https://en.wikipedia.org/wiki/IETF_language_tag) (For example: `en`, `de`, …)
|
||||
|
||||
**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.
|
||||
**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.
|
||||
|
||||
##### 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
|
||||
|
||||
- [pretty-bytes-cli](https://github.com/sindresorhus/pretty-bytes-cli) - CLI for this module
|
||||
|
||||
|
||||
## License
|
||||
|
||||
MIT © [Sindre Sorhus](https://sindresorhus.com)
|
||||
- [pretty-ms](https://github.com/sindresorhus/pretty-ms) - Convert milliseconds to a human readable string
|
||||
|
||||
Reference in New Issue
Block a user