[Pkg-javascript-commits] [node-bytes] 01/14: Imported Upstream version 0.2.1

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 17dac182426e80698d7299b9c5b42a860816ad9b
Author: Jérémy Lal <kapouer at melix.org>
Date:   Sun Oct 27 15:43:27 2013 +0100

    Imported Upstream version 0.2.1
---
 .gitignore       |  1 +
 .npmignore       |  1 +
 History.md       | 15 +++++++++++++++
 Makefile         |  7 +++++++
 Readme.md        | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++
 component.json   |  7 +++++++
 index.js         | 39 +++++++++++++++++++++++++++++++++++++++
 package.json     | 17 +++++++++++++++++
 test/bytes.js    | 24 ++++++++++++++++++++++++
 test/tostring.js | 31 +++++++++++++++++++++++++++++++
 10 files changed, 193 insertions(+)

diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..3c3629e
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1 @@
+node_modules
diff --git a/.npmignore b/.npmignore
new file mode 100644
index 0000000..9daeafb
--- /dev/null
+++ b/.npmignore
@@ -0,0 +1 @@
+test
diff --git a/History.md b/History.md
new file mode 100644
index 0000000..f233ed1
--- /dev/null
+++ b/History.md
@@ -0,0 +1,15 @@
+
+0.2.1 / 2013-04-01 
+==================
+
+  * add .component
+
+0.2.0 / 2012-10-28 
+==================
+
+  * bytes(200).should.eql('200b')
+
+0.1.0 / 2012-07-04 
+==================
+
+  * add bytes to string conversion [yields]
diff --git a/Makefile b/Makefile
new file mode 100644
index 0000000..8e8640f
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,7 @@
+
+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
new file mode 100644
index 0000000..9325d5b
--- /dev/null
+++ b/Readme.md
@@ -0,0 +1,51 @@
+# node-bytes
+
+  Byte string parser / formatter.
+
+## Example:
+
+```js
+bytes('1kb')
+// => 1024
+
+bytes('2mb')
+// => 2097152
+
+bytes('1gb')
+// => 1073741824
+
+bytes(1073741824)
+// => 1gb
+```
+
+## Installation
+
+```
+$ npm install bytes
+$ component install visionmedia/bytes.js
+```
+
+## License 
+
+(The MIT License)
+
+Copyright (c) 2012 TJ Holowaychuk <tj at vision-media.ca>
+
+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:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+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.
diff --git a/component.json b/component.json
new file mode 100644
index 0000000..2929c25
--- /dev/null
+++ b/component.json
@@ -0,0 +1,7 @@
+{
+  "name": "bytes",
+  "description": "byte size string parser / serializer",
+  "keywords": ["bytes", "utility"],
+  "version": "0.2.1",
+  "scripts": ["index.js"]
+}
diff --git a/index.js b/index.js
new file mode 100644
index 0000000..70b2e01
--- /dev/null
+++ b/index.js
@@ -0,0 +1,39 @@
+
+/**
+ * Parse byte `size` string.
+ *
+ * @param {String} size
+ * @return {Number}
+ * @api public
+ */
+
+module.exports = function(size) {
+  if ('number' == typeof size) return convert(size);
+  var parts = size.match(/^(\d+(?:\.\d+)?) *(kb|mb|gb)$/)
+    , n = parseFloat(parts[1])
+    , type = parts[2];
+
+  var map = {
+      kb: 1 << 10
+    , mb: 1 << 20
+    , gb: 1 << 30
+  };
+
+  return map[type] * n;
+};
+
+/**
+ * convert bytes into string.
+ *
+ * @param {Number} b - bytes to convert
+ * @return {String}
+ * @api public
+ */
+
+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';
+  return b + 'b';
+}
diff --git a/package.json b/package.json
new file mode 100644
index 0000000..06bdb78
--- /dev/null
+++ b/package.json
@@ -0,0 +1,17 @@
+{
+  "name": "bytes",
+  "author": "TJ Holowaychuk <tj at vision-media.ca> (http://tjholowaychuk.com)",
+  "description": "byte size string parser / serializer",
+  "version": "0.2.1",
+  "main": "index.js",
+  "dependencies": {},
+  "devDependencies": {
+    "mocha": "*",
+    "should": "*"
+  },
+  "component": {
+    "scripts": {
+      "bytes/index.js": "index.js"
+    }
+  }
+}
diff --git a/test/bytes.js b/test/bytes.js
new file mode 100644
index 0000000..a59c9d0
--- /dev/null
+++ b/test/bytes.js
@@ -0,0 +1,24 @@
+
+var bytes = require('..');
+
+describe('bytes(str)', function(){
+  it('should parse kb', function(){
+    bytes('1kb').should.equal(1024);
+  })
+
+  it('should parse mb', function(){
+    bytes('1mb').should.equal(1024 * 1024);
+  })
+
+  it('should parse gb', function(){
+    bytes('5gb').should.equal(5 * 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
diff --git a/test/tostring.js b/test/tostring.js
new file mode 100644
index 0000000..fc693bd
--- /dev/null
+++ b/test/tostring.js
@@ -0,0 +1,31 @@
+
+var bytes = require('..')
+  , 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');
+  })
+
+  it('should convert numbers >= 1024 to kb string', function () {
+    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(2 * mb).should.equal('2mb')
+  })
+
+  it('should convert numbers >= (1 << 30) to gb string', function () {
+    bytes(gb).should.equal('1gb')
+    bytes(2 * gb).should.equal('2gb')
+  })
+
+  it('should support floats', function () {
+    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