[Pkg-javascript-commits] [node-bytes] 03/14: Imported Upstream version 1.0.0

Paolo Greppi paolog-guest at moszumanska.debian.org
Fri Dec 23 13:20:49 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 4f6786b5716b41e507c40540d34c2991f38e4512
Author: Jérémy Lal <kapouer at melix.org>
Date:   Sat Jun 14 21:20:17 2014 +0200

    Imported Upstream version 1.0.0
---
 History.md       | 10 ++++++++++
 Readme.md        |  3 +++
 index.js         | 12 +++++++-----
 package.json     |  6 +++++-
 test/bytes.js    |  4 ++++
 test/tostring.js | 12 ++++++++++++
 6 files changed, 41 insertions(+), 6 deletions(-)

diff --git a/History.md b/History.md
index f233ed1..5097352 100644
--- a/History.md
+++ b/History.md
@@ -1,4 +1,14 @@
 
+1.0.0 / 2014-05-05
+==================
+
+ * add negative support. fixes #6
+
+0.3.0 / 2014-03-19
+==================
+
+ * added terabyte support
+
 0.2.1 / 2013-04-01 
 ==================
 
diff --git a/Readme.md b/Readme.md
index 9325d5b..5591b28 100644
--- a/Readme.md
+++ b/Readme.md
@@ -16,6 +16,9 @@ bytes('1gb')
 
 bytes(1073741824)
 // => 1gb
+
+bytes(1099511627776)
+// => 1tb
 ```
 
 ## Installation
diff --git a/index.js b/index.js
index 70b2e01..c1da2fe 100644
--- a/index.js
+++ b/index.js
@@ -9,7 +9,7 @@
 
 module.exports = function(size) {
   if ('number' == typeof size) return convert(size);
-  var parts = size.match(/^(\d+(?:\.\d+)?) *(kb|mb|gb)$/)
+  var parts = size.match(/^(\d+(?:\.\d+)?) *(kb|mb|gb|tb)$/)
     , n = parseFloat(parts[1])
     , type = parts[2];
 
@@ -17,6 +17,7 @@ module.exports = function(size) {
       kb: 1 << 10
     , mb: 1 << 20
     , gb: 1 << 30
+    , tb: ((1 << 30) * 1024)
   };
 
   return map[type] * n;
@@ -31,9 +32,10 @@ module.exports = function(size) {
  */
 
 function convert (b) {
-  var gb = 1 << 30, mb = 1 << 20, kb = 1 << 10;
-  if (b >= gb) return (Math.round(b / gb * 100) / 100) + 'gb';
-  if (b >= mb) return (Math.round(b / mb * 100) / 100) + 'mb';
-  if (b >= kb) return (Math.round(b / kb * 100) / 100) + 'kb';
+  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';
 }
diff --git a/package.json b/package.json
index 06bdb78..76e0bff 100644
--- a/package.json
+++ b/package.json
@@ -2,7 +2,11 @@
   "name": "bytes",
   "author": "TJ Holowaychuk <tj at vision-media.ca> (http://tjholowaychuk.com)",
   "description": "byte size string parser / serializer",
-  "version": "0.2.1",
+  "repository": {
+    "type": "git",
+    "url": "https://github.com/visionmedia/bytes.js.git"
+  },
+  "version": "1.0.0",
   "main": "index.js",
   "dependencies": {},
   "devDependencies": {
diff --git a/test/bytes.js b/test/bytes.js
index a59c9d0..0e56653 100644
--- a/test/bytes.js
+++ b/test/bytes.js
@@ -14,6 +14,10 @@ describe('bytes(str)', function(){
     bytes('5gb').should.equal(5 * 1024 * 1024 * 1024);
   })
 
+  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);
   })
diff --git a/test/tostring.js b/test/tostring.js
index fc693bd..f044721 100644
--- a/test/tostring.js
+++ b/test/tostring.js
@@ -1,5 +1,6 @@
 
 var bytes = require('..')
+  , tb = (1 << 30) * 1024
   , gb = 1 << 30
   , mb = 1 << 20
   , kb = 1 << 10;
@@ -7,25 +8,36 @@ var bytes = require('..')
 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