bytes
Advanced tools
Comparing version 2.4.0 to 2.5.0
@@ -0,1 +1,6 @@ | ||
2.5.0 / 2017-03-24 | ||
================== | ||
* Add option "unit" | ||
2.4.0 / 2016-06-01 | ||
@@ -2,0 +7,0 @@ ================== |
23
index.js
@@ -79,2 +79,3 @@ /*! | ||
* @param {string} [options.thousandsSeparator=] | ||
* @param {string} [options.unit=] | ||
* @param {string} [options.unitSeparator=] | ||
@@ -96,12 +97,16 @@ * | ||
var fixedDecimals = Boolean(options && options.fixedDecimals); | ||
var unit = 'B'; | ||
var unit = (options && options.unit) || ''; | ||
if (mag >= map.tb) { | ||
unit = 'TB'; | ||
} else if (mag >= map.gb) { | ||
unit = 'GB'; | ||
} else if (mag >= map.mb) { | ||
unit = 'MB'; | ||
} else if (mag >= map.kb) { | ||
unit = 'kB'; | ||
if (!unit || !map[unit.toLowerCase()]) { | ||
if (mag >= map.tb) { | ||
unit = 'TB'; | ||
} else if (mag >= map.gb) { | ||
unit = 'GB'; | ||
} else if (mag >= map.mb) { | ||
unit = 'MB'; | ||
} else if (mag >= map.kb) { | ||
unit = 'kB'; | ||
} else { | ||
unit = 'B'; | ||
} | ||
} | ||
@@ -108,0 +113,0 @@ |
{ | ||
"name": "bytes", | ||
"description": "Utility to parse a string bytes to bytes and vice-versa", | ||
"version": "2.4.0", | ||
"version": "2.5.0", | ||
"author": "TJ Holowaychuk <tj@vision-media.ca> (http://tjholowaychuk.com)", | ||
@@ -27,3 +27,4 @@ "contributors": [ | ||
"devDependencies": { | ||
"mocha": "1.21.5" | ||
"mocha": "1.21.5", | ||
"nyc": "10.1.2" | ||
}, | ||
@@ -36,5 +37,10 @@ "files": [ | ||
], | ||
"engines": { | ||
"node": ">= 0.6" | ||
}, | ||
"scripts": { | ||
"test": "mocha --check-leaks --reporter spec" | ||
"test": "mocha --check-leaks --reporter spec", | ||
"test-ci": "nyc --reporter=text npm test", | ||
"test-cov": "nyc --reporter=html --reporter=text npm test" | ||
} | ||
} |
@@ -6,2 +6,3 @@ # Bytes utility | ||
[![Build Status][travis-image]][travis-url] | ||
[![Test Coverage][coveralls-image]][coveralls-url] | ||
@@ -16,3 +17,3 @@ Utility to parse a string bytes (ex: `1TB`) to bytes (`1099511627776`) and vice-versa. | ||
#### bytes.format(number value, [options]): string|null | ||
#### bytes.format(number value, [options]): string|null | ||
@@ -33,6 +34,7 @@ Format the given value in bytes into a string. If the value is negative, it is kept as such. If it is a float, it is | ||
|-------------------|--------|-----------------------------------------------------------------------------------------| | ||
| 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 `' '`. | | ||
| unitSeparator | `string`|`null` | Separator to use between number and unit. Default value to `''`. | | ||
| 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 `''`. | | ||
| unit | `string`|`null` | The unit in which the result will be returned (B/kB/MB/GB/TB). Default value to `''` (which means auto detect). | | ||
| unitSeparator | `string`|`null` | Separator to use between number and unit. Default value to `''`. | | ||
@@ -43,3 +45,3 @@ **Returns** | ||
|---------|-------------|-------------------------| | ||
| results | `string`|`null` | Return null upon error. String value otherwise. | | ||
| results | `string`|`null` | Return null upon error. String value otherwise. | | ||
@@ -66,13 +68,14 @@ **Example** | ||
#### bytes.parse(string value): number|null | ||
#### bytes.parse(string|number value): number|null | ||
Parse the string value into an integer in bytes. If no unit is given, it is assumed the value is in bytes. | ||
Parse the string value into an integer in bytes. If no unit is given, or `value` | ||
is a number, it is assumed the value is in bytes. | ||
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 | ||
* `b` for bytes | ||
* `kb` for kilobytes | ||
* `mb` for megabytes | ||
* `gb` for gigabytes | ||
* `tb` for terabytes | ||
@@ -85,3 +88,3 @@ The units are in powers of two, not ten. This means 1kb = 1024b according to this parser. | ||
|---------------|--------|--------------------| | ||
| value | `string` | String to parse. | | ||
| value | `string`|`number` | String to parse, or number in bytes. | | ||
@@ -92,3 +95,3 @@ **Returns** | ||
|---------|-------------|-------------------------| | ||
| results | `number`|`null` | Return null upon error. Value in bytes otherwise. | | ||
| results | `number`|`null` | Return null upon error. Value in bytes otherwise. | | ||
@@ -103,2 +106,5 @@ **Example** | ||
// output: 1024 | ||
bytes(1024); | ||
// output: 1024 | ||
``` | ||
@@ -115,3 +121,3 @@ | ||
[![npm](https://img.shields.io/npm/l/express.svg)](https://github.com/visionmedia/bytes.js/blob/master/LICENSE) | ||
[MIT](LICENSE) | ||
@@ -124,1 +130,3 @@ [downloads-image]: https://img.shields.io/npm/dm/bytes.svg | ||
[travis-url]: https://travis-ci.org/visionmedia/bytes.js | ||
[coveralls-image]: https://img.shields.io/coveralls/visionmedia/bytes.js/master.svg | ||
[coveralls-url]: https://coveralls.io/r/visionmedia/bytes.js?branch=master |
10600
135
123
2