pax_global_header00006660000000000000000000000064134217657330014525gustar00rootroot0000000000000052 comment=49ac709cb210af60e35957c069bb2cd07f335cfd bytes.js-3.1.0/000077500000000000000000000000001342176573300132675ustar00rootroot00000000000000bytes.js-3.1.0/.editorconfig000066400000000000000000000002631342176573300157450ustar00rootroot00000000000000# http://editorconfig.org root = true [*] charset = utf-8 insert_final_newline = true trim_trailing_whitespace = true [{*.js,*.json,*.yml}] indent_size = 2 indent_style = space bytes.js-3.1.0/.eslintignore000066400000000000000000000000421342176573300157660ustar00rootroot00000000000000.nyc_output coverage node_modules bytes.js-3.1.0/.eslintrc.yml000066400000000000000000000002741342176573300157160ustar00rootroot00000000000000root: true extends: eslint:recommended env: node: true rules: eol-last: error indent: ["error", 2, { "SwitchCase": 1 }] no-mixed-spaces-and-tabs: error no-trailing-spaces: error bytes.js-3.1.0/.gitignore000066400000000000000000000001051342176573300152530ustar00rootroot00000000000000.nyc_output/ coverage/ node_modules/ npm-debug.log package-lock.json bytes.js-3.1.0/.travis.yml000066400000000000000000000035031342176573300154010ustar00rootroot00000000000000language: node_js node_js: - "0.8" - "0.10" - "0.12" - "1.8" - "2.5" - "3.3" - "4.9" - "5.12" - "6.16" - "7.10" - "8.15" - "9.11" - "10.15" - "11.7" sudo: false cache: directories: - node_modules before_install: # Configure npm - | # Skip updating shrinkwrap / lock npm config set shrinkwrap false # Setup Node.js version-specific dependencies - | # mocha for testing # - use 2.x for Node.js < 0.10 # - use 3.x for Node.js < 6 if [[ "$(cut -d. -f1 <<< "$TRAVIS_NODE_VERSION")" -eq 0 && "$(cut -d. -f2 <<< "$TRAVIS_NODE_VERSION")" -lt 10 ]]; then npm install --save-dev mocha@2.5.3 elif [[ "$(cut -d. -f1 <<< "$TRAVIS_NODE_VERSION")" -lt 6 ]]; then npm install --save-dev mocha@3.5.3 fi - | # nyc for coverage # - remove on Node.js < 6 if [[ "$(cut -d. -f1 <<< "$TRAVIS_NODE_VERSION")" -lt 6 ]]; then npm rm --save-dev nyc fi - | # eslint for linting # - remove on Node.js < 6 if [[ "$(cut -d. -f1 <<< "$TRAVIS_NODE_VERSION")" -lt 6 ]]; then node -pe 'Object.keys(require("./package").devDependencies).join("\n")' | \ grep -E '^eslint(-|$)' | \ xargs npm rm --save-dev fi # Update Node.js modules - | # Prune & rebuild node_modules if [[ -d node_modules ]]; then npm prune npm rebuild fi script: - | # Run test script, depending on nyc install if [[ -n "$(npm -ps ls nyc)" ]]; then npm run-script test-ci else npm test fi - | # Run linting, depending on eslint install if [[ -n "$(npm -ps ls eslint)" ]]; then npm run-script lint fi after_script: - | # Upload coverage to coveralls, if exists if [[ -d .nyc_output ]]; then npm install --save-dev coveralls@2 nyc report --reporter=text-lcov | coveralls fi bytes.js-3.1.0/History.md000066400000000000000000000030551342176573300152550ustar00rootroot000000000000003.1.0 / 2019-01-22 ================== * Add petabyte (`pb`) support 3.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] bytes.js-3.1.0/LICENSE000066400000000000000000000022011342176573300142670ustar00rootroot00000000000000(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-3.1.0/Readme.md000066400000000000000000000074621342176573300150170ustar00rootroot00000000000000# 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 * `pb` for petabytes 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: 1KB ``` ## License [MIT](LICENSE) [coveralls-image]: https://badgen.net/coveralls/c/github/visionmedia/bytes.js/master [coveralls-url]: https://coveralls.io/r/visionmedia/bytes.js?branch=master [downloads-image]: https://badgen.net/npm/dm/bytes [downloads-url]: https://npmjs.org/package/bytes [npm-image]: https://badgen.net/npm/node/bytes [npm-url]: https://npmjs.org/package/bytes [travis-image]: https://badgen.net/travis/visionmedia/bytes.js/master [travis-url]: https://travis-ci.org/visionmedia/bytes.js bytes.js-3.1.0/index.js000066400000000000000000000066151342176573300147440ustar00rootroot00000000000000/*! * 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: Math.pow(1024, 4), pb: Math.pow(1024, 5), }; var parseRegExp = /^((-|\+)?(\d+(?:\.\d+)?)) *(kb|mb|gb|tb|pb)$/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.pb) { unit = 'PB'; } else 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); } bytes.js-3.1.0/package.json000066400000000000000000000016101342176573300155530ustar00rootroot00000000000000{ "name": "bytes", "description": "Utility to parse a string bytes to bytes and vice-versa", "version": "3.1.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": { "eslint": "5.12.1", "mocha": "5.2.0", "nyc": "13.1.0" }, "files": [ "History.md", "LICENSE", "Readme.md", "index.js" ], "engines": { "node": ">= 0.8" }, "scripts": { "lint": "eslint .", "test": "mocha --check-leaks --reporter spec", "test-ci": "nyc --reporter=text npm test", "test-cov": "nyc --reporter=html --reporter=text npm test" } } bytes.js-3.1.0/test/000077500000000000000000000000001342176573300142465ustar00rootroot00000000000000bytes.js-3.1.0/test/.eslintrc.yml000066400000000000000000000000231342176573300166650ustar00rootroot00000000000000env: mocha: true bytes.js-3.1.0/test/byte-format.js000066400000000000000000000122521342176573300170370ustar00rootroot00000000000000'use strict'; var assert = require('assert'); var bytes = require('..'); describe('Test byte format function', function(){ var pb = Math.pow(1024, 5); var tb = (1 << 30) * 1024, gb = 1 << 30, mb = 1 << 20, kb = 1 << 10; it('Should return null if input is 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 convert numbers >= 1 125 899 906 842 624 to pb string', function(){ assert.equal(bytes.format(pb).toLowerCase(), '1pb'); assert.equal(bytes.format(-pb).toLowerCase(), '-1pb'); assert.equal(bytes.format(2 * pb).toLowerCase(), '2pb'); }); 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'); assert.equal(bytes.format(pb), '1PB'); }); it('Should 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 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'); }) }); bytes.js-3.1.0/test/byte-parse.js000066400000000000000000000105741342176573300166660ustar00rootroot00000000000000'use strict'; var assert = require('assert'); var bytes = require('..'); describe('Test byte parse function', function(){ it('Should return null if input is 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 parse PB', function(){ assert.equal(bytes.parse('1pb'), 1 * Math.pow(1024, 5)); assert.equal(bytes.parse('1PB'), 1 * Math.pow(1024, 5)); assert.equal(bytes.parse('1Pb'), 1 * Math.pow(1024, 5)); assert.equal(bytes.parse('1pB'), 1 * Math.pow(1024, 5)); assert.equal(bytes.parse('0.5pb'), 0.5 * Math.pow(1024, 5)); assert.equal(bytes.parse('0.5PB'), 0.5 * Math.pow(1024, 5)); assert.equal(bytes.parse('0.5Pb'), 0.5 * Math.pow(1024, 5)); assert.equal(bytes.parse('0.5pB'), 0.5 * Math.pow(1024, 5)); assert.equal(bytes.parse('1.5pb'), 1.5 * Math.pow(1024, 5)); assert.equal(bytes.parse('1.5PB'), 1.5 * Math.pow(1024, 5)); assert.equal(bytes.parse('1.5Pb'), 1.5 * Math.pow(1024, 5)); assert.equal(bytes.parse('1.5pB'), 1.5 * Math.pow(1024, 5)); }); 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)); }); }); bytes.js-3.1.0/test/bytes.js000066400000000000000000000022131342176573300157300ustar00rootroot00000000000000'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 is 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('Should be able to parse a string into a number', function(){ // This function is tested more accurately in another test suite assert.equal(bytes('1KB'), 1024); }); it('Should convert a number into a string', function(){ // This function is tested 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 tested more accurately in another test suite assert.equal(bytes(1000, {thousandsSeparator: ' '}), '1 000B'); }); });