pax_global_header00006660000000000000000000000064121263531330014511gustar00rootroot0000000000000052 comment=6f482b4884d348cd7ca3ed22c4157f422eaf5838 bytes.js-0.2.1/000077500000000000000000000000001212635313300132525ustar00rootroot00000000000000bytes.js-0.2.1/.gitignore000066400000000000000000000000151212635313300152360ustar00rootroot00000000000000node_modules bytes.js-0.2.1/.npmignore000066400000000000000000000000051212635313300152440ustar00rootroot00000000000000test bytes.js-0.2.1/History.md000066400000000000000000000003341212635313300152350ustar00rootroot00000000000000 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] bytes.js-0.2.1/Makefile000066400000000000000000000001311212635313300147050ustar00rootroot00000000000000 test: @./node_modules/.bin/mocha \ --reporter spec \ --require should .PHONY: testbytes.js-0.2.1/Readme.md000066400000000000000000000025551212635313300150000ustar00rootroot00000000000000# 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@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. bytes.js-0.2.1/component.json000066400000000000000000000002441212635313300161470ustar00rootroot00000000000000{ "name": "bytes", "description": "byte size string parser / serializer", "keywords": ["bytes", "utility"], "version": "0.2.1", "scripts": ["index.js"] } bytes.js-0.2.1/index.js000066400000000000000000000014401212635313300147160ustar00rootroot00000000000000 /** * 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'; } bytes.js-0.2.1/package.json000066400000000000000000000005571212635313300155470ustar00rootroot00000000000000{ "name": "bytes", "author": "TJ Holowaychuk (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" } } } bytes.js-0.2.1/test/000077500000000000000000000000001212635313300142315ustar00rootroot00000000000000bytes.js-0.2.1/test/bytes.js000066400000000000000000000010121212635313300157070ustar00rootroot00000000000000 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); }) })bytes.js-0.2.1/test/tostring.js000066400000000000000000000014471212635313300164460ustar00rootroot00000000000000 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') }) })