pax_global_header00006660000000000000000000000064141746735460014533gustar00rootroot0000000000000052 comment=9ddc13b6c66e0cb293616fba246e05db4b6cef4d bytes.js-3.1.2/000077500000000000000000000000001417467354600132775ustar00rootroot00000000000000bytes.js-3.1.2/.editorconfig000066400000000000000000000002631417467354600157550ustar00rootroot00000000000000# 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.2/.eslintignore000066400000000000000000000000421417467354600157760ustar00rootroot00000000000000.nyc_output coverage node_modules bytes.js-3.1.2/.eslintrc.yml000066400000000000000000000004711417467354600157250ustar00rootroot00000000000000root: true extends: - eslint:recommended - plugin:markdown/recommended env: node: true plugins: - markdown overrides: - files: '**/*.md' processor: 'markdown/markdown' rules: eol-last: error indent: ["error", 2, { "SwitchCase": 1 }] no-mixed-spaces-and-tabs: error no-trailing-spaces: error bytes.js-3.1.2/.github/000077500000000000000000000000001417467354600146375ustar00rootroot00000000000000bytes.js-3.1.2/.github/workflows/000077500000000000000000000000001417467354600166745ustar00rootroot00000000000000bytes.js-3.1.2/.github/workflows/ci.yml000066400000000000000000000115101417467354600200100ustar00rootroot00000000000000name: ci on: - pull_request - push jobs: test: runs-on: ubuntu-20.04 strategy: matrix: name: - Node.js 0.8 - Node.js 0.10 - Node.js 0.12 - io.js 1.x - io.js 2.x - io.js 3.x - Node.js 4.x - Node.js 5.x - Node.js 6.x - Node.js 7.x - Node.js 8.x - Node.js 9.x - Node.js 10.x - Node.js 11.x - Node.js 12.x - Node.js 13.x - Node.js 14.x - Node.js 15.x - Node.js 16.x - Node.js 17.x include: - name: Node.js 0.8 node-version: "0.8" npm-i: mocha@2.5.3 npm-rm: nyc - name: Node.js 0.10 node-version: "0.10" npm-i: mocha@3.5.3 npm-rm: nyc - name: Node.js 0.12 node-version: "0.12" npm-i: mocha@3.5.3 npm-rm: nyc - name: io.js 1.x node-version: "1.8" npm-i: mocha@3.5.3 npm-rm: nyc - name: io.js 2.x node-version: "2.5" npm-i: mocha@3.5.3 npm-rm: nyc - name: io.js 3.x node-version: "3.3" npm-i: mocha@3.5.3 npm-rm: nyc - name: Node.js 4.x node-version: "4.9" npm-i: mocha@5.2.0 npm-rm: nyc - name: Node.js 5.x node-version: "5.12" npm-i: mocha@5.2.0 npm-rm: nyc - name: Node.js 6.x node-version: "6.16" npm-i: mocha@6.2.3 npm-rm: nyc - name: Node.js 7.x node-version: "7.10" npm-i: mocha@6.2.3 npm-rm: nyc - name: Node.js 8.x node-version: "8.15" npm-i: mocha@7.2.0 - name: Node.js 9.x node-version: "9.11" npm-i: mocha@7.2.0 - name: Node.js 10.x node-version: "10.24" npm-i: mocha@8.4.0 - name: Node.js 11.x node-version: "11.15" npm-i: mocha@8.4.0 - name: Node.js 12.x node-version: "12.22" - name: Node.js 13.x node-version: "13.14" - name: Node.js 14.x node-version: "14.18" - name: Node.js 15.x node-version: "15.14" - name: Node.js 16.x node-version: "16.13" - name: Node.js 17.x node-version: "17.4" steps: - uses: actions/checkout@v2 - name: Install Node.js ${{ matrix.node-version }} shell: bash -eo pipefail -l {0} run: | nvm install --default ${{ matrix.node-version }} if [[ "${{ matrix.node-version }}" == 0.* && "$(cut -d. -f2 <<< "${{ matrix.node-version }}")" -lt 10 ]]; then nvm install --alias=npm 0.10 nvm use ${{ matrix.node-version }} sed -i '1s;^.*$;'"$(printf '#!%q' "$(nvm which npm)")"';' "$(readlink -f "$(which npm)")" npm config set strict-ssl false fi dirname "$(nvm which ${{ matrix.node-version }})" >> "$GITHUB_PATH" - name: Configure npm run: npm config set shrinkwrap false - name: Remove npm module(s) ${{ matrix.npm-rm }} run: npm rm --silent --save-dev ${{ matrix.npm-rm }} if: matrix.npm-rm != '' - name: Install npm module(s) ${{ matrix.npm-i }} run: npm install --save-dev ${{ matrix.npm-i }} if: matrix.npm-i != '' - name: Setup Node.js version-specific dependencies shell: bash run: | # eslint for linting # - remove on Node.js < 10 if [[ "$(cut -d. -f1 <<< "${{ matrix.node-version }}")" -lt 10 ]]; then node -pe 'Object.keys(require("./package").devDependencies).join("\n")' | \ grep -E '^eslint(-|$)' | \ sort -r | \ xargs -n1 npm rm --silent --save-dev fi - name: Install Node.js dependencies run: npm install - name: List environment id: list_env shell: bash run: | echo "node@$(node -v)" echo "npm@$(npm -v)" npm -s ls ||: (npm -s ls --depth=0 ||:) | awk -F'[ @]' 'NR>1 && $2 { print "::set-output name=" $2 "::" $3 }' - name: Run tests shell: bash run: | if npm -ps ls nyc | grep -q nyc; then npm run test-ci else npm test fi - name: Lint code if: steps.list_env.outputs.eslint != '' run: npm run lint - name: Collect code coverage uses: coverallsapp/github-action@master if: steps.list_env.outputs.nyc != '' with: github-token: ${{ secrets.GITHUB_TOKEN }} flag-name: run-${{ matrix.test_number }} parallel: true coverage: needs: test runs-on: ubuntu-latest steps: - name: Upload code coverage uses: coverallsapp/github-action@master with: github-token: ${{ secrets.github_token }} parallel-finished: true bytes.js-3.1.2/.gitignore000066400000000000000000000001051417467354600152630ustar00rootroot00000000000000.nyc_output/ coverage/ node_modules/ npm-debug.log package-lock.json bytes.js-3.1.2/History.md000066400000000000000000000033571417467354600152720ustar00rootroot000000000000003.1.2 / 2022-01-27 ================== * Fix return value for un-parsable strings 3.1.1 / 2021-11-15 ================== * Fix "thousandsSeparator" incorrecting formatting fractional part 3.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.2/LICENSE000066400000000000000000000022011417467354600142770ustar00rootroot00000000000000(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.2/Readme.md000066400000000000000000000112421417467354600150160ustar00rootroot00000000000000# Bytes utility [![NPM Version][npm-image]][npm-url] [![NPM Downloads][downloads-image]][downloads-url] [![Build Status][ci-image]][ci-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(number|string value, [options]): number|string|null Default export function. Delegates to either `bytes.format` or `bytes.parse` based on the type of `value`. **Arguments** | Name | Type | Description | |---------|----------|--------------------| | value | `number`|`string` | Number value to format or string value to parse | | options | `Object` | Conversion options for `format` | **Returns** | Name | Type | Description | |---------|------------------|-------------------------------------------------| | results | `string`|`number`|`null` | Return null upon error. Numeric value in bytes, or string value otherwise. | **Example** ```js bytes(1024); // output: '1KB' bytes('1KB'); // output: 1024 ``` #### 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.format(1024); // output: '1KB' bytes.format(1000); // output: '1000B' bytes.format(1000, {thousandsSeparator: ' '}); // output: '1 000B' bytes.format(1024 * 1.7, {decimalPlaces: 0}); // output: '2KB' bytes.format(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.parse('1KB'); // output: 1024 bytes.parse('1024'); // output: 1024 bytes.parse(1024); // output: 1024 ``` ## License [MIT](LICENSE) [ci-image]: https://badgen.net/github/checks/visionmedia/bytes.js/master?label=ci [ci-url]: https://github.com/visionmedia/bytes.js/actions?query=workflow%3Aci [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/v/bytes [npm-url]: https://npmjs.org/package/bytes bytes.js-3.1.2/index.js000066400000000000000000000070351417467354600147510ustar00rootroot00000000000000/*! * 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.split('.').map(function (s, i) { return i === 0 ? s.replace(formatThousandsRegExp, thousandsSeparator) : s }).join('.'); } 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(); } if (isNaN(floatValue)) { return null; } return Math.floor(map[unit] * floatValue); } bytes.js-3.1.2/package.json000066400000000000000000000016771417467354600156000ustar00rootroot00000000000000{ "name": "bytes", "description": "Utility to parse a string bytes to bytes and vice-versa", "version": "3.1.2", "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": "7.32.0", "eslint-plugin-markdown": "2.2.1", "mocha": "9.2.0", "nyc": "15.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=lcov --reporter=text npm test", "test-cov": "nyc --reporter=html --reporter=text npm test" } } bytes.js-3.1.2/test/000077500000000000000000000000001417467354600142565ustar00rootroot00000000000000bytes.js-3.1.2/test/.eslintrc.yml000066400000000000000000000000231417467354600166750ustar00rootroot00000000000000env: mocha: true bytes.js-3.1.2/test/byte-format.js000066400000000000000000000126001417467354600170440ustar00rootroot00000000000000'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'); assert.equal(bytes.format(1005.1005 * kb, {decimalPlaces: 4, thousandsSeparator: '_'}).toLowerCase(), '1_005.1005kb'); }); 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'); assert.equal(bytes.format(1.1005 * kb, {decimalPlaces: 4}).toLowerCase(), '1.1005kb'); }); 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.2/test/byte-parse.js000066400000000000000000000106611417467354600166730ustar00rootroot00000000000000'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); assert.strictEqual(bytes.parse('foobar'), 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.2/test/bytes.js000066400000000000000000000022721417467354600157450ustar00rootroot00000000000000'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); assert.strictEqual(bytes('foobar'), 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'); }); });