Comparing version 2.1.0 to 2.2.0
@@ -0,1 +1,7 @@ | ||
2.2.0 / 2015-11-13 | ||
================== | ||
* add option "decimalPlaces" | ||
* add option "fixedDecimals" | ||
2.1.0 / 2015-05-21 | ||
@@ -2,0 +8,0 @@ ================== |
28
index.js
@@ -38,2 +38,4 @@ /*! | ||
* case: [string], | ||
* decimalPlaces: [number] | ||
* fixedDecimals: [boolean] | ||
* thousandsSeparator: [string] | ||
@@ -65,2 +67,4 @@ * }} [options] bytes options. | ||
* @param {object} [options] | ||
* @param {number} [options.decimalPlaces=2] | ||
* @param {number} [options.fixedDecimals=false] | ||
* @param {string} [options.thousandsSeparator=] | ||
@@ -70,31 +74,35 @@ * @public | ||
function format(val, options) { | ||
if (typeof val !== 'number') { | ||
function format(value, options) { | ||
if (typeof value !== 'number') { | ||
return null; | ||
} | ||
var mag = Math.abs(val); | ||
var mag = Math.abs(value); | ||
var thousandsSeparator = (options && options.thousandsSeparator) || ''; | ||
var decimalPlaces = (options && options.decimalPlaces !== undefined) ? options.decimalPlaces : 2; | ||
var fixedDecimals = Boolean(options && options.fixedDecimals); | ||
var unit = 'B'; | ||
var value = val; | ||
if (mag >= map.tb) { | ||
value = Math.round(value / map.tb * 100) / 100; | ||
unit = 'TB'; | ||
} else if (mag >= map.gb) { | ||
value = Math.round(value / map.gb * 100) / 100; | ||
unit = 'GB'; | ||
} else if (mag >= map.mb) { | ||
value = Math.round(value / map.mb * 100) / 100; | ||
unit = 'MB'; | ||
} else if (mag >= map.kb) { | ||
value = Math.round(value / map.kb * 100) / 100; | ||
unit = 'kB'; | ||
} | ||
var val = value / map[unit.toLowerCase()]; | ||
var str = val.toFixed(decimalPlaces); | ||
if (!fixedDecimals) { | ||
str = str.replace(/(?:\.0*|(\.[^0]+)0+)$/, '$1'); | ||
} | ||
if (thousandsSeparator) { | ||
value = value.toString().replace(/\B(?=(\d{3})+(?!\d))/g, thousandsSeparator); | ||
str = str.replace(/\B(?=(\d{3})+(?!\d))/g, thousandsSeparator); | ||
} | ||
return value + unit; | ||
return str + unit; | ||
} | ||
@@ -101,0 +109,0 @@ |
{ | ||
"name": "bytes", | ||
"description": "Utility to parse a string bytes to bytes and vice-versa", | ||
"version": "2.1.0", | ||
"version": "2.2.0", | ||
"author": "TJ Holowaychuk <tj@vision-media.ca> (http://tjholowaychuk.com)", | ||
"contributors": [ | ||
"Jed Watson <jed.watson@me.com>" | ||
"Jed Watson <jed.watson@me.com>", | ||
"Théo FIDRY <theo.fidry@gmail.com>" | ||
], | ||
@@ -26,3 +27,3 @@ "license": "MIT", | ||
"devDependencies": { | ||
"mocha": "*" | ||
"mocha": "1.21.5" | ||
}, | ||
@@ -29,0 +30,0 @@ "files": [ |
# Bytes utility | ||
[![NPM Version][npm-image]][npm-url] | ||
[![NPM Downloads][downloads-image]][downloads-url] | ||
[![Build Status][travis-image]][travis-url] | ||
Utility to parse a string bytes (ex: `1TB`) to bytes (`1099511627776`) and vice-versa. | ||
@@ -27,2 +31,4 @@ | ||
|-------------------|--------|-----------------------------------------------------------------------------------------| | ||
| decimalPlaces | `number`|`null` | Maximum number of decimal places to include in output. Default value to `2`. | | ||
| fixedDecimals | `boolean`|`null` | Whether to always display the maximum number of decimal places. Default value to `false` | | ||
| thousandsSeparator | `string`|`null` | Example of values: `' '`, `','` and `.`... Default value to `' '`. | | ||
@@ -47,2 +53,5 @@ | ||
// output: '1 000B' | ||
bytes(1024 * 1.7, {decimalPlaces: 0}); | ||
// output: '2kB' | ||
``` | ||
@@ -86,1 +95,8 @@ | ||
[![npm](https://img.shields.io/npm/l/express.svg)](https://github.com/visionmedia/bytes.js/blob/master/LICENSE) | ||
[downloads-image]: https://img.shields.io/npm/dm/bytes.svg | ||
[downloads-url]: https://npmjs.org/package/bytes | ||
[npm-image]: https://img.shields.io/npm/v/bytes.svg | ||
[npm-url]: https://npmjs.org/package/bytes | ||
[travis-image]: https://img.shields.io/travis/visionmedia/bytes.js/master.svg | ||
[travis-url]: https://travis-ci.org/visionmedia/bytes.js |
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
8643
5
118
100