pax_global_header00006660000000000000000000000064131760751340014521gustar00rootroot0000000000000052 comment=bd004c9c5024061cd90564d42605e0dcb7cf528f node-bytes-3.0.0/000077500000000000000000000000001317607513400135725ustar00rootroot00000000000000node-bytes-3.0.0/.gitignore000066400000000000000000000001041317607513400155550ustar00rootroot00000000000000.nyc_output/ coverage/ node_modules/ npm-debug.log package-log.json node-bytes-3.0.0/.travis.yml000066400000000000000000000014461317607513400157100ustar00rootroot00000000000000language: node_js node_js: - "0.8" - "0.10" - "0.12" - "1.8" - "2.4" - "3.3" - "4.8" - "5.12" - "6.11" - "7.10" - "8.4" sudo: false cache: directories: - node_modules before_install: # Skip updating shrinkwrap / lock - "npm config set shrinkwrap false" # Setup Node.js version-specific dependencies - "test $TRAVIS_NODE_VERSION != '0.8' || npm rm --save-dev nyc" # Update Node.js modules - "test ! -d node_modules || npm prune" - "test ! -d node_modules || npm rebuild" script: # Run test script, depending on nyc install - "test ! -z $(npm -ps ls nyc) || npm test" - "test -z $(npm -ps ls nyc) || npm run-script test-ci" after_script: # Report coverage - "test -d .nyc_output && npm install coveralls@2 && nyc report --reporter=text-lcov | coveralls" node-bytes-3.0.0/History.md000066400000000000000000000027451317607513400155650ustar00rootroot000000000000003.0.0 / 2017-08-31 ================== * Change "kB" to "KB" in format output * Remove support for Node.js 0.6 * Remove support for ComponentJS 2.5.0 / 2017-03-24 ================== * Add option "unit" 2.4.0 / 2016-06-01 ================== * Add option "unitSeparator" 2.3.0 / 2016-02-15 ================== * Drop partial bytes on all parsed units * Fix non-finite numbers to `.format` to return `null` * Fix parsing byte string that looks like hex * perf: hoist regular expressions 2.2.0 / 2015-11-13 ================== * add option "decimalPlaces" * add option "fixedDecimals" 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 ================== * 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] node-bytes-3.0.0/LICENSE000066400000000000000000000022011317607513400145720ustar00rootroot00000000000000(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. node-bytes-3.0.0/Readme.md000066400000000000000000000074601317607513400153200ustar00rootroot00000000000000# Bytes utility [![NPM Version][npm-image]][npm-url] [![NPM Downloads][downloads-image]][downloads-url] [![Build Status][travis-image]][travis-url] [![Test Coverage][coveralls-image]][coveralls-url] Utility to parse a string bytes (ex: `1TB`) to bytes (`1099511627776`) and vice-versa. ## Installation This is a [Node.js](https://nodejs.org/en/) module available through the [npm registry](https://www.npmjs.com/). Installation is done using the [`npm install` command](https://docs.npmjs.com/getting-started/installing-npm-packages-locally): ```bash $ npm install bytes ``` ## 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 | |-------------------|--------|-----------------------------------------------------------------------------------------| | decimalPlaces | `number`|`null` | Maximum number of decimal places to include in output. Default value to `2`. | | fixedDecimals | `boolean`|`null` | Whether to always display the maximum number of decimal places. Default value to `false` | | thousandsSeparator | `string`|`null` | Example of values: `' '`, `','` and `.`... Default value to `''`. | | unit | `string`|`null` | The unit in which the result will be returned (B/KB/MB/GB/TB). Default value to `''` (which means auto detect). | | unitSeparator | `string`|`null` | Separator to use between number and unit. 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(1024 * 1.7, {decimalPlaces: 0}); // output: '2KB' bytes(1024, {unitSeparator: ' '}); // output: '1 KB' ``` #### bytes.parse(string|number value): number|null Parse the string value into an integer in bytes. If no unit is given, or `value` is a number, it is assumed the value is in bytes. Supported units and abbreviations are as follows and are case-insensitive: * `b` for bytes * `kb` for kilobytes * `mb` for megabytes * `gb` for gigabytes * `tb` for terabytes The units are in powers of two, not ten. This means 1kb = 1024b according to this parser. **Arguments** | Name | Type | Description | |---------------|--------|--------------------| | value | `string`|`number` | String to parse, or number in bytes. | **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 bytes(1024); // output: 1024 ``` ## License [MIT](LICENSE) [downloads-image]: https://img.shields.io/npm/dm/bytes.svg [downloads-url]: https://npmjs.org/package/bytes [npm-image]: https://img.shields.io/npm/v/bytes.svg [npm-url]: https://npmjs.org/package/bytes [travis-image]: https://img.shields.io/travis/visionmedia/bytes.js/master.svg [travis-url]: https://travis-ci.org/visionmedia/bytes.js [coveralls-image]: https://img.shields.io/coveralls/visionmedia/bytes.js/master.svg [coveralls-url]: https://coveralls.io/r/visionmedia/bytes.js?branch=master node-bytes-3.0.0/index.js000066400000000000000000000064761317607513400152540ustar00rootroot00000000000000/*! * 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 formatThousandsRegExp = /\B(?=(\d{3})+(?!\d))/g; var formatDecimalsRegExp = /(?:\.0*|(\.[^0]+)0+)$/; var map = { b: 1, kb: 1 << 10, mb: 1 << 20, gb: 1 << 30, tb: ((1 << 30) * 1024) }; var parseRegExp = /^((-|\+)?(\d+(?:\.\d+)?)) *(kb|mb|gb|tb)$/i; /** * 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], * decimalPlaces: [number] * fixedDecimals: [boolean] * thousandsSeparator: [string] * unitSeparator: [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 {number} [options.decimalPlaces=2] * @param {number} [options.fixedDecimals=false] * @param {string} [options.thousandsSeparator=] * @param {string} [options.unit=] * @param {string} [options.unitSeparator=] * * @returns {string|null} * @public */ function format(value, options) { if (!Number.isFinite(value)) { return null; } var mag = Math.abs(value); var thousandsSeparator = (options && options.thousandsSeparator) || ''; var unitSeparator = (options && options.unitSeparator) || ''; var decimalPlaces = (options && options.decimalPlaces !== undefined) ? options.decimalPlaces : 2; var fixedDecimals = Boolean(options && options.fixedDecimals); var unit = (options && options.unit) || ''; if (!unit || !map[unit.toLowerCase()]) { if (mag >= map.tb) { unit = 'TB'; } else if (mag >= map.gb) { unit = 'GB'; } else if (mag >= map.mb) { unit = 'MB'; } else if (mag >= map.kb) { unit = 'KB'; } else { unit = 'B'; } } var val = value / map[unit.toLowerCase()]; var str = val.toFixed(decimalPlaces); if (!fixedDecimals) { str = str.replace(formatDecimalsRegExp, '$1'); } if (thousandsSeparator) { str = str.replace(formatThousandsRegExp, thousandsSeparator); } return str + unitSeparator + 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 * * @returns {number|null} * @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 = parseRegExp.exec(val); var floatValue; var unit = 'b'; if (!results) { // Nothing could be extracted from the given string floatValue = parseInt(val, 10); unit = 'b' } else { // Retrieve the value and the unit floatValue = parseFloat(results[1]); unit = results[4].toLowerCase(); } return Math.floor(map[unit] * floatValue); } node-bytes-3.0.0/package.json000066400000000000000000000015301317607513400160570ustar00rootroot00000000000000{ "name": "bytes", "description": "Utility to parse a string bytes to bytes and vice-versa", "version": "3.0.0", "author": "TJ Holowaychuk (http://tjholowaychuk.com)", "contributors": [ "Jed Watson ", "Théo FIDRY " ], "license": "MIT", "keywords": [ "byte", "bytes", "utility", "parse", "parser", "convert", "converter" ], "repository": "visionmedia/bytes.js", "devDependencies": { "mocha": "2.5.3", "nyc": "10.3.2" }, "files": [ "History.md", "LICENSE", "Readme.md", "index.js" ], "engines": { "node": ">= 0.8" }, "scripts": { "test": "mocha --check-leaks --reporter spec", "test-ci": "nyc --reporter=text npm test", "test-cov": "nyc --reporter=html --reporter=text npm test" } } node-bytes-3.0.0/test/000077500000000000000000000000001317607513400145515ustar00rootroot00000000000000node-bytes-3.0.0/test/byte-format.js000066400000000000000000000115141317607513400173420ustar00rootroot00000000000000'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(NaN), null); assert.strictEqual(bytes.format(Infinity), 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 custom unit separator', function(){ assert.equal(bytes.format(1024), '1KB'); assert.equal(bytes.format(1024, {unitSeparator: ''}), '1KB'); assert.equal(bytes.format(1024, {unitSeparator: null}), '1KB'); assert.equal(bytes.format(1024, {unitSeparator: ' '}), '1 KB'); assert.equal(bytes.format(1024, {unitSeparator: '\t'}), '1\tKB'); }); it('Should support custom number of decimal places', function(){ assert.equal(bytes.format(kb - 1, {decimalPlaces: 0}).toLowerCase(), '1023b'); assert.equal(bytes.format(kb, {decimalPlaces: 0}).toLowerCase(), '1kb'); assert.equal(bytes.format(1.4 * kb, {decimalPlaces: 0}).toLowerCase(), '1kb'); assert.equal(bytes.format(1.5 * kb, {decimalPlaces: 0}).toLowerCase(), '2kb'); assert.equal(bytes.format(kb - 1, {decimalPlaces: 1}).toLowerCase(), '1023b'); assert.equal(bytes.format(kb, {decimalPlaces: 1}).toLowerCase(), '1kb'); assert.equal(bytes.format(1.04 * kb, {decimalPlaces: 1}).toLowerCase(), '1kb'); assert.equal(bytes.format(1.05 * kb, {decimalPlaces: 1}).toLowerCase(), '1.1kb'); }); it('Should support fixed decimal places', function(){ assert.equal(bytes.format(kb, {decimalPlaces: 3, fixedDecimals: true}).toLowerCase(), '1.000kb'); }); 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'); }) it('Should support custom unit', function(){ assert.equal(bytes.format(12 * mb, {unit: 'b'}).toLowerCase(), '12582912b'); assert.equal(bytes.format(12 * mb, {unit: 'kb'}).toLowerCase(), '12288kb'); assert.equal(bytes.format(12 * gb, {unit: 'mb'}).toLowerCase(), '12288mb'); assert.equal(bytes.format(12 * tb, {unit: 'gb'}).toLowerCase(), '12288gb'); assert.equal(bytes.format(12 * mb, {unit: ''}).toLowerCase(), '12mb'); assert.equal(bytes.format(12 * mb, {unit: 'bb'}).toLowerCase(), '12mb'); }) }); node-bytes-3.0.0/test/byte-parse.js000066400000000000000000000071201317607513400171620ustar00rootroot00000000000000'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); assert.equal(bytes.parse('0x11'), 0); }); 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 drop partial bytes', function(){ assert.equal(bytes.parse('1.1b'), 1); assert.equal(bytes.parse('1.0001kb'), 1024); }); it('Should allow whitespace', function(){ assert.equal(bytes.parse('1 TB'), 1 * Math.pow(1024, 4)); }); }); node-bytes-3.0.0/test/bytes.js000066400000000000000000000022071317607513400162360ustar00rootroot00000000000000'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('Should return null if input invalid', function(){ assert.strictEqual(bytes(undefined), null); assert.strictEqual(bytes(null), null); assert.strictEqual(bytes(true), null); assert.strictEqual(bytes(false), null); assert.strictEqual(bytes(NaN), null); assert.strictEqual(bytes(function(){}), null); assert.strictEqual(bytes({}), null); }); 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'); }); });