pretty-bytes
Advanced tools
Comparing version 3.0.1 to 4.0.0
21
index.js
'use strict'; | ||
var numberIsNan = require('number-is-nan'); | ||
const UNITS = ['B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']; | ||
module.exports = function (num) { | ||
if (typeof num !== 'number' || numberIsNan(num)) { | ||
throw new TypeError('Expected a number, got ' + typeof num); | ||
module.exports = num => { | ||
if (typeof num !== 'number' || Number.isNaN(num)) { | ||
throw new TypeError(`Expected a number, got ${typeof num}`); | ||
} | ||
var exponent; | ||
var unit; | ||
var neg = num < 0; | ||
var units = ['B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']; | ||
const neg = num < 0; | ||
@@ -22,7 +19,7 @@ if (neg) { | ||
exponent = Math.min(Math.floor(Math.log(num) / Math.log(1000)), units.length - 1); | ||
num = Number((num / Math.pow(1000, exponent)).toFixed(2)); | ||
unit = units[exponent]; | ||
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)); | ||
const unit = UNITS[exponent]; | ||
return (neg ? '-' : '') + num + ' ' + unit; | ||
return (neg ? '-' : '') + numStr + ' ' + unit; | ||
}; |
{ | ||
"name": "pretty-bytes", | ||
"version": "3.0.1", | ||
"version": "4.0.0", | ||
"description": "Convert bytes to a human readable string: 1337 → 1.34 kB", | ||
@@ -13,3 +13,3 @@ "license": "MIT", | ||
"engines": { | ||
"node": ">=0.10.0" | ||
"node": ">=4" | ||
}, | ||
@@ -35,5 +35,2 @@ "scripts": { | ||
], | ||
"dependencies": { | ||
"number-is-nan": "^1.0.0" | ||
}, | ||
"devDependencies": { | ||
@@ -40,0 +37,0 @@ "ava": "*", |
@@ -9,3 +9,3 @@ # 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 (eg. kilobyte). | ||
*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/)* | ||
@@ -41,2 +41,2 @@ | ||
MIT © [Sindre Sorhus](http://sindresorhus.com) | ||
MIT © [Sindre Sorhus](https://sindresorhus.com) |
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
0
3151
18
- Removednumber-is-nan@^1.0.0
- Removednumber-is-nan@1.0.1(transitive)