[Pkg-javascript-commits] [node-bytes] 01/05: New upstream version 2.4.0

Paolo Greppi paolog-guest at moszumanska.debian.org
Fri Dec 23 13:33:46 UTC 2016


This is an automated email from the git hooks/post-receive script.

paolog-guest pushed a commit to branch master
in repository node-bytes.

commit 624b9f0511fb4985e8ca488a62a78ec030c541a5
Author: Paolo Greppi <paolo.greppi at libpf.com>
Date:   Fri Dec 23 13:22:06 2016 +0000

    New upstream version 2.4.0
---
 .travis.yml         | 20 ++++++++++++++++++++
 History.md          | 19 +++++++++++++++++++
 Readme.md           | 31 +++++++++++++++++++++++++++++++
 component.json      |  2 +-
 index.js            | 52 ++++++++++++++++++++++++++++++++++++++--------------
 package.json        |  7 ++++---
 test/byte-format.js | 25 +++++++++++++++++++++++++
 test/byte-parse.js  |  6 ++++++
 8 files changed, 144 insertions(+), 18 deletions(-)

diff --git a/.travis.yml b/.travis.yml
new file mode 100644
index 0000000..064f369
--- /dev/null
+++ b/.travis.yml
@@ -0,0 +1,20 @@
+language: node_js
+node_js:
+  - "0.6"
+  - "0.8"
+  - "0.10"
+  - "0.12"
+  - "1.8"
+  - "2.4"
+  - "3.3"
+  - "4.4"
+  - "5.10"
+sudo: false
+cache:
+  directories:
+    - node_modules
+before_install:
+  # Update Node.js modules
+  - "test ! -d node_modules || npm prune"
+  - "test ! -d node_modules || npm rebuild"
+script: "npm test"
diff --git a/History.md b/History.md
index 578d84f..56932a4 100644
--- a/History.md
+++ b/History.md
@@ -1,3 +1,22 @@
+2.4.0 / 2016-06-01
+==================
+
+  * Add option "unitSeparator"
+
+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
+==================
+
+  * add option "decimalPlaces"
+  * add option "fixedDecimals"
+
 2.1.0 / 2015-05-21
 ==================
 
diff --git a/Readme.md b/Readme.md
index 8f15dec..7465fde 100644
--- a/Readme.md
+++ b/Readme.md
@@ -1,5 +1,9 @@
 # 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.
 
 ## Usage
@@ -24,7 +28,10 @@ Format the given value in bytes into a string. If the value is negative, it is k
 
 | Property          | Type   | Description                                                                             |
 |-------------------|--------|-----------------------------------------------------------------------------------------|
+| 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 `''`. |
 
 **Returns**
 
@@ -43,12 +50,29 @@ bytes(1000);
 
 bytes(1000, {thousandsSeparator: ' '});
 // output: '1 000B'
