Skip to content

Commit

Permalink
Require Node.js 6
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed May 6, 2018
1 parent 73a7955 commit ca9d8e0
Show file tree
Hide file tree
Showing 10 changed files with 112 additions and 101 deletions.
2 changes: 1 addition & 1 deletion .editorconfig
Expand Up @@ -7,6 +7,6 @@ charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[{package.json,*.yml}]
[*.yml]
indent_style = space
indent_size = 2
1 change: 1 addition & 0 deletions .gitattributes
@@ -1 +1,2 @@
* text=auto
*.js text eol=lf
1 change: 1 addition & 0 deletions .gitignore
@@ -1 +1,2 @@
node_modules
yarn.lock
1 change: 1 addition & 0 deletions .npmrc
@@ -0,0 +1 @@
package-lock=false
8 changes: 2 additions & 6 deletions .travis.yml
@@ -1,9 +1,5 @@
sudo: false
before_install:
- npm install full-icu
before_script:
- export NODE_ICU_DATA=node_modules/full-icu
language: node_js
node_js:
- '10'
- '8'
- '6'
- '4'
73 changes: 42 additions & 31 deletions index.js
@@ -1,46 +1,57 @@
'use strict';
const UNITS = ['B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];

module.exports = (num, options) => {
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();
}

return result;
};

module.exports = (number, options) => {
if (!Number.isFinite(number)) {
throw new TypeError(`Expected a finite number, got ${typeof number}: ${number}`);
}

options = Object.assign({}, options);

const neg = num < 0;
const isNegative = number < 0;

if (neg) {
num = -num;
if (isNegative) {
number = -number;
}

if (num < 1) {
const numStr = toLocaleString(num, options.locale);
return (neg ? '-' : '') + numStr + ' B';
if (number < 1) {
const numberString = toLocaleString(number, options.locale);
return (isNegative ? '-' : '') + numberString + ' B';
}

const exponent = Math.min(Math.floor(Math.log10(num) / 3), UNITS.length - 1);
num = Number((num / Math.pow(1000, exponent)).toPrecision(3));
const numStr = toLocaleString(num, options.locale);
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;
};

/**
* Formats the given number using number.toLocaleString(..).
* If locale is a string, the value is expected to be a locale-key (e.g. '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.
*/
function toLocaleString(num, locale) {
let result = num;
if (typeof locale === 'string') {
result = num.toLocaleString(locale);
} else if (locale === true) {
result = num.toLocaleString();
}

return result;
}
20 changes: 4 additions & 16 deletions license
@@ -1,21 +1,9 @@
The MIT License (MIT)
MIT License

Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (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:
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:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
76 changes: 40 additions & 36 deletions package.json
@@ -1,38 +1,42 @@
{
"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": "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": ">=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": "*"
}
}
27 changes: 18 additions & 9 deletions readme.md
Expand Up @@ -4,14 +4,14 @@
Useful for displaying file sizes for humans.

*Note that it uses base-10 (e.g. kilobyte).
*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 --save pretty-bytes
$ npm install pretty-bytes
```


Expand All @@ -26,27 +26,36 @@ prettyBytes(1337);
prettyBytes(100);
//=> '100 B'

// Localize output using german locale
prettyBytes(1337, { locale: 'de' });
// 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`
Default: `false` / no localization

- `string`: Expects a [BCP 47 language tag](https://en.wikipedia.org/wiki/IETF_language_tag) (e.g. `en`, `de`, ...)
- `boolean`: If `true`: Localize the output using the system/browser 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.

**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

Expand Down
4 changes: 2 additions & 2 deletions test.js
@@ -1,5 +1,5 @@
import test from 'ava';
import m from './';
import m from '.';

test('throws on invalid input', t => {
t.throws(() => m(''));
Expand Down Expand Up @@ -32,7 +32,7 @@ test('supports negative number', t => {
t.is(m(-1001), '-1 kB');
});

test('localized output', t => {
test('locale option', t => {
t.is(m(-0.4, {locale: 'de'}), '-0,4 B');
t.is(m(0.4, {locale: 'de'}), '0,4 B');
t.is(m(1001, {locale: 'de'}), '1 kB');
Expand Down

0 comments on commit ca9d8e0

Please sign in to comment.