Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
73a7955
commit ca9d8e0
Showing
10 changed files
with
112 additions
and
101 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
* text=auto | ||
*.js text eol=lf |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
node_modules | ||
yarn.lock |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
package-lock=false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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": "*" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters