|
3 | 3 | *
|
4 | 4 | * @copyright 2022 Jason Mulligan <[email protected]>
|
5 | 5 | * @license BSD-3-Clause
|
6 |
| - * @version 9.0.0 |
| 6 | + * @version 9.0.1 |
7 | 7 | */
|
8 | 8 | (function (global, factory) {
|
9 | 9 | typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
|
10 | 10 | typeof define === 'function' && define.amd ? define(factory) :
|
11 | 11 | (global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.filesize = factory());
|
12 | 12 | })(this, (function () { 'use strict';
|
13 | 13 |
|
14 |
| - const symbol = { |
15 |
| - iec: { |
16 |
| - bits: ["bit", "Kibit", "Mibit", "Gibit", "Tibit", "Pibit", "Eibit", "Zibit", "Yibit"], |
17 |
| - bytes: ["B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB"] |
| 14 | + const strings = { |
| 15 | + symbol: { |
| 16 | + iec: { |
| 17 | + bits: ["bit", "Kibit", "Mibit", "Gibit", "Tibit", "Pibit", "Eibit", "Zibit", "Yibit"], |
| 18 | + bytes: ["B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB"] |
| 19 | + }, |
| 20 | + jedec: { |
| 21 | + bits: ["bit", "Kbit", "Mbit", "Gbit", "Tbit", "Pbit", "Ebit", "Zbit", "Ybit"], |
| 22 | + bytes: ["B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"] |
| 23 | + } |
18 | 24 | },
|
19 |
| - jedec: { |
20 |
| - bits: ["bit", "Kbit", "Mbit", "Gbit", "Tbit", "Pbit", "Ebit", "Zbit", "Ybit"], |
21 |
| - bytes: ["B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"] |
| 25 | + fullform: { |
| 26 | + iec: ["", "kibi", "mebi", "gibi", "tebi", "pebi", "exbi", "zebi", "yobi"], |
| 27 | + jedec: ["", "kilo", "mega", "giga", "tera", "peta", "exa", "zetta", "yotta"] |
22 | 28 | }
|
23 | 29 | },
|
24 |
| - fullform = { |
25 |
| - iec: ["", "kibi", "mebi", "gibi", "tebi", "pebi", "exbi", "zebi", "yobi"], |
26 |
| - jedec: ["", "kilo", "mega", "giga", "tera", "peta", "exa", "zetta", "yotta"] |
27 |
| - }, |
28 | 30 | roundingFuncs = {
|
| 31 | + ceil: Math.ceil, |
29 | 32 | floor: Math.floor,
|
30 |
| - ceil: Math.ceil |
| 33 | + round: Math.round |
31 | 34 | };
|
32 | 35 |
|
33 | 36 | /**
|
|
38 | 41 | * @param {Object} descriptor [Optional] Flags
|
39 | 42 | * @return {String} Readable file size String
|
40 | 43 | */
|
41 |
| - function filesize (arg, descriptor = {}) { |
42 |
| - let result = [], |
| 44 | + function filesize (arg, {bits = false, pad = false, base = 10, round = 2, locale = "", localeOptions = {}, separator = "", spacer = " ", symbols = {}, standard = "iec", output = "string", fullform = false, fullforms = [], exponent = -1, roundingMethod = "round", precision = 0} = {}) { |
| 45 | + let e = exponent, |
| 46 | + num = Number(arg), |
| 47 | + result = [], |
43 | 48 | val = 0,
|
44 |
| - e, base, bits, ceil, full, fullforms, locale, localeOptions, neg, num, output, pad, round, u, separator, spacer, standard, symbols, roundingFunc, precision; |
| 49 | + u = ""; |
| 50 | + const ceil = base === 10 ? 1000 : 1024, |
| 51 | + full = fullform === true, |
| 52 | + neg = num < 0, |
| 53 | + roundingFunc = roundingFuncs[roundingMethod]; |
45 | 54 |
|
46 | 55 | if (isNaN(arg)) {
|
47 | 56 | throw new TypeError("Invalid number");
|
48 | 57 | }
|
49 | 58 |
|
50 |
| - bits = descriptor.bits === true; |
51 |
| - pad = descriptor.pad === true; |
52 |
| - base = descriptor.base || 10; |
53 |
| - round = descriptor.round !== void 0 ? descriptor.round : 2; |
54 |
| - locale = descriptor.locale !== void 0 ? descriptor.locale : ""; |
55 |
| - localeOptions = descriptor.localeOptions || {}; |
56 |
| - separator = descriptor.separator !== void 0 ? descriptor.separator : ""; |
57 |
| - spacer = descriptor.spacer !== void 0 ? descriptor.spacer : " "; |
58 |
| - symbols = descriptor.symbols || {}; |
59 |
| - standard = descriptor.standard in symbol ? descriptor.standard : "iec"; |
60 |
| - output = descriptor.output || "string"; |
61 |
| - full = descriptor.fullform === true; |
62 |
| - fullforms = descriptor.fullforms instanceof Array ? descriptor.fullforms : []; |
63 |
| - e = descriptor.exponent !== void 0 ? descriptor.exponent : -1; |
64 |
| - roundingFunc = roundingFuncs[descriptor.roundingMethod] || Math.round; |
65 |
| - num = Number(arg); |
66 |
| - neg = num < 0; |
67 |
| - ceil = base > 2 ? 1000 : 1024; |
68 |
| - precision = isNaN(descriptor.precision) === false ? parseInt(descriptor.precision, 10) : 0; |
69 |
| - |
70 | 59 | // Flipping a negative number to determine the size
|
71 | 60 | if (neg) {
|
72 | 61 | num = -num;
|
|
97 | 86 | // Zero is now a special case because bytes divide by 1
|
98 | 87 | if (num === 0) {
|
99 | 88 | result[0] = 0;
|
100 |
| - u = result[1] = symbol[standard][bits ? "bits" : "bytes"][e]; |
| 89 | + u = result[1] = strings.symbol[standard][bits ? "bits" : "bytes"][e]; |
101 | 90 | } else {
|
102 | 91 | val = num / (base === 2 ? Math.pow(2, e * 10) : Math.pow(1000, e));
|
103 | 92 |
|
|
113 | 102 | const p = Math.pow(10, e > 0 ? round : 0);
|
114 | 103 | result[0] = roundingFunc(val * p) / p;
|
115 | 104 |
|
116 |
| - if (result[0] === ceil && e < 8 && descriptor.exponent === void 0) { |
| 105 | + if (result[0] === ceil && e < 8 && exponent === -1) { |
117 | 106 | result[0] = 1;
|
118 | 107 | e++;
|
119 | 108 | }
|
120 | 109 |
|
121 |
| - u = result[1] = symbol[standard][bits ? "bits" : "bytes"][e]; |
| 110 | + u = result[1] = strings.symbol[standard][bits ? "bits" : "bytes"][e]; |
122 | 111 | }
|
123 | 112 |
|
124 | 113 | // Decorating a 'diff'
|
|
153 | 142 | }
|
154 | 143 |
|
155 | 144 | if (full) {
|
156 |
| - result[1] = fullforms[e] ? fullforms[e] : fullform[standard][e] + (bits ? "bit" : "byte") + (result[0] === 1 ? "" : "s"); |
| 145 | + result[1] = fullforms[e] ? fullforms[e] : strings.fullform[standard][e] + (bits ? "bit" : "byte") + (result[0] === 1 ? "" : "s"); |
157 | 146 | }
|
158 | 147 |
|
159 | 148 | // Returning Array, Object, or String (default)
|
|
0 commit comments