pretty-bytes
Advanced tools
Comparing version 5.3.0 to 5.4.0
@@ -23,5 +23,5 @@ declare namespace prettyBytes { | ||
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 | ||
``` | ||
@@ -35,2 +35,19 @@ import prettyBytes = require('pretty-bytes'); | ||
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 | ||
``` | ||
import prettyBytes = require('pretty-bytes'); | ||
prettyBytes(1000, {binary: true}); | ||
//=> '1000 bit' | ||
prettyBytes(1024, {binary: true}); | ||
//=> '1 kiB' | ||
``` | ||
*/ | ||
readonly binary?: boolean; | ||
} | ||
@@ -37,0 +54,0 @@ } |
20
index.js
@@ -15,2 +15,14 @@ 'use strict'; | ||
const BIBYTE_UNITS = [ | ||
'B', | ||
'kiB', | ||
'MiB', | ||
'GiB', | ||
'TiB', | ||
'PiB', | ||
'EiB', | ||
'ZiB', | ||
'YiB' | ||
]; | ||
const BIT_UNITS = [ | ||
@@ -50,4 +62,4 @@ 'b', | ||
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 ? BIBYTE_UNITS : BIT_UNITS) : BYTE_UNITS; | ||
@@ -70,5 +82,5 @@ if (options.signed && number === 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)); | ||
number = Number((number / Math.pow(options.binary ? 1024 : 1000, exponent)).toPrecision(3)); | ||
const numberString = toLocaleString(number, options.locale); | ||
@@ -75,0 +87,0 @@ |
{ | ||
"name": "pretty-bytes", | ||
"version": "5.3.0", | ||
"version": "5.4.0", | ||
"description": "Convert bytes to a human readable string: 1337 → 1.34 kB", | ||
"license": "MIT", | ||
"repository": "sindresorhus/pretty-bytes", | ||
"funding": "https://github.com/sponsors/sindresorhus", | ||
"author": { | ||
"name": "Sindre Sorhus", | ||
"email": "sindresorhus@gmail.com", | ||
"url": "sindresorhus.com" | ||
"url": "https://sindresorhus.com" | ||
}, | ||
@@ -16,3 +17,3 @@ "engines": { | ||
"scripts": { | ||
"test": "xo && NODE_ICU_DATA=node_modules/full-icu ava && tsd" | ||
"test": "xo && ava && tsd" | ||
}, | ||
@@ -41,3 +42,2 @@ "files": [ | ||
"ava": "^1.4.1", | ||
"full-icu": "^1.2.1", | ||
"tsd": "^0.7.2", | ||
@@ -44,0 +44,0 @@ "xo": "^0.24.0" |
@@ -1,2 +0,2 @@ | ||
# pretty-bytes [![Build Status](https://travis-ci.org/sindresorhus/pretty-bytes.svg?branch=master)](https://travis-ci.org/sindresorhus/pretty-bytes) | ||
# pretty-bytes [![Build Status](https://travis-ci.com/sindresorhus/pretty-bytes.svg?branch=master)](https://travis-ci.com/github/sindresorhus/pretty-bytes) | ||
@@ -10,3 +10,2 @@ > Convert bytes to a human readable string: `1337` → `1.34 kB` | ||
## Install | ||
@@ -18,3 +17,2 @@ | ||
## Usage | ||
@@ -44,6 +42,5 @@ | ||
## API | ||
### prettyBytes(number, [options]) | ||
### prettyBytes(number, options?) | ||
@@ -62,3 +59,3 @@ #### number | ||
Type: `boolean`<br> | ||
Type: `boolean`\ | ||
Default: `false` | ||
@@ -70,3 +67,3 @@ | ||
Type: `boolean`<br> | ||
Type: `boolean`\ | ||
Default: `false` | ||
@@ -76,20 +73,24 @@ | ||
##### 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`, …) | ||
**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. | ||
## 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 |
Sorry, the diff of this file is not supported yet
8881
3
137
91