[Pkg-javascript-commits] [node-bytes] 08/14: Imported Upstream version 2.1.0

Paolo Greppi paolog-guest at moszumanska.debian.org
Fri Dec 23 13:20:50 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 49fdd69b576a9f573cad9193ac318a8442f535dd
Author: Jérémy Lal <kapouer at melix.org>
Date:   Mon Oct 26 18:21:47 2015 +0100

    Imported Upstream version 2.1.0
---
 .gitignore           | 185 +++++++++++++++++++++++++++++++++++++++++++++++++++
 .npmignore           |   1 -
 History.md           |  32 ++++++++-
 Readme.md => LICENSE |  35 +---------
 Makefile             |   7 --
 Readme.md            | 103 +++++++++++++++++-----------
 component.json       |  13 ++--
 index.js             | 148 +++++++++++++++++++++++++++++++++--------
 package.json         |  40 +++++++----
 test/byte-format.js  |  75 +++++++++++++++++++++
 test/byte-parse.js   |  87 ++++++++++++++++++++++++
 test/bytes.js        |  43 ++++++------
 test/tostring.js     |  43 ------------
 13 files changed, 621 insertions(+), 191 deletions(-)

diff --git a/.gitignore b/.gitignore
index 3c3629e..57473c4 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1 +1,186 @@
+# Created by https://www.gitignore.io
+
+### Node ###
+# Logs
+logs
+*.log
+
+# Runtime data
+pids
+*.pid
+*.seed
+
+# Directory for instrumented libs generated by jscoverage/JSCover
+lib-cov
+
+# Coverage directory used by tools like istanbul
+coverage
+
+# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
+.grunt
+
+# node-waf configuration
+.lock-wscript
+
+# Compiled binary addons (http://nodejs.org/api/addons.html)
+build/Release
+
+# Dependency directory
+# https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git
 node_modules
+
+### Linux ###
+*~
+
+# KDE directory preferences
+.directory
+
+# Linux trash folder which might appear on any partition or disk
+.Trash-*
+
+### OSX ###
+.DS_Store
+.AppleDouble
+.LSOverride
+
+# Icon must end with two \r
+Icon
+
+
+# Thumbnails
+._*
+
+# Files that might appear in the root of a volume
+.DocumentRevisions-V100
+.fseventsd
+.Spotlight-V100
+.TemporaryItems
+.Trashes
+.VolumeIcon.icns
+
+# Directories potentially created on remote AFP share
+.AppleDB
+.AppleDesktop
+Network Trash Folder
+Temporary Items
+.apdisk
+
+### Intellij ###
+# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm
+
+*.iml
+
+## Directory-based project format:
+.idea/
+# if you remove the above rule, at least ignore the following:
+
+# User-specific stuff:
+# .idea/workspace.xml
+# .idea/tasks.xml
+# .idea/dictionaries
+
+# Sensitive or high-churn files:
+# .idea/dataSources.ids
+# .idea/dataSources.xml
+# .idea/sqlDataSources.xml
+# .idea/dynamic.xml
+# .idea/uiDesigner.xml
+
+# Gradle:
+# .idea/gradle.xml
+# .idea/libraries
+
+# Mongo Explorer plugin:
+# .idea/mongoSettings.xml
+
+## File-based project format:
+*.ipr
+*.iws
+
+## Plugin-specific files:
+
+# IntelliJ
+out/
+
+# mpeltonen/sbt-idea plugin
+.idea_modules/
+
+# JIRA plugin
+atlassian-ide-plugin.xml
+
+# Crashlytics plugin (for Android Studio and IntelliJ)
+com_crashlytics_export_strings.xml
+crashlytics.properties
+crashlytics-build.properties
+
+
+### NetBeans ###
+nbproject/private/
+build/
+nbbuild/
+dist/
+nbdist/
+nbactions.xml
+nb-configuration.xml
+.nb-gradle/
+
+
+### Eclipse ###
+*.pydevproject
+.metadata
+.gradle
+bin/
+tmp/
+*.tmp
+*.bak
+*.swp
+*~.nib
+local.properties
+.settings/
+.loadpath
+
+# Eclipse Core
+.project
+
+# External tool builders
+.externalToolBuilders/
+
+# Locally stored "Eclipse launch configurations"
+*.launch
+
+# CDT-specific
+.cproject
+
+# JDT-specific (Eclipse Java Development Tools)
+.classpath
+
+# PDT-specific
+.buildpath
+
+# sbteclipse plugin
+.target
+
+# TeXlipse plugin
+.texlipse
+
+
+### Windows ###
+# Windows image file caches
+Thumbs.db
+ehthumbs.db
+
+# Folder config file
+Desktop.ini
+
+# Recycle Bin used on file shares
+$RECYCLE.BIN/
+
+# Windows Installer files
+*.cab
+*.msi
+*.msm
+*.msp
+
+# Windows shortcuts
+*.lnk
+
diff --git a/.npmignore b/.npmignore
deleted file mode 100644
index 9daeafb..0000000
--- a/.npmignore
+++ /dev/null
@@ -1 +0,0 @@
-test
diff --git a/History.md b/History.md
index 5097352..578d84f 100644
--- a/History.md
+++ b/History.md
@@ -1,3 +1,29 @@
+2.1.0 / 2015-05-21
+==================
+
+  * add `.format` export
+  * add `.parse` export
+
+2.0.2 / 2015-05-20
+==================
+
+  * remove map recreation
+  * remove unnecessary object construction
+
+2.0.1 / 2015-05-07
+==================
+
+  * fix browserify require
+  * remove node.extend dependency
+
+2.0.0 / 2015-04-12
+==================
+
+  * add option "case"
+  * add option "thousandsSeparator"
+  * return "null" on invalid parse input
+  * support proper round-trip: bytes(bytes(num)) === num
+  * units no longer case sensitive when parsing
 
 1.0.0 / 2014-05-05
 ==================
