pax_global_header00006660000000000000000000000064125275152360014522gustar00rootroot0000000000000052 comment=86e4520cc369b34866154a53344ca50b2bb5ddcd bytes.js-2.1.0/000077500000000000000000000000001252751523600132635ustar00rootroot00000000000000bytes.js-2.1.0/.gitignore000066400000000000000000000052221252751523600152540ustar00rootroot00000000000000# 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 bytes.js-2.1.0/History.md000066400000000000000000000016031252751523600152460ustar00rootroot000000000000002.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 ================== * add negative support. fixes #6 0.3.0 / 2014-03-19 ================== * added terabyte support 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-2.1.0/LICENSE000066400000000000000000000022011252751523600142630ustar00rootroot00000000000000(The MIT License) Copyright (c) 2012-2014 TJ Holowaychuk Copyright (c) 2015 Jed Watson 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-2.1.0/Readme.md000066400000000000000000000040261252751523600150040ustar00rootroot00000000000000# Bytes utility Utility to parse a string bytes (ex: `1TB`) to bytes (`1099511627776`) and vice-versa. ## Usage ```js var bytes = require('bytes'); ``` #### bytes.format(number value, [options]): string|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. **Arguments** | Name | Type | Description | |---------|--------|--------------------| | value | `number` | Value in bytes | | options | `Object` | Conversion options | **Options** | 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' ``` #### 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. **Arguments** | Name | Type | Description | |---------------|--------|--------------------| | value | `string` | String to parse. | **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 [![npm](https://img.shields.io/npm/l/express.svg)](https://github.com/visionmedia/bytes.js/blob/master/LICENSE) bytes.js-2.1.0/component.json000066400000000000000000000003611252751523600161600ustar00rootroot00000000000000{ "name": "bytes", "description": "Byte size string parser / serializer.", "repository": "visionmedia/bytes.js.git", "keywords": [ "bytes", "utility" ], "version": "2.1.0", "scripts": ["index.js"], "license": "MIT" } bytes.js-2.1.0/index.js000066400000000000000000000051021252751523600147260ustar00rootroot00000000000000/*! * bytes * Copyright(c) 2012-2014 TJ Holowaychuk * Copyright(c) 2015 Jed Watson * MIT Licensed */ 'use strict'; /** * Module exports. * @public */ module.exports = bytes; module.exports.format = format; module.exports.parse = parse; /** * Module variables. * @private */ var map = { b: 1, kb: 1 << 10, mb: 1 << 20, gb: 1 << 30, tb: ((1 << 30) * 1024) }; /** *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. * * If no unit is given, it is assumed the value is in bytes. * * @param {number|string} val * @public */ 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; } bytes.js-2.1.0/package.json000066400000000000000000000013151252751523600155510ustar00rootroot00000000000000{ "name": "bytes", "description": "Utility to parse a string bytes to bytes and vice-versa", "version": "2.1.0", "author": "TJ Holowaychuk (http://tjholowaychuk.com)", "contributors": [ "Jed Watson " ], "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" } } bytes.js-2.1.0/test/000077500000000000000000000000001252751523600142425ustar00rootroot00000000000000bytes.js-2.1.0/test/byte-format.js000066400000000000000000000057431252751523600170420ustar00rootroot00000000000000'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'); }) }); bytes.js-2.1.0/test/byte-parse.js000066400000000000000000000066261252751523600166650ustar00rootroot00000000000000'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)); }); }); bytes.js-2.1.0/test/bytes.js000066400000000000000000000014201252751523600157230ustar00rootroot00000000000000'use strict'; var assert = require('assert'); var bytes = require('../index.js'); describe('Test constructor', function(){ it('Expect a function', function(){ assert.equal(typeof bytes, 'function'); }); 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 convert a number into a string', function(){ // This function is testes more accurately in another test suite assert.equal(bytes(1024), '1kB'); }); 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'); }); });