pretty-bytes
Advanced tools
Comparing version 5.6.0 to 6.0.0
145
index.d.ts
@@ -1,96 +0,91 @@ | ||
declare namespace prettyBytes { | ||
interface Options { | ||
/** | ||
Include plus sign for positive numbers. If the difference is exactly zero a space character will be prepended instead for better alignment. | ||
export interface Options { | ||
/** | ||
Include plus sign for positive numbers. If the difference is exactly zero a space character will be prepended instead for better alignment. | ||
@default false | ||
*/ | ||
readonly signed?: boolean; | ||
@default false | ||
*/ | ||
readonly signed?: boolean; | ||
/** | ||
- 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`, …) | ||
/** | ||
- 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 string[]; | ||
@default false | ||
*/ | ||
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). | ||
/** | ||
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 | ||
@default false | ||
@example | ||
``` | ||
import prettyBytes from 'pretty-bytes'; | ||
@example | ||
``` | ||
import prettyBytes = require('pretty-bytes'); | ||
prettyBytes(1337, {bits: true}); | ||
//=> '1.34 kbit' | ||
``` | ||
*/ | ||
readonly bits?: boolean; | ||
prettyBytes(1337, {bits: true}); | ||
//=> '1.34 kbit' | ||
``` | ||
*/ | ||
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. | ||
/** | ||
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 | ||
@default false | ||
@example | ||
``` | ||
import prettyBytes from 'pretty-bytes'; | ||
@example | ||
``` | ||
import prettyBytes = require('pretty-bytes'); | ||
prettyBytes(1000, {binary: true}); | ||
//=> '1000 bit' | ||
prettyBytes(1000, {binary: true}); | ||
//=> '1000 bit' | ||
prettyBytes(1024, {binary: true}); | ||
//=> '1 kiB' | ||
``` | ||
*/ | ||
readonly binary?: boolean; | ||
prettyBytes(1024, {binary: true}); | ||
//=> '1 kiB' | ||
``` | ||
*/ | ||
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 to round to 3 significant digits. | ||
If neither `minimumFractionDigits` or `maximumFractionDigits` are set, the default behavior is to round to 3 significant digits. | ||
@default undefined | ||
@default undefined | ||
``` | ||
import prettyBytes from 'pretty-bytes'; | ||
``` | ||
import prettyBytes = require('pretty-bytes'); | ||
// Show the number with at least 3 fractional digits | ||
prettyBytes(1900, {minimumFractionDigits: 3}); | ||
//=> '1.900 kB' | ||
// Show the number with at least 3 fractional digits | ||
prettyBytes(1900, {minimumFractionDigits: 3}); | ||
//=> '1.900 kB' | ||
prettyBytes(1900); | ||
//=> '1.9 kB' | ||
``` | ||
*/ | ||
readonly minimumFractionDigits?: number; | ||
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. | ||
/** | ||
The maximum number of fraction digits to display. | ||
@default undefined | ||
If neither `minimumFractionDigits` or `maximumFractionDigits` are set, the default behavior is to round to 3 significant digits. | ||
``` | ||
import prettyBytes from 'pretty-bytes'; | ||
@default undefined | ||
// Show the number with at most 1 fractional digit | ||
prettyBytes(1920, {maximumFractionDigits: 1}); | ||
//=> '1.9 kB' | ||
``` | ||
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; | ||
} | ||
prettyBytes(1920); | ||
//=> '1.92 kB' | ||
``` | ||
*/ | ||
readonly maximumFractionDigits?: number; | ||
} | ||
@@ -105,3 +100,3 @@ | ||
``` | ||
import prettyBytes = require('pretty-bytes'); | ||
import prettyBytes from 'pretty-bytes'; | ||
@@ -123,7 +118,5 @@ prettyBytes(1337); | ||
*/ | ||
declare function prettyBytes( | ||
export default function prettyBytes( | ||
number: number, | ||
options?: prettyBytes.Options | ||
options?: Options | ||
): string; | ||
export = prettyBytes; |
31
index.js
@@ -1,3 +0,1 @@ | ||
'use strict'; | ||
const BYTE_UNITS = [ | ||
@@ -12,3 +10,3 @@ 'B', | ||
'ZB', | ||
'YB' | ||
'YB', | ||
]; | ||
@@ -25,3 +23,3 @@ | ||
'ZiB', | ||
'YiB' | ||
'YiB', | ||
]; | ||
@@ -38,3 +36,3 @@ | ||
'Zbit', | ||
'Ybit' | ||
'Ybit', | ||
]; | ||
@@ -51,3 +49,3 @@ | ||
'Zibit', | ||
'Yibit' | ||
'Yibit', | ||
]; | ||
@@ -72,3 +70,3 @@ | ||
module.exports = (number, options) => { | ||
export default function prettyBytes(number, options) { | ||
if (!Number.isFinite(number)) { | ||
@@ -78,7 +76,11 @@ throw new TypeError(`Expected a finite number, got ${typeof number}: ${number}`); | ||
options = Object.assign({bits: false, binary: false}, options); | ||
options = { | ||
bits: false, | ||
binary: false, | ||
...options, | ||
}; | ||
const UNITS = options.bits ? | ||
(options.binary ? BIBIT_UNITS : BIT_UNITS) : | ||
(options.binary ? BIBYTE_UNITS : BYTE_UNITS); | ||
const UNITS = options.bits | ||
? (options.binary ? BIBIT_UNITS : BIT_UNITS) | ||
: (options.binary ? BIBYTE_UNITS : BYTE_UNITS); | ||
@@ -103,3 +105,3 @@ if (options.signed && number === 0) { | ||
if (options.maximumFractionDigits !== undefined) { | ||
localeOptions = Object.assign({maximumFractionDigits: options.maximumFractionDigits}, localeOptions); | ||
localeOptions = {maximumFractionDigits: options.maximumFractionDigits, ...localeOptions}; | ||
} | ||
@@ -113,4 +115,3 @@ | ||
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); | ||
number /= (options.binary ? 1024 : 1000) ** exponent; | ||
@@ -126,2 +127,2 @@ if (!localeOptions) { | ||
return prefix + numberString + ' ' + unit; | ||
}; | ||
} |
{ | ||
"name": "pretty-bytes", | ||
"version": "5.6.0", | ||
"version": "6.0.0", | ||
"description": "Convert bytes to a human readable string: 1337 → 1.34 kB", | ||
@@ -13,4 +13,7 @@ "license": "MIT", | ||
}, | ||
"type": "module", | ||
"exports": "./index.js", | ||
"types": "./index.d.ts", | ||
"engines": { | ||
"node": ">=6" | ||
"node": "^14.13.1 || >=16.0.0" | ||
}, | ||
@@ -41,6 +44,6 @@ "scripts": { | ||
"devDependencies": { | ||
"ava": "^1.4.1", | ||
"tsd": "^0.7.2", | ||
"xo": "^0.24.0" | ||
"ava": "^4.0.1", | ||
"tsd": "^0.19.1", | ||
"xo": "^0.48.0" | ||
} | ||
} |
@@ -12,5 +12,5 @@ # pretty-bytes | ||
```sh | ||
npm install pretty-bytes | ||
``` | ||
$ npm install pretty-bytes | ||
``` | ||
@@ -20,3 +20,3 @@ ## Usage | ||
```js | ||
const prettyBytes = require('pretty-bytes'); | ||
import prettyBytes from 'pretty-bytes'; | ||
@@ -88,4 +88,2 @@ prettyBytes(1337); | ||
**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 | ||
@@ -101,3 +99,3 @@ | ||
```js | ||
const prettyBytes = require('pretty-bytes'); | ||
import prettyBytes from 'pretty-bytes'; | ||
@@ -122,3 +120,3 @@ // Show the number with at least 3 fractional digits | ||
```js | ||
const prettyBytes = require('pretty-bytes'); | ||
import prettyBytes from 'pretty-bytes'; | ||
@@ -125,0 +123,0 @@ // Show the number with at most 1 fractional digit |
Yes
10705
188
130