+
+bytes(1024 * 1.7, {decimalPlaces: 0});
+// output: '2kB'
+
+bytes(1024, {unitSeparator: ' '});
+// output: '1 kB'
+
 ```
 
 #### bytes.parse(string 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.
 
+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**
 
 | Name          | Type   | Description        |
@@ -81,3 +105,10 @@ component install visionmedia/bytes.js
 ## License 
 
 [![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
diff --git a/component.json b/component.json
index 38c3484..8f4eaff 100644
--- a/component.json
+++ b/component.json
@@ -6,7 +6,7 @@
     "bytes",
     "utility"
   ],
-  "version": "2.1.0",
+  "version": "2.4.0",
   "scripts": ["index.js"],
   "license": "MIT"
 }
diff --git a/index.js b/index.js
index dc4df2e..aa24231 100644
--- a/index.js
+++ b/index.js
@@ -21,6 +21,10 @@ module.exports.parse = parse;
  * @private
  */
 
+var formatThousandsRegExp = /\B(?=(\d{3})+(?!\d))/g;
+
+var formatDecimalsRegExp = /(?:\.0*|(\.[^0]+)0+)$/;
+
 var map = {
   b:  1,
   kb: 1 << 10,
@@ -29,13 +33,21 @@ var map = {
   tb: ((1 << 30) * 1024)
 };
 
+// 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.
  *
  * @param {string|number} value
  * @param {{
  *  case: [string],
+ *  decimalPlaces: [number]
+ *  fixedDecimals: [boolean]
  *  thousandsSeparator: [string]
+ *  unitSeparator: [string]
  *  }} [options] bytes options.
  *
  * @returns {string|number|null}
@@ -61,39 +73,49 @@ function bytes(value, options) {
  *
  * @param {number} value
  * @param {object} [options]
+ * @param {number} [options.decimalPlaces=2]
+ * @param {number} [options.fixedDecimals=false]
  * @param {string} [options.thousandsSeparator=]
+ * @param {string} [options.unitSeparator=]
+ *
+ * @returns {string|null}
  * @public
  */
 
-function format(val, options) {
-  if (typeof val !== 'number') {
+function format(value, options) {
+  if (!numberIsFinite(value)) {
     return null;
   }
 
-  var mag = Math.abs(val);
+  var mag = Math.abs(value);
   var thousandsSeparator = (options && options.thousandsSeparator) || '';
+  var unitSeparator = (options && options.unitSeparator) || '';
+  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(formatDecimalsRegExp, '$1');
+  }
+
   if (thousandsSeparator) {
-    value = value.toString().replace(/\B(?=(\d{3})+(?!\d))/g, thousandsSeparator);
+    str = str.replace(formatThousandsRegExp, thousandsSeparator);
   }
 
-  return value + unit;
+  return str + unitSeparator + unit;
 }
 
 /**
@@ -102,6 +124,8 @@ function format(val, options) {
  * If no unit is given, it is assumed the value is in bytes.
  *
  * @param {number|string} val
+ *
+ * @returns {number|null}
  * @public
  */
 
@@ -115,13 +139,13 @@ function parse(val) {
   }
 
   // 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;
   var unit = 'b';
 
   if (!results) {
     // Nothing could be extracted from the given string
-    floatValue = parseInt(val);
+    floatValue = parseInt(val, 10);
     unit = 'b'
   } else {
     // Retrieve the value and the unit
@@ -129,5 +153,5 @@ function parse(val) {
     unit = results[4].toLowerCase();
   }
 
-  return map[unit] * floatValue;
+  return Math.floor(map[unit] * floatValue);
 }
diff --git a/package.json b/package.json
index a0d0354..fc289ba 100644
--- a/package.json
+++ b/package.json
@@ -1,10 +1,11 @@
 {
   "name": "bytes",
   "description": "Utility to parse a string bytes to bytes and vice-versa",
-  "version": "2.1.0",
+  "version": "2.4.0",
   "author": "TJ Holowaychuk <tj at vision-media.ca> (http://tjholowaychuk.com)",
   "contributors": [
-    "Jed Watson <jed.watson at me.com>"
+    "Jed Watson <jed.watson at me.com>",
+    "Théo FIDRY <theo.fidry at gmail.com>"
   ],
   "license": "MIT",
   "keywords": [
@@ -23,7 +24,7 @@
     }
   },
   "devDependencies": {
-    "mocha": "*"
+    "mocha": "1.21.5"
   },
   "files": [
     "History.md",
diff --git a/test/byte-format.js b/test/byte-format.js
index b4406ed..0914a01 100644
--- a/test/byte-format.js
+++ b/test/byte-format.js
@@ -14,6 +14,8 @@ describe('Test byte format function', function(){
     assert.strictEqual(bytes.format(null), null);
     assert.strictEqual(bytes.format(true), null);
     assert.strictEqual(bytes.format(false), null);
+    assert.strictEqual(bytes.format(NaN), null);
+    assert.strictEqual(bytes.format(Infinity), null);
     assert.strictEqual(bytes.format(''), null);
     assert.strictEqual(bytes.format('string'), null);
     assert.strictEqual(bytes.format(function(){}), null);
@@ -67,6 +69,29 @@ describe('Test byte format function', function(){
     assert.equal(bytes.format(1000, {thousandsSeparator: ' '}).toLowerCase(), '1 000b');
   });
 
+  it('Should custom unit separator', function(){
+    assert.equal(bytes.format(1024), '1kB');
+    assert.equal(bytes.format(1024, {unitSeparator: ''}), '1kB');
+    assert.equal(bytes.format(1024, {unitSeparator: null}), '1kB');
+    assert.equal(bytes.format(1024, {unitSeparator: ' '}), '1 kB');
+    assert.equal(bytes.format(1024, {unitSeparator: '\t'}), '1\tkB');
+  });
+
+  it('Should support custom number of decimal places', function(){
+    assert.equal(bytes.format(kb - 1, {decimalPlaces: 0}).toLowerCase(), '1023b');
+    assert.equal(bytes.format(kb, {decimalPlaces: 0}).toLowerCase(), '1kb');
+    assert.equal(bytes.format(1.4 * kb, {decimalPlaces: 0}).toLowerCase(), '1kb');
+    assert.equal(bytes.format(1.5 * kb, {decimalPlaces: 0}).toLowerCase(), '2kb');
+    assert.equal(bytes.format(kb - 1, {decimalPlaces: 1}).toLowerCase(), '1023b');
+    assert.equal(bytes.format(kb, {decimalPlaces: 1}).toLowerCase(), '1kb');
+    assert.equal(bytes.format(1.04 * kb, {decimalPlaces: 1}).toLowerCase(), '1kb');
+    assert.equal(bytes.format(1.05 * kb, {decimalPlaces: 1}).toLowerCase(), '1.1kb');
+  });
+
+  it('Should support fixed decimal places', function(){
+    assert.equal(bytes.format(kb, {decimalPlaces: 3, fixedDecimals: true}).toLowerCase(), '1.000kb');
+  });
+
   it('Should support floats', function(){
     assert.equal(bytes.format(1.2 * mb).toLowerCase(), '1.2mb');
     assert.equal(bytes.format(-1.2 * mb).toLowerCase(), '-1.2mb');
diff --git a/test/byte-parse.js b/test/byte-parse.js
index 926136a..7fa8272 100644
--- a/test/byte-parse.js
+++ b/test/byte-parse.js
@@ -73,6 +73,7 @@ describe('Test byte parse function', function(){
     assert.equal(bytes.parse('0'), 0);
     assert.equal(bytes.parse('-1'), -1);
     assert.equal(bytes.parse('1024'), 1024);
+    assert.equal(bytes.parse('0x11'), 0);
   });
 
   it('Should accept negative values', function(){
@@ -81,6 +82,11 @@ describe('Test byte parse function', function(){
     assert.equal(bytes.parse('-1.5TB'), -1.5 * Math.pow(1024, 4));
   });
 
+  it('Should drop partial bytes', function(){
+    assert.equal(bytes.parse('1.1b'), 1);
+    assert.equal(bytes.parse('1.0001kb'), 1024);
+  });
+
   it('Should allow whitespace', function(){
     assert.equal(bytes.parse('1 TB'), 1 * Math.pow(1024, 4));
   });

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-bytes.git



More information about the Pkg-javascript-commits mailing list