Comparing version 2.2.0 to 2.3.0
@@ -0,1 +1,9 @@ | ||
2.3.0 / 2016-02-15 | ||
================== | ||
* Drop partial bytes on all parsed units | ||
* Fix non-finite numbers to `.format` to return `null` | ||
* Fix parsing byte string that looks like hex | ||
* perf: hoist regular expressions | ||
2.2.0 / 2015-11-13 | ||
@@ -2,0 +10,0 @@ ================== |
27
index.js
@@ -24,2 +24,6 @@ /*! | ||
var formatThousandsRegExp = /\B(?=(\d{3})+(?!\d))/g; | ||
var formatDecimalsRegExp = /(?:\.0*|(\.[^0]+)0+)$/; | ||
var map = { | ||
@@ -33,4 +37,9 @@ b: 1, | ||
// TODO: use is-finite module? | ||
var numberIsFinite = Number.isFinite || function (v) { return typeof v === 'number' && isFinite(v); }; | ||
var parseRegExp = /^((-|\+)?(\d+(?:\.\d+)?)) *(kb|mb|gb|tb)$/i; | ||
/** | ||
*Convert the given value in bytes into a string or parse to string to an integer in bytes. | ||
* Convert the given value in bytes into a string or parse to string to an integer in bytes. | ||
* | ||
@@ -71,2 +80,4 @@ * @param {string|number} value | ||
* @param {string} [options.thousandsSeparator=] | ||
* | ||
* @returns {string|null} | ||
* @public | ||
@@ -76,3 +87,3 @@ */ | ||
function format(value, options) { | ||
if (typeof value !== 'number') { | ||
if (!numberIsFinite(value)) { | ||
return null; | ||
@@ -101,7 +112,7 @@ } | ||
if (!fixedDecimals) { | ||
str = str.replace(/(?:\.0*|(\.[^0]+)0+)$/, '$1'); | ||
str = str.replace(formatDecimalsRegExp, '$1'); | ||
} | ||
if (thousandsSeparator) { | ||
str = str.replace(/\B(?=(\d{3})+(?!\d))/g, thousandsSeparator); | ||
str = str.replace(formatThousandsRegExp, thousandsSeparator); | ||
} | ||
@@ -118,2 +129,4 @@ | ||
* @param {number|string} val | ||
* | ||
* @returns {number|null} | ||
* @public | ||
@@ -132,3 +145,3 @@ */ | ||
// Test if the string passed is valid | ||
var results = val.match(/^((-|\+)?(\d+(?:\.\d+)?)) *(kb|mb|gb|tb)$/i); | ||
var results = parseRegExp.exec(val); | ||
var floatValue; | ||
@@ -139,3 +152,3 @@ var unit = 'b'; | ||
// Nothing could be extracted from the given string | ||
floatValue = parseInt(val); | ||
floatValue = parseInt(val, 10); | ||
unit = 'b' | ||
@@ -148,3 +161,3 @@ } else { | ||
return map[unit] * floatValue; | ||
return Math.floor(map[unit] * floatValue); | ||
} |
{ | ||
"name": "bytes", | ||
"description": "Utility to parse a string bytes to bytes and vice-versa", | ||
"version": "2.2.0", | ||
"version": "2.3.0", | ||
"author": "TJ Holowaychuk <tj@vision-media.ca> (http://tjholowaychuk.com)", | ||
@@ -6,0 +6,0 @@ "contributors": [ |
@@ -61,2 +61,12 @@ # Bytes utility | ||
Supported units and abbreviations are as follows and are case-insensitive: | ||
* "b" for bytes | ||
* "kb" for kilobytes | ||
* "mb" for megabytes | ||
* "gb" for gigabytes | ||
* "tb" for terabytes | ||
The units are in powers of two, not ten. This means 1kb = 1024b according to this parser. | ||
**Arguments** | ||
@@ -63,0 +73,0 @@ |
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
9485
127
110