@@ -9,17 +35,17 @@
 
  * added terabyte support
 
-0.2.1 / 2013-04-01 
+0.2.1 / 2013-04-01
 ==================
 
   * add .component
 
-0.2.0 / 2012-10-28 
+0.2.0 / 2012-10-28
 ==================
 
   * bytes(200).should.eql('200b')
 
-0.1.0 / 2012-07-04 
+0.1.0 / 2012-07-04
 ==================
 
   * add bytes to string conversion [yields]
diff --git a/Readme.md b/LICENSE
similarity index 73%
copy from Readme.md
copy to LICENSE
index 5591b28..63e95a9 100644
--- a/Readme.md
+++ b/LICENSE
@@ -1,38 +1,7 @@
-# node-bytes
-
-  Byte string parser / formatter.
-
-## Example:
-
-```js
-bytes('1kb')
-// => 1024
-
-bytes('2mb')
-// => 2097152
-
-bytes('1gb')
-// => 1073741824
-
-bytes(1073741824)
-// => 1gb
-
-bytes(1099511627776)
-// => 1tb
-```
-
-## Installation
-
-```
-$ npm install bytes
-$ component install visionmedia/bytes.js
-```
-
-## License 
-
 (The MIT License)
 
-Copyright (c) 2012 TJ Holowaychuk <tj at vision-media.ca>
+Copyright (c) 2012-2014 TJ Holowaychuk <tj at vision-media.ca>
+Copyright (c) 2015 Jed Watson <jed.watson at me.com>
 
 Permission is hereby granted, free of charge, to any person obtaining
 a copy of this software and associated documentation files (the
diff --git a/Makefile b/Makefile
deleted file mode 100644
index 8e8640f..0000000
--- a/Makefile
+++ /dev/null
@@ -1,7 +0,0 @@
-
-test:
-	@./node_modules/.bin/mocha \
-		--reporter spec \
-		--require should
-
-.PHONY: test
\ No newline at end of file
diff --git a/Readme.md b/Readme.md
index 5591b28..8f15dec 100644
--- a/Readme.md
+++ b/Readme.md
@@ -1,54 +1,83 @@
-# node-bytes
+# Bytes utility
 
-  Byte string parser / formatter.
+Utility to parse a string bytes (ex: `1TB`) to bytes (`1099511627776`) and vice-versa.
 
-## Example:
+## Usage
 
 ```js
-bytes('1kb')
-// => 1024
+var bytes = require('bytes');
+```
 
-bytes('2mb')
-// => 2097152
+#### bytes.format(number value, [options]): string|null
 
-bytes('1gb')
-// => 1073741824
+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
+ rounded.
 
-bytes(1073741824)
-// => 1gb
+**Arguments**
 
-bytes(1099511627776)
-// => 1tb
-```
+| Name    | Type   | Description        |
+|---------|--------|--------------------|
+| value   | `number` | Value in bytes     |
+| options | `Object` | Conversion options |
 
-## Installation
+**Options**
 
-```
-$ npm install bytes
-$ component install visionmedia/bytes.js
+| Property          | Type   | Description                                                                             |
+|-------------------|--------|-----------------------------------------------------------------------------------------|
+| thousandsSeparator | `string`|`null` | Example of values: `' '`, `','` and `.`... Default value to `' '`. |
+
+**Returns**
+
+| Name    | Type        | Description             |
+|---------|-------------|-------------------------|
+| results | `string`|`null` | Return null upon error. String value otherwise. |
+
+**Example**
+
+```js
+bytes(1024);
+// output: '1kB'
+
+bytes(1000);
+// output: '1000B'
+
+bytes(1000, {thousandsSeparator: ' '});
+// output: '1 000B'
 ```
 
-## License 
+#### bytes.parse(string value): number|null
 
-(The MIT License)
+Parse the string value into an integer in bytes. If no unit is given, it is assumed the value is in bytes.
 
-Copyright (c) 2012 TJ Holowaychuk <tj at vision-media.ca>
+**Arguments**
 
-Permission is hereby granted, free of charge, to any person obtaining
-a copy of this software and associated documentation files (the
-'Software'), to deal in the Software without restriction, including
-without limitation the rights to use, copy, modify, merge, publish,
-distribute, sublicense, and/or sell copies of the Software, and to
-permit persons to whom the Software is furnished to do so, subject to
-the following conditions:
+| Name          | Type   | Description        |
+|---------------|--------|--------------------|
+| value   | `string` | String to parse.   |
 
-The above copyright notice and this permission notice shall be
-included in all copies or substantial portions of the Software.
+**Returns**
+
+| Name    | Type        | Description             |
+|---------|-------------|-------------------------|
+| results | `number`|`null` | Return null upon error. Value in bytes otherwise. |
+
+**Example**
+
+```js
+bytes('1kB');
+// output: 1024
+
+bytes('1024');
+// output: 1024
+```
+
+## Installation
+
+```bash
+npm install bytes --save
+component install visionmedia/bytes.js
+```
+
+## License 
 
-THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
-EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
-MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
-IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
-CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
-TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
-SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+[![npm](https://img.shields.io/npm/l/express.svg)](https://github.com/visionmedia/bytes.js/blob/master/LICENSE)
diff --git a/component.json b/component.json
index 2929c25..38c3484 100644
--- a/component.json
+++ b/component.json
@@ -1,7 +1,12 @@
 {
   "name": "bytes",
-  "description": "byte size string parser / serializer",
-  "keywords": ["bytes", "utility"],
-  "version": "0.2.1",
-  "scripts": ["index.js"]
+  "description": "Byte size string parser / serializer.",
+  "repository": "visionmedia/bytes.js.git",
+  "keywords": [
+    "bytes",
+    "utility"
+  ],
+  "version": "2.1.0",
+  "scripts": ["index.js"],
+  "license": "MIT"
 }
diff --git a/index.js b/index.js
index c1da2fe..dc4df2e 100644
--- a/index.js
+++ b/index.js
@@ -1,41 +1,133 @@
+/*!
+ * bytes
+ * Copyright(c) 2012-2014 TJ Holowaychuk
+ * Copyright(c) 2015 Jed Watson
+ * MIT Licensed
+ */
+
+'use strict';
 
 /**
- * Parse byte `size` string.
- *
- * @param {String} size
- * @return {Number}
- * @api public
+ * Module exports.
+ * @public
  */
 
-module.exports = function(size) {
-  if ('number' == typeof size) return convert(size);
-  var parts = size.match(/^(\d+(?:\.\d+)?) *(kb|mb|gb|tb)$/)
-    , n = parseFloat(parts[1])
-    , type = parts[2];
+module.exports = bytes;
+module.exports.format = format;
+module.exports.parse = parse;
 
-  var map = {
-      kb: 1 << 10
-    , mb: 1 << 20
-    , gb: 1 << 30
-    , tb: ((1 << 30) * 1024)
-  };
+/**
+ * Module variables.
+ * @private
+ */
 
-  return map[type] * n;
+var map = {
+  b:  1,
+  kb: 1 << 10,
+  mb: 1 << 20,
+  gb: 1 << 30,
+  tb: ((1 << 30) * 1024)
 };
 
 /**
- * convert bytes into string.
+ *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],
+ *  thousandsSeparator: [string]
+ *  }} [options] bytes options.
+ *
+ * @returns {string|number|null}
+ */
+
+function bytes(value, options) {
+  if (typeof value === 'string') {
+    return parse(value);
+  }
+
+  if (typeof value === 'number') {
+    return format(value, options);
+  }
+
+  return null;
+}
+
+/**
+ * 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 rounded.
+ *
+ * @param {number} value
+ * @param {object} [options]
+ * @param {string} [options.thousandsSeparator=]
+ * @public
+ */
+
+function format(val, options) {
+  if (typeof val !== 'number') {
+    return null;
+  }
+
+  var mag = Math.abs(val);
+  var thousandsSeparator = (options && options.thousandsSeparator) || '';
+  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';
+  }
+
+  if (thousandsSeparator) {
+    value = value.toString().replace(/\B(?=(\d{3})+(?!\d))/g, thousandsSeparator);
+  }
+
+  return value + unit;
+}
+
+/**
+ * Parse the string value into an integer in bytes.
  *
- * @param {Number} b - bytes to convert
- * @return {String}
- * @api public
+ * If no unit is given, it is assumed the value is in bytes.
+ *
+ * @param {number|string} val
+ * @public
  */
 
-function convert (b) {
-  var tb = ((1 << 30) * 1024), gb = 1 << 30, mb = 1 << 20, kb = 1 << 10, abs = Math.abs(b);
-  if (abs >= tb) return (Math.round(b / tb * 100) / 100) + 'tb';
-  if (abs >= gb) return (Math.round(b / gb * 100) / 100) + 'gb';
-  if (abs >= mb) return (Math.round(b / mb * 100) / 100) + 'mb';
-  if (abs >= kb) return (Math.round(b / kb * 100) / 100) + 'kb';
-  return b + 'b';
+function parse(val) {
+  if (typeof val === 'number' && !isNaN(val)) {
+    return val;
+  }
+
+  if (typeof val !== 'string') {
+    return null;
+  }
+
+  // Test if the string passed is valid
+  var results = val.match(/^((-|\+)?(\d+(?:\.\d+)?)) *(kb|mb|gb|tb)$/i);
+  var floatValue;
+  var unit = 'b';
+
+  if (!results) {
+    // Nothing could be extracted from the given string
+    floatValue = parseInt(val);
+    unit = 'b'
+  } else {
+    // Retrieve the value and the unit
+    floatValue = parseFloat(results[1]);
+    unit = results[4].toLowerCase();
+  }
+
+  return map[unit] * floatValue;
 }
diff --git a/package.json b/package.json
index 76e0bff..a0d0354 100644
--- a/package.json
+++ b/package.json
@@ -1,21 +1,37 @@
 {
   "name": "bytes",
+  "description": "Utility to parse a string bytes to bytes and vice-versa",
+  "version": "2.1.0",
   "author": "TJ Holowaychuk <tj at vision-media.ca> (http://tjholowaychuk.com)",
-  "description": "byte size string parser / serializer",
-  "repository": {
-    "type": "git",
-    "url": "https://github.com/visionmedia/bytes.js.git"
-  },
-  "version": "1.0.0",
-  "main": "index.js",
-  "dependencies": {},
-  "devDependencies": {
-    "mocha": "*",
-    "should": "*"
-  },
+  "contributors": [
+    "Jed Watson <jed.watson at me.com>"
+  ],
+  "license": "MIT",
+  "keywords": [
+    "byte",
+    "bytes",
+    "utility",
+    "parse",
+    "parser",
+    "convert",
+    "converter"
+  ],
+  "repository": "visionmedia/bytes.js",
   "component": {
     "scripts": {
       "bytes/index.js": "index.js"
     }
+  },
+  "devDependencies": {
+    "mocha": "*"
+  },
+  "files": [
+    "History.md",
+    "LICENSE",
+    "Readme.md",
+    "index.js"
+  ],
+  "scripts": {
+    "test": "mocha --check-leaks --reporter spec"
   }
 }
diff --git a/test/byte-format.js b/test/byte-format.js
new file mode 100644
index 0000000..b4406ed
--- /dev/null
+++ b/test/byte-format.js
@@ -0,0 +1,75 @@
+'use strict';
+
+var assert = require('assert');
+var bytes = require('..');
+
+describe('Test byte format function', function(){
+  var tb = (1 << 30) * 1024,
+      gb = 1 << 30,
+      mb = 1 << 20,
+      kb = 1 << 10;
+
+  it('Should return null if input invalid', function(){
+    assert.strictEqual(bytes.format(undefined), null);
+    assert.strictEqual(bytes.format(null), null);
+    assert.strictEqual(bytes.format(true), null);
+    assert.strictEqual(bytes.format(false), null);
+    assert.strictEqual(bytes.format(''), null);
+    assert.strictEqual(bytes.format('string'), null);
+    assert.strictEqual(bytes.format(function(){}), null);
+    assert.strictEqual(bytes.format({}), null);
+  });
+
+  it('Should convert numbers < 1024 to `bytes` string', function(){
+    assert.equal(bytes.format(0).toLowerCase(), '0b');
+    assert.equal(bytes.format(100).toLowerCase(), '100b');
+    assert.equal(bytes.format(-100).toLowerCase(), '-100b');
+  });
+
+  it('Should convert numbers >= 1 024 to kb string', function(){
+    assert.equal(bytes.format(kb).toLowerCase(), '1kb');
+    assert.equal(bytes.format(-kb).toLowerCase(), '-1kb');
+    assert.equal(bytes.format(2 * kb).toLowerCase(), '2kb');
+  });
+
+  it('Should convert numbers >= 1 048 576 to mb string', function(){
+    assert.equal(bytes.format(mb).toLowerCase(), '1mb');
+    assert.equal(bytes.format(-mb).toLowerCase(), '-1mb');
+    assert.equal(bytes.format(2 * mb).toLowerCase(), '2mb');
+  });
+
+  it('Should convert numbers >= (1 << 30) to gb string', function(){
+    assert.equal(bytes.format(gb).toLowerCase(), '1gb');
+    assert.equal(bytes.format(-gb).toLowerCase(), '-1gb');
+    assert.equal(bytes.format(2 * gb).toLowerCase(), '2gb');
+  });
+
+  it('Should convert numbers >= ((1 << 30) * 1024) to tb string', function(){
+    assert.equal(bytes.format(tb).toLowerCase(), '1tb');
+    assert.equal(bytes.format(-tb).toLowerCase(), '-1tb');
+    assert.equal(bytes.format(2 * tb).toLowerCase(), '2tb');
+  });
+
+  it('Should return standard case', function(){
+    assert.equal(bytes.format(10), '10B');
+    assert.equal(bytes.format(kb), '1kB');
+    assert.equal(bytes.format(mb), '1MB');
+    assert.equal(bytes.format(gb), '1GB');
+    assert.equal(bytes.format(tb), '1TB');
+  });
+
+  it('Support custom thousands separator', function(){
+    assert.equal(bytes.format(1000).toLowerCase(), '1000b');
+    assert.equal(bytes.format(1000, {thousandsSeparator: ''}).toLowerCase(), '1000b');
+    assert.equal(bytes.format(1000, {thousandsSeparator: null}).toLowerCase(), '1000b');
+    assert.equal(bytes.format(1000, {thousandsSeparator: '.'}).toLowerCase(), '1.000b');
+    assert.equal(bytes.format(1000, {thousandsSeparator: ','}).toLowerCase(), '1,000b');
+    assert.equal(bytes.format(1000, {thousandsSeparator: ' '}).toLowerCase(), '1 000b');
+  });
+
+  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');
+    assert.equal(bytes.format(1.2 * kb).toLowerCase(), '1.2kb');
+  })
+});
diff --git a/test/byte-parse.js b/test/byte-parse.js
new file mode 100644
index 0000000..926136a
--- /dev/null
+++ b/test/byte-parse.js
@@ -0,0 +1,87 @@
+'use strict';
+
+var assert = require('assert');
+var bytes = require('..');
+
+describe('Test byte parse function', function(){
+  it('Should return null if input invalid', function(){
+    assert.strictEqual(bytes.parse(undefined), null);
+    assert.strictEqual(bytes.parse(null), null);
+    assert.strictEqual(bytes.parse(true), null);
+    assert.strictEqual(bytes.parse(false), null);
+    assert.strictEqual(bytes.parse(NaN), null);
+    assert.strictEqual(bytes.parse(function(){}), null);
+    assert.strictEqual(bytes.parse({}), null);
+  });
+
+  it('Should parse raw number', function(){
+    assert.strictEqual(bytes.parse(0), 0);
+    assert.strictEqual(bytes.parse(-1), -1);
+    assert.strictEqual(bytes.parse(1), 1);
+    assert.strictEqual(bytes.parse(10.5), 10.5);
+  });
+
+  it('Should parse kB', function(){
+    assert.equal(bytes.parse('1kb'), 1 * Math.pow(1024, 1));
+    assert.equal(bytes.parse('1KB'), 1 * Math.pow(1024, 1));
+    assert.equal(bytes.parse('1Kb'), 1 * Math.pow(1024, 1));
+    assert.equal(bytes.parse('1kB'), 1 * Math.pow(1024, 1));
+
+    assert.equal(bytes.parse('0.5kb'), 0.5 * Math.pow(1024, 1));
+    assert.equal(bytes.parse('0.5KB'), 0.5 * Math.pow(1024, 1));
+    assert.equal(bytes.parse('0.5Kb'), 0.5 * Math.pow(1024, 1));
+    assert.equal(bytes.parse('0.5kB'), 0.5 * Math.pow(1024, 1));
+
+    assert.equal(bytes.parse('1.5kb'), 1.5 * Math.pow(1024, 1));
+    assert.equal(bytes.parse('1.5KB'), 1.5 * Math.pow(1024, 1));
+    assert.equal(bytes.parse('1.5Kb'), 1.5 * Math.pow(1024, 1));
+    assert.equal(bytes.parse('1.5kB'), 1.5 * Math.pow(1024, 1));
+  });
+
+  it('Should parse MB', function(){
+    assert.equal(bytes.parse('1mb'), 1 * Math.pow(1024, 2));
+    assert.equal(bytes.parse('1MB'), 1 * Math.pow(1024, 2));
+    assert.equal(bytes.parse('1Mb'), 1 * Math.pow(1024, 2));
+    assert.equal(bytes.parse('1mB'), 1 * Math.pow(1024, 2));
+  });
+
+  it('Should parse GB', function(){
+    assert.equal(bytes.parse('1gb'), 1 * Math.pow(1024, 3));
+    assert.equal(bytes.parse('1GB'), 1 * Math.pow(1024, 3));
+    assert.equal(bytes.parse('1Gb'), 1 * Math.pow(1024, 3));
+    assert.equal(bytes.parse('1gB'), 1 * Math.pow(1024, 3));
+  });
+
+  it('Should parse TB', function(){
+    assert.equal(bytes.parse('1tb'), 1 * Math.pow(1024, 4));
+    assert.equal(bytes.parse('1TB'), 1 * Math.pow(1024, 4));
+    assert.equal(bytes.parse('1Tb'), 1 * Math.pow(1024, 4));
+    assert.equal(bytes.parse('1tB'), 1 * Math.pow(1024, 4));
+
+    assert.equal(bytes.parse('0.5tb'), 0.5 * Math.pow(1024, 4));
+    assert.equal(bytes.parse('0.5TB'), 0.5 * Math.pow(1024, 4));
+    assert.equal(bytes.parse('0.5Tb'), 0.5 * Math.pow(1024, 4));
+    assert.equal(bytes.parse('0.5tB'), 0.5 * Math.pow(1024, 4));
+
+    assert.equal(bytes.parse('1.5tb'), 1.5 * Math.pow(1024, 4));
+    assert.equal(bytes.parse('1.5TB'), 1.5 * Math.pow(1024, 4));
+    assert.equal(bytes.parse('1.5Tb'), 1.5 * Math.pow(1024, 4));
+    assert.equal(bytes.parse('1.5tB'), 1.5 * Math.pow(1024, 4));
+  });
+
+  it('Should assume bytes when no units', function(){
+    assert.equal(bytes.parse('0'), 0);
+    assert.equal(bytes.parse('-1'), -1);
+    assert.equal(bytes.parse('1024'), 1024);
+  });
+
+  it('Should accept negative values', function(){
+    assert.equal(bytes.parse('-1'), -1);
+    assert.equal(bytes.parse('-1024'), -1024);
+    assert.equal(bytes.parse('-1.5TB'), -1.5 * Math.pow(1024, 4));
+  });
+
+  it('Should allow whitespace', function(){
+    assert.equal(bytes.parse('1 TB'), 1 * Math.pow(1024, 4));
+  });
+});
diff --git a/test/bytes.js b/test/bytes.js
index 0e56653..4ad8165 100644
--- a/test/bytes.js
+++ b/test/bytes.js
@@ -1,28 +1,25 @@
+'use strict';
 
-var bytes = require('..');
+var assert = require('assert');
+var bytes = require('../index.js');
 
-describe('bytes(str)', function(){
-  it('should parse kb', function(){
-    bytes('1kb').should.equal(1024);
-  })
+describe('Test constructor', function(){
+  it('Expect a function', function(){
+    assert.equal(typeof bytes, 'function');
+  });
 
-  it('should parse mb', function(){
-    bytes('1mb').should.equal(1024 * 1024);
-  })
+  it('Shoud be able to parse a string into a number', function(){
+    // This function is testes more accurately in another test suite
+    assert.equal(bytes('1kB'), 1024);
+  });
 
-  it('should parse gb', function(){
-    bytes('5gb').should.equal(5 * 1024 * 1024 * 1024);
-  })
+  it('Should convert a number into a string', function(){
+    // This function is testes more accurately in another test suite
+    assert.equal(bytes(1024), '1kB');
+  });
 
-  it('should parse tb', function(){
-    bytes('6tb').should.equal(6 * 1024 * 1024 * 1024 * 1024);
-  })
-
-  it('should support floats', function(){
-    bytes('1.5mb').should.equal(1.5 * 1024 * 1024);
-  })
-
-  it('should allow whitespace', function(){
-    bytes('1 mb').should.equal(1024 * 1024);
-  })
-})
\ No newline at end of file
+  it('Should convert a number into a string with options', function(){
+    // This function is testes more accurately in another test suite
+    assert.equal(bytes(1000, {thousandsSeparator: ' '}), '1 000B');
+  });
+});
diff --git a/test/tostring.js b/test/tostring.js
deleted file mode 100644
index f044721..0000000
--- a/test/tostring.js
+++ /dev/null
@@ -1,43 +0,0 @@
-
-var bytes = require('..')
-  , tb = (1 << 30) * 1024
-  , gb = 1 << 30
-  , mb = 1 << 20
-  , kb = 1 << 10;
-
-describe('bytes(number)', function () {
-  it('should convert numbers < 1024 to `bytes` string', function () {
-    bytes(200).should.eql('200b');
-    bytes(-200).should.eql('-200b');
-  })
-
-  it('should convert numbers >= 1024 to kb string', function () {
-    bytes(kb).should.equal('1kb')
-    bytes(-kb).should.equal('-1kb')
-    bytes(2 * kb).should.equal('2kb')
-  })
-
-  it('should convert numbers >= 1048576 to mb string', function () {
-    bytes(mb).should.equal('1mb')
-    bytes(-mb).should.equal('-1mb')
-    bytes(2 * mb).should.equal('2mb')
-  })
-
-  it('should convert numbers >= (1 << 30) to gb string', function () {
-    bytes(gb).should.equal('1gb')
-    bytes(-gb).should.equal('-1gb')
-    bytes(2 * gb).should.equal('2gb')
-  })
-
-  it('should convert numbers >= ((1 << 30) * 1024) to tb string', function () {
-    bytes(tb).should.equal('1tb')
-    bytes(-tb).should.equal('-1tb')
-    bytes(2 * tb).should.equal('2tb')
-  })
-
-  it('should support floats', function () {
-    bytes(1.2 * mb).should.equal('1.2mb')
-    bytes(-1.2 * mb).should.equal('-1.2mb')
-    bytes(1.2 * kb).should.equal('1.2kb')
-  })
-})

-- 
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