Comparing version
@@ -14,2 +14,49 @@ (function (global, factory) { | ||
const referenceTables = { | ||
metric: [ | ||
{ from: 0, to: 1e3, unit: 'B', long: 'bytes' }, | ||
{ from: 1e3, to: 1e6, unit: 'kB', long: 'kilobytes' }, | ||
{ from: 1e6, to: 1e9, unit: 'MB', long: 'megabytes' }, | ||
{ from: 1e9, to: 1e12, unit: 'GB', long: 'gigabytes' }, | ||
{ from: 1e12, to: 1e15, unit: 'TB', long: 'terabytes' }, | ||
{ from: 1e15, to: 1e18, unit: 'PB', long: 'petabytes' }, | ||
{ from: 1e18, to: 1e21, unit: 'EB', long: 'exabytes' }, | ||
{ from: 1e21, to: 1e24, unit: 'ZB', long: 'zettabytes' }, | ||
{ from: 1e24, to: 1e27, unit: 'YB', long: 'yottabytes' } | ||
], | ||
metric_octet: [ | ||
{ from: 0, to: 1e3, unit: 'o', long: 'octets' }, | ||
{ from: 1e3, to: 1e6, unit: 'ko', long: 'kilooctets' }, | ||
{ from: 1e6, to: 1e9, unit: 'Mo', long: 'megaoctets' }, | ||
{ from: 1e9, to: 1e12, unit: 'Go', long: 'gigaoctets' }, | ||
{ from: 1e12, to: 1e15, unit: 'To', long: 'teraoctets' }, | ||
{ from: 1e15, to: 1e18, unit: 'Po', long: 'petaoctets' }, | ||
{ from: 1e18, to: 1e21, unit: 'Eo', long: 'exaoctets' }, | ||
{ from: 1e21, to: 1e24, unit: 'Zo', long: 'zettaoctets' }, | ||
{ from: 1e24, to: 1e27, unit: 'Yo', long: 'yottaoctets' } | ||
], | ||
iec: [ | ||
{ from: 0, to: Math.pow(1024, 1), unit: 'B', long: 'bytes' }, | ||
{ from: Math.pow(1024, 1), to: Math.pow(1024, 2), unit: 'KiB', long: 'kibibytes' }, | ||
{ from: Math.pow(1024, 2), to: Math.pow(1024, 3), unit: 'MiB', long: 'mebibytes' }, | ||
{ from: Math.pow(1024, 3), to: Math.pow(1024, 4), unit: 'GiB', long: 'gibibytes' }, | ||
{ from: Math.pow(1024, 4), to: Math.pow(1024, 5), unit: 'TiB', long: 'tebibytes' }, | ||
{ from: Math.pow(1024, 5), to: Math.pow(1024, 6), unit: 'PiB', long: 'pebibytes' }, | ||
{ from: Math.pow(1024, 6), to: Math.pow(1024, 7), unit: 'EiB', long: 'exbibytes' }, | ||
{ from: Math.pow(1024, 7), to: Math.pow(1024, 8), unit: 'ZiB', long: 'zebibytes' }, | ||
{ from: Math.pow(1024, 8), to: Math.pow(1024, 9), unit: 'YiB', long: 'yobibytes' } | ||
], | ||
iec_octet: [ | ||
{ from: 0, to: Math.pow(1024, 1), unit: 'o', long: 'octets' }, | ||
{ from: Math.pow(1024, 1), to: Math.pow(1024, 2), unit: 'Kio', long: 'kibioctets' }, | ||
{ from: Math.pow(1024, 2), to: Math.pow(1024, 3), unit: 'Mio', long: 'mebioctets' }, | ||
{ from: Math.pow(1024, 3), to: Math.pow(1024, 4), unit: 'Gio', long: 'gibioctets' }, | ||
{ from: Math.pow(1024, 4), to: Math.pow(1024, 5), unit: 'Tio', long: 'tebioctets' }, | ||
{ from: Math.pow(1024, 5), to: Math.pow(1024, 6), unit: 'Pio', long: 'pebioctets' }, | ||
{ from: Math.pow(1024, 6), to: Math.pow(1024, 7), unit: 'Eio', long: 'exbioctets' }, | ||
{ from: Math.pow(1024, 7), to: Math.pow(1024, 8), unit: 'Zio', long: 'zebioctets' }, | ||
{ from: Math.pow(1024, 8), to: Math.pow(1024, 9), unit: 'Yio', long: 'yobioctets' } | ||
] | ||
}; | ||
class ByteSize { | ||
@@ -19,63 +66,23 @@ constructor (bytes, options) { | ||
units: 'metric', | ||
precision: 1 | ||
precision: 1, | ||
locale: undefined // Default to the user's system locale | ||
}, defaultOptions, options); | ||
_options.set(this, options); | ||
const tables = { | ||
metric: [ | ||
{ from: 0 , to: 1e3 , unit: 'B' , long: 'bytes' }, | ||
{ from: 1e3 , to: 1e6 , unit: 'kB', long: 'kilobytes' }, | ||
{ from: 1e6 , to: 1e9 , unit: 'MB', long: 'megabytes' }, | ||
{ from: 1e9 , to: 1e12, unit: 'GB', long: 'gigabytes' }, | ||
{ from: 1e12, to: 1e15, unit: 'TB', long: 'terabytes' }, | ||
{ from: 1e15, to: 1e18, unit: 'PB', long: 'petabytes' }, | ||
{ from: 1e18, to: 1e21, unit: 'EB', long: 'exabytes' }, | ||
{ from: 1e21, to: 1e24, unit: 'ZB', long: 'zettabytes' }, | ||
{ from: 1e24, to: 1e27, unit: 'YB', long: 'yottabytes' }, | ||
], | ||
metric_octet: [ | ||
{ from: 0 , to: 1e3 , unit: 'o' , long: 'octets' }, | ||
{ from: 1e3 , to: 1e6 , unit: 'ko', long: 'kilooctets' }, | ||
{ from: 1e6 , to: 1e9 , unit: 'Mo', long: 'megaoctets' }, | ||
{ from: 1e9 , to: 1e12, unit: 'Go', long: 'gigaoctets' }, | ||
{ from: 1e12, to: 1e15, unit: 'To', long: 'teraoctets' }, | ||
{ from: 1e15, to: 1e18, unit: 'Po', long: 'petaoctets' }, | ||
{ from: 1e18, to: 1e21, unit: 'Eo', long: 'exaoctets' }, | ||
{ from: 1e21, to: 1e24, unit: 'Zo', long: 'zettaoctets' }, | ||
{ from: 1e24, to: 1e27, unit: 'Yo', long: 'yottaoctets' }, | ||
], | ||
iec: [ | ||
{ from: 0 , to: Math.pow(1024, 1), unit: 'B' , long: 'bytes' }, | ||
{ from: Math.pow(1024, 1), to: Math.pow(1024, 2), unit: 'KiB', long: 'kibibytes' }, | ||
{ from: Math.pow(1024, 2), to: Math.pow(1024, 3), unit: 'MiB', long: 'mebibytes' }, | ||
{ from: Math.pow(1024, 3), to: Math.pow(1024, 4), unit: 'GiB', long: 'gibibytes' }, | ||
{ from: Math.pow(1024, 4), to: Math.pow(1024, 5), unit: 'TiB', long: 'tebibytes' }, | ||
{ from: Math.pow(1024, 5), to: Math.pow(1024, 6), unit: 'PiB', long: 'pebibytes' }, | ||
{ from: Math.pow(1024, 6), to: Math.pow(1024, 7), unit: 'EiB', long: 'exbibytes' }, | ||
{ from: Math.pow(1024, 7), to: Math.pow(1024, 8), unit: 'ZiB', long: 'zebibytes' }, | ||
{ from: Math.pow(1024, 8), to: Math.pow(1024, 9), unit: 'YiB', long: 'yobibytes' }, | ||
], | ||
iec_octet: [ | ||
{ from: 0 , to: Math.pow(1024, 1), unit: 'o' , long: 'octets' }, | ||
{ from: Math.pow(1024, 1), to: Math.pow(1024, 2), unit: 'Kio', long: 'kibioctets' }, | ||
{ from: Math.pow(1024, 2), to: Math.pow(1024, 3), unit: 'Mio', long: 'mebioctets' }, | ||
{ from: Math.pow(1024, 3), to: Math.pow(1024, 4), unit: 'Gio', long: 'gibioctets' }, | ||
{ from: Math.pow(1024, 4), to: Math.pow(1024, 5), unit: 'Tio', long: 'tebioctets' }, | ||
{ from: Math.pow(1024, 5), to: Math.pow(1024, 6), unit: 'Pio', long: 'pebioctets' }, | ||
{ from: Math.pow(1024, 6), to: Math.pow(1024, 7), unit: 'Eio', long: 'exbioctets' }, | ||
{ from: Math.pow(1024, 7), to: Math.pow(1024, 8), unit: 'Zio', long: 'zebioctets' }, | ||
{ from: Math.pow(1024, 8), to: Math.pow(1024, 9), unit: 'Yio', long: 'yobioctets' }, | ||
], | ||
}; | ||
Object.assign(tables, options.customUnits); | ||
Object.assign(referenceTables, options.customUnits); | ||
const prefix = bytes < 0 ? '-' : ''; | ||
bytes = Math.abs(bytes); | ||
const table = tables[options.units]; | ||
const table = referenceTables[options.units]; | ||
if (table) { | ||
const units = table.find(u => bytes >= u.from && bytes < u.to); | ||
if (units) { | ||
const defaultFormat = new Intl.NumberFormat(options.locale, { | ||
style: 'decimal', | ||
minimumFractionDigits: options.precision, | ||
maximumFractionDigits: options.precision | ||
}); | ||
const value = units.from === 0 | ||
? prefix + bytes | ||
: prefix + (bytes / units.from).toFixed(options.precision); | ||
: prefix + defaultFormat.format(bytes / units.from); | ||
this.value = value; | ||
@@ -82,0 +89,0 @@ this.unit = units.unit; |
{ | ||
"name": "byte-size", | ||
"author": "Lloyd Brookes <75pound@gmail.com>", | ||
"contributors": [ | ||
{ | ||
"name": "Raul Perez", | ||
"email": "repejota@gmail.com", | ||
"url": "http://repejota.com" | ||
} | ||
], | ||
"version": "7.0.1", | ||
"main": "dist/index.js", | ||
"version": "8.0.0", | ||
"description": "Convert a bytes or octets value (e.g. 34565346) to a human-readable string ('34.6 MB'). Choose between metric or IEC units.", | ||
"repository": "https://github.com/75lb/byte-size", | ||
"exports": { | ||
"import": "./index.mjs", | ||
"require": "./dist/index.cjs" | ||
}, | ||
"license": "MIT", | ||
"engines": { | ||
"node": ">=10" | ||
"node": ">=14" | ||
}, | ||
"description": "Convert a bytes or octets value (e.g. 34565346) to a human-readable string ('34.6 MB'). Choose between metric or IEC units.", | ||
"repository": "https://github.com/75lb/byte-size", | ||
"files": [ | ||
"index.mjs", | ||
"dist/index.js" | ||
"dist" | ||
], | ||
@@ -35,16 +31,16 @@ "keywords": [ | ||
"test": "npm run dist && npm run test:esm && npm run test:web", | ||
"test:esm": "esm-runner test.mjs", | ||
"test:esm": "test-runner --view.hide-skips test.mjs", | ||
"test:web": "web-runner test.mjs", | ||
"docs": "jsdoc2md -t README.hbs dist/index.js > README.md", | ||
"docs": "jsdoc2md -c jsdoc.conf -t README.hbs index.mjs > README.md", | ||
"cover": "c8 npm test && c8 report --reporter=text-lcov | coveralls", | ||
"dist": "rollup -f umd -n byteSize -o dist/index.js index.mjs" | ||
"dist": "rollup -c" | ||
}, | ||
"devDependencies": { | ||
"@test-runner/web": "^0.3.5", | ||
"coveralls": "^3.1.0", | ||
"esm-runner": "^0.3.4", | ||
"isomorphic-assert": "^0.1.1", | ||
"jsdoc-to-markdown": "^7.0.0", | ||
"rollup": "^2.40.0", | ||
"test-object-model": "^0.6.1" | ||
"@rollup/plugin-node-resolve": "^13.0.0", | ||
"@test-runner/web": "^0.4.1", | ||
"coveralls": "^3.1.1", | ||
"isomorphic-assert": "^0.2.0", | ||
"jsdoc-to-markdown": "^7.0.1", | ||
"rollup": "^2.52.7", | ||
"test-runner": "^0.9.6" | ||
}, | ||
@@ -55,3 +51,10 @@ "standard": { | ||
] | ||
} | ||
}, | ||
"contributors": [ | ||
{ | ||
"name": "Raul Perez", | ||
"email": "repejota@gmail.com", | ||
"url": "http://repejota.com" | ||
} | ||
] | ||
} |
@@ -5,3 +5,3 @@ [](https://www.npmjs.org/package/byte-size) | ||
[](https://github.com/75lb/byte-size/network/dependents?dependent_type=PACKAGE) | ||
[](https://travis-ci.org/75lb/byte-size) | ||
[](https://github.com/75lb/byte-size/actions/workflows/node.js.yml) | ||
[](https://coveralls.io/github/75lb/byte-size) | ||
@@ -154,2 +154,3 @@ [](https://github.com/feross/standard) | ||
| [options.toStringFn] | <code>function</code> | A `toString` function to override the default. | | ||
| [options.locale] | <code>string</code> \| <code>Array.<string></code> | The locale to use for number formatting (e.g. `'de-DE'`). Defaults to your system locale. Passed directed into [Intl.NumberFormat()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat/NumberFormat). | | ||
@@ -172,3 +173,3 @@ <a name="module_byte-size--byteSize.defaultOptions"></a> | ||
Node.js: | ||
Node.js (CommonJS): | ||
@@ -179,3 +180,3 @@ ```js | ||
Within Node.js with ECMAScript Module support enabled: | ||
Node.js (ECMAScript Module): | ||
@@ -192,6 +193,6 @@ ```js | ||
Old browser (adds `window.byteSize`): | ||
Browser global (adds `window.byteSize`): | ||
```html | ||
<script nomodule src="./node_modules/byte-size/dist/index.js"></script> | ||
<script src="./node_modules/byte-size/dist/index.js"></script> | ||
``` | ||
@@ -198,0 +199,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
32123
23.54%7
16.67%348
58.18%200
0.5%