pretty-bytes
Advanced tools
Comparing version 4.0.2 to 5.0.0
57
index.js
'use strict'; | ||
const UNITS = ['B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']; | ||
module.exports = num => { | ||
if (!Number.isFinite(num)) { | ||
throw new TypeError(`Expected a finite number, got ${typeof num}: ${num}`); | ||
const UNITS = [ | ||
'B', | ||
'kB', | ||
'MB', | ||
'GB', | ||
'TB', | ||
'PB', | ||
'EB', | ||
'ZB', | ||
'YB' | ||
]; | ||
/* | ||
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) => { | ||
let result = number; | ||
if (typeof locale === 'string') { | ||
result = number.toLocaleString(locale); | ||
} else if (locale === true) { | ||
result = number.toLocaleString(); | ||
} | ||
const neg = num < 0; | ||
return result; | ||
}; | ||
if (neg) { | ||
num = -num; | ||
module.exports = (number, options) => { | ||
if (!Number.isFinite(number)) { | ||
throw new TypeError(`Expected a finite number, got ${typeof number}: ${number}`); | ||
} | ||
if (num < 1) { | ||
return (neg ? '-' : '') + num + ' B'; | ||
options = Object.assign({}, options); | ||
const isNegative = number < 0; | ||
if (isNegative) { | ||
number = -number; | ||
} | ||
const exponent = Math.min(Math.floor(Math.log(num) / Math.log(1000)), UNITS.length - 1); | ||
const numStr = Number((num / Math.pow(1000, exponent)).toPrecision(3)); | ||
if (number < 1) { | ||
const numberString = toLocaleString(number, options.locale); | ||
return (isNegative ? '-' : '') + numberString + ' B'; | ||
} | ||
const exponent = Math.min(Math.floor(Math.log10(number) / 3), UNITS.length - 1); | ||
number = Number((number / Math.pow(1000, exponent)).toPrecision(3)); | ||
const numberString = toLocaleString(number, options.locale); | ||
const unit = UNITS[exponent]; | ||
return (neg ? '-' : '') + numStr + ' ' + unit; | ||
return (isNegative ? '-' : '') + numberString + ' ' + unit; | ||
}; |
{ | ||
"name": "pretty-bytes", | ||
"version": "4.0.2", | ||
"description": "Convert bytes to a human readable string: 1337 → 1.34 kB", | ||
"license": "MIT", | ||
"repository": "sindresorhus/pretty-bytes", | ||
"author": { | ||
"name": "Sindre Sorhus", | ||
"email": "sindresorhus@gmail.com", | ||
"url": "sindresorhus.com" | ||
}, | ||
"engines": { | ||
"node": ">=4" | ||
}, | ||
"scripts": { | ||
"test": "xo && ava" | ||
}, | ||
"files": [ | ||
"index.js" | ||
], | ||
"keywords": [ | ||
"pretty", | ||
"bytes", | ||
"byte", | ||
"filesize", | ||
"size", | ||
"file", | ||
"human", | ||
"humanized", | ||
"readable", | ||
"si", | ||
"data" | ||
], | ||
"devDependencies": { | ||
"ava": "*", | ||
"xo": "*" | ||
} | ||
"name": "pretty-bytes", | ||
"version": "5.0.0", | ||
"description": "Convert bytes to a human readable string: 1337 → 1.34 kB", | ||
"license": "MIT", | ||
"repository": "sindresorhus/pretty-bytes", | ||
"author": { | ||
"name": "Sindre Sorhus", | ||
"email": "sindresorhus@gmail.com", | ||
"url": "sindresorhus.com" | ||
}, | ||
"engines": { | ||
"node": ">=6" | ||
}, | ||
"scripts": { | ||
"test": "xo && NODE_ICU_DATA=node_modules/full-icu ava" | ||
}, | ||
"files": [ | ||
"index.js" | ||
], | ||
"keywords": [ | ||
"pretty", | ||
"bytes", | ||
"byte", | ||
"filesize", | ||
"size", | ||
"file", | ||
"human", | ||
"humanized", | ||
"readable", | ||
"si", | ||
"data", | ||
"locale", | ||
"localization", | ||
"localized" | ||
], | ||
"devDependencies": { | ||
"ava": "*", | ||
"full-icu": "^1.2.1", | ||
"xo": "*" | ||
} | ||
} |
@@ -7,12 +7,10 @@ # pretty-bytes [![Build Status](https://travis-ci.org/sindresorhus/pretty-bytes.svg?branch=master)](https://travis-ci.org/sindresorhus/pretty-bytes) | ||
- | ||
*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/)* | ||
*Note that it uses base-10 (e.g. kilobyte). | ||
[Read about the difference between kilobyte and kibibyte.](http://pacoup.com/2009/05/26/kb-kb-kib-whats-up-with-that/)* | ||
## Install | ||
``` | ||
$ npm install --save pretty-bytes | ||
$ npm install pretty-bytes | ||
``` | ||
@@ -31,5 +29,34 @@ | ||
//=> '100 B' | ||
// Localized output using German locale | ||
prettyBytes(1337, {locale: 'de'}); | ||
//=> '1,34 kB' | ||
``` | ||
## API | ||
### prettyBytes(input, [options]) | ||
#### input | ||
Type: `number` | ||
The number to format. | ||
#### options | ||
Type: `Object` | ||
##### locale | ||
Type: `boolean` `string`<br> | ||
Default: `false` *(No localization)* | ||
- 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. | ||
## Related | ||
@@ -36,0 +63,0 @@ |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
4770
46
68
3