|
5 | 5 | *
|
6 | 6 | * @copyright 2019 Jason Mulligan <[email protected]>
|
7 | 7 | * @license BSD-3-Clause
|
8 |
| - * @version 4.1.2 |
| 8 | + * @version 4.2.0 |
9 | 9 | */
|
10 | 10 | (function (global) {
|
11 |
| - var b = /^(b|B)$/, |
12 |
| - symbol = { |
13 |
| - iec: { |
14 |
| - bits: ["b", "Kib", "Mib", "Gib", "Tib", "Pib", "Eib", "Zib", "Yib"], |
15 |
| - bytes: ["B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB"] |
16 |
| - }, |
17 |
| - jedec: { |
18 |
| - bits: ["b", "Kb", "Mb", "Gb", "Tb", "Pb", "Eb", "Zb", "Yb"], |
19 |
| - bytes: ["B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"] |
20 |
| - } |
21 |
| - }, |
22 |
| - fullform = { |
23 |
| - iec: ["", "kibi", "mebi", "gibi", "tebi", "pebi", "exbi", "zebi", "yobi"], |
24 |
| - jedec: ["", "kilo", "mega", "giga", "tera", "peta", "exa", "zetta", "yotta"] |
25 |
| - }; |
26 |
| - |
27 |
| - /** |
28 |
| - * filesize |
29 |
| - * |
30 |
| - * @method filesize |
31 |
| - * @param {Mixed} arg String, Int or Float to transform |
32 |
| - * @param {Object} descriptor [Optional] Flags |
33 |
| - * @return {String} Readable file size String |
34 |
| - */ |
35 |
| - function filesize(arg) { |
36 |
| - var descriptor = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; |
37 |
| - |
38 |
| - var result = [], |
39 |
| - val = 0, |
40 |
| - e = void 0, |
41 |
| - base = void 0, |
42 |
| - bits = void 0, |
43 |
| - ceil = void 0, |
44 |
| - full = void 0, |
45 |
| - fullforms = void 0, |
46 |
| - locale = void 0, |
47 |
| - localeOptions = void 0, |
48 |
| - neg = void 0, |
49 |
| - num = void 0, |
50 |
| - output = void 0, |
51 |
| - round = void 0, |
52 |
| - unix = void 0, |
53 |
| - separator = void 0, |
54 |
| - spacer = void 0, |
55 |
| - standard = void 0, |
56 |
| - symbols = void 0; |
57 |
| - |
58 |
| - if (isNaN(arg)) { |
59 |
| - throw new TypeError("Invalid number"); |
60 |
| - } |
61 |
| - |
62 |
| - bits = descriptor.bits === true; |
63 |
| - unix = descriptor.unix === true; |
64 |
| - base = descriptor.base || 2; |
65 |
| - round = descriptor.round !== void 0 ? descriptor.round : unix ? 1 : 2; |
66 |
| - locale = descriptor.locale !== void 0 ? descriptor.locale : ""; |
67 |
| - localeOptions = descriptor.localeOptions || {}; |
68 |
| - separator = descriptor.separator !== void 0 ? descriptor.separator : ""; |
69 |
| - spacer = descriptor.spacer !== void 0 ? descriptor.spacer : unix ? "" : " "; |
70 |
| - symbols = descriptor.symbols || {}; |
71 |
| - standard = base === 2 ? descriptor.standard || "jedec" : "jedec"; |
72 |
| - output = descriptor.output || "string"; |
73 |
| - full = descriptor.fullform === true; |
74 |
| - fullforms = descriptor.fullforms instanceof Array ? descriptor.fullforms : []; |
75 |
| - e = descriptor.exponent !== void 0 ? descriptor.exponent : -1; |
76 |
| - num = Number(arg); |
77 |
| - neg = num < 0; |
78 |
| - ceil = base > 2 ? 1000 : 1024; |
79 |
| - |
80 |
| - // Flipping a negative number to determine the size |
81 |
| - if (neg) { |
82 |
| - num = -num; |
83 |
| - } |
84 |
| - |
85 |
| - // Determining the exponent |
86 |
| - if (e === -1 || isNaN(e)) { |
87 |
| - e = Math.floor(Math.log(num) / Math.log(ceil)); |
88 |
| - |
89 |
| - if (e < 0) { |
90 |
| - e = 0; |
91 |
| - } |
92 |
| - } |
93 |
| - |
94 |
| - // Exceeding supported length, time to reduce & multiply |
95 |
| - if (e > 8) { |
96 |
| - e = 8; |
97 |
| - } |
98 |
| - |
99 |
| - if (output === "exponent") { |
100 |
| - return e; |
101 |
| - } |
102 |
| - |
103 |
| - // Zero is now a special case because bytes divide by 1 |
104 |
| - if (num === 0) { |
105 |
| - result[0] = 0; |
106 |
| - result[1] = unix ? "" : symbol[standard][bits ? "bits" : "bytes"][e]; |
107 |
| - } else { |
108 |
| - val = num / (base === 2 ? Math.pow(2, e * 10) : Math.pow(1000, e)); |
109 |
| - |
110 |
| - if (bits) { |
111 |
| - val = val * 8; |
112 |
| - |
113 |
| - if (val >= ceil && e < 8) { |
114 |
| - val = val / ceil; |
115 |
| - e++; |
116 |
| - } |
117 |
| - } |
118 |
| - |
119 |
| - result[0] = Number(val.toFixed(e > 0 ? round : 0)); |
120 |
| - result[1] = base === 10 && e === 1 ? bits ? "kb" : "kB" : symbol[standard][bits ? "bits" : "bytes"][e]; |
121 |
| - |
122 |
| - if (unix) { |
123 |
| - result[1] = standard === "jedec" ? result[1].charAt(0) : e > 0 ? result[1].replace(/B$/, "") : result[1]; |
124 |
| - |
125 |
| - if (b.test(result[1])) { |
126 |
| - result[0] = Math.floor(result[0]); |
127 |
| - result[1] = ""; |
128 |
| - } |
129 |
| - } |
130 |
| - } |
131 |
| - |
132 |
| - // Decorating a 'diff' |
133 |
| - if (neg) { |
134 |
| - result[0] = -result[0]; |
135 |
| - } |
136 |
| - |
137 |
| - // Applying custom symbol |
138 |
| - result[1] = symbols[result[1]] || result[1]; |
139 |
| - |
140 |
| - if (locale === true) { |
141 |
| - result[0] = result[0].toLocaleString(); |
142 |
| - } else if (locale.length > 0) { |
143 |
| - result[0] = result[0].toLocaleString(locale, localeOptions); |
144 |
| - } else if (separator.length > 0) { |
145 |
| - result[0] = result[0].toString().replace(".", separator); |
146 |
| - } |
147 |
| - |
148 |
| - // Returning Array, Object, or String (default) |
149 |
| - if (output === "array") { |
150 |
| - return result; |
151 |
| - } |
152 |
| - |
153 |
| - if (full) { |
154 |
| - result[1] = fullforms[e] ? fullforms[e] : fullform[standard][e] + (bits ? "bit" : "byte") + (result[0] === 1 ? "" : "s"); |
155 |
| - } |
156 |
| - |
157 |
| - if (output === "object") { |
158 |
| - return { value: result[0], symbol: result[1] }; |
159 |
| - } |
160 |
| - |
161 |
| - return result.join(spacer); |
162 |
| - } |
163 |
| - |
164 |
| - // Partial application for functional programming |
165 |
| - filesize.partial = function (opt) { |
166 |
| - return function (arg) { |
167 |
| - return filesize(arg, opt); |
168 |
| - }; |
169 |
| - }; |
170 |
| - |
171 |
| - // CommonJS, AMD, script tag |
172 |
| - if (typeof exports !== "undefined") { |
173 |
| - module.exports = filesize; |
174 |
| - } else if (typeof define === "function" && define.amd !== void 0) { |
175 |
| - define(function () { |
176 |
| - return filesize; |
177 |
| - }); |
178 |
| - } else { |
179 |
| - global.filesize = filesize; |
180 |
| - } |
| 11 | + var b = /^(b|B)$/, |
| 12 | + symbol = { |
| 13 | + iec: { |
| 14 | + bits: ["b", "Kib", "Mib", "Gib", "Tib", "Pib", "Eib", "Zib", "Yib"], |
| 15 | + bytes: ["B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB"] |
| 16 | + }, |
| 17 | + jedec: { |
| 18 | + bits: ["b", "Kb", "Mb", "Gb", "Tb", "Pb", "Eb", "Zb", "Yb"], |
| 19 | + bytes: ["B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"] |
| 20 | + } |
| 21 | + }, |
| 22 | + fullform = { |
| 23 | + iec: ["", "kibi", "mebi", "gibi", "tebi", "pebi", "exbi", "zebi", "yobi"], |
| 24 | + jedec: ["", "kilo", "mega", "giga", "tera", "peta", "exa", "zetta", "yotta"] |
| 25 | + }; |
| 26 | + /** |
| 27 | + * filesize |
| 28 | + * |
| 29 | + * @method filesize |
| 30 | + * @param {Mixed} arg String, Int or Float to transform |
| 31 | + * @param {Object} descriptor [Optional] Flags |
| 32 | + * @return {String} Readable file size String |
| 33 | + */ |
| 34 | + |
| 35 | + function filesize(arg) { |
| 36 | + var descriptor = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; |
| 37 | + var result = [], |
| 38 | + val = 0, |
| 39 | + e = void 0, |
| 40 | + base = void 0, |
| 41 | + bits = void 0, |
| 42 | + ceil = void 0, |
| 43 | + full = void 0, |
| 44 | + fullforms = void 0, |
| 45 | + locale = void 0, |
| 46 | + localeOptions = void 0, |
| 47 | + neg = void 0, |
| 48 | + num = void 0, |
| 49 | + output = void 0, |
| 50 | + round = void 0, |
| 51 | + unix = void 0, |
| 52 | + separator = void 0, |
| 53 | + spacer = void 0, |
| 54 | + standard = void 0, |
| 55 | + symbols = void 0; |
| 56 | + |
| 57 | + if (isNaN(arg)) { |
| 58 | + throw new TypeError("Invalid number"); |
| 59 | + } |
| 60 | + |
| 61 | + bits = descriptor.bits === true; |
| 62 | + unix = descriptor.unix === true; |
| 63 | + base = descriptor.base || 2; |
| 64 | + round = descriptor.round !== void 0 ? descriptor.round : unix ? 1 : 2; |
| 65 | + locale = descriptor.locale !== void 0 ? descriptor.locale : ""; |
| 66 | + localeOptions = descriptor.localeOptions || {}; |
| 67 | + separator = descriptor.separator !== void 0 ? descriptor.separator : ""; |
| 68 | + spacer = descriptor.spacer !== void 0 ? descriptor.spacer : unix ? "" : " "; |
| 69 | + symbols = descriptor.symbols || {}; |
| 70 | + standard = base === 2 ? descriptor.standard || "jedec" : "jedec"; |
| 71 | + output = descriptor.output || "string"; |
| 72 | + full = descriptor.fullform === true; |
| 73 | + fullforms = descriptor.fullforms instanceof Array ? descriptor.fullforms : []; |
| 74 | + e = descriptor.exponent !== void 0 ? descriptor.exponent : -1; |
| 75 | + num = Number(arg); |
| 76 | + neg = num < 0; |
| 77 | + ceil = base > 2 ? 1000 : 1024; // Flipping a negative number to determine the size |
| 78 | + |
| 79 | + if (neg) { |
| 80 | + num = -num; |
| 81 | + } // Determining the exponent |
| 82 | + |
| 83 | + |
| 84 | + if (e === -1 || isNaN(e)) { |
| 85 | + e = Math.floor(Math.log(num) / Math.log(ceil)); |
| 86 | + |
| 87 | + if (e < 0) { |
| 88 | + e = 0; |
| 89 | + } |
| 90 | + } // Exceeding supported length, time to reduce & multiply |
| 91 | + |
| 92 | + |
| 93 | + if (e > 8) { |
| 94 | + e = 8; |
| 95 | + } |
| 96 | + |
| 97 | + if (output === "exponent") { |
| 98 | + return e; |
| 99 | + } // Zero is now a special case because bytes divide by 1 |
| 100 | + |
| 101 | + |
| 102 | + if (num === 0) { |
| 103 | + result[0] = 0; |
| 104 | + result[1] = unix ? "" : symbol[standard][bits ? "bits" : "bytes"][e]; |
| 105 | + } else { |
| 106 | + val = num / (base === 2 ? Math.pow(2, e * 10) : Math.pow(1000, e)); |
| 107 | + |
| 108 | + if (bits) { |
| 109 | + val = val * 8; |
| 110 | + |
| 111 | + if (val >= ceil && e < 8) { |
| 112 | + val = val / ceil; |
| 113 | + e++; |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + result[0] = Number(val.toFixed(e > 0 ? round : 0)); |
| 118 | + result[1] = base === 10 && e === 1 ? bits ? "kb" : "kB" : symbol[standard][bits ? "bits" : "bytes"][e]; |
| 119 | + |
| 120 | + if (unix) { |
| 121 | + result[1] = standard === "jedec" ? result[1].charAt(0) : e > 0 ? result[1].replace(/B$/, "") : result[1]; |
| 122 | + |
| 123 | + if (b.test(result[1])) { |
| 124 | + result[0] = Math.floor(result[0]); |
| 125 | + result[1] = ""; |
| 126 | + } |
| 127 | + } |
| 128 | + } // Decorating a 'diff' |
| 129 | + |
| 130 | + |
| 131 | + if (neg) { |
| 132 | + result[0] = -result[0]; |
| 133 | + } // Applying custom symbol |
| 134 | + |
| 135 | + |
| 136 | + result[1] = symbols[result[1]] || result[1]; |
| 137 | + |
| 138 | + if (locale === true) { |
| 139 | + result[0] = result[0].toLocaleString(); |
| 140 | + } else if (locale.length > 0) { |
| 141 | + result[0] = result[0].toLocaleString(locale, localeOptions); |
| 142 | + } else if (separator.length > 0) { |
| 143 | + result[0] = result[0].toString().replace(".", separator); |
| 144 | + } // Returning Array, Object, or String (default) |
| 145 | + |
| 146 | + |
| 147 | + if (output === "array") { |
| 148 | + return result; |
| 149 | + } |
| 150 | + |
| 151 | + if (full) { |
| 152 | + result[1] = fullforms[e] ? fullforms[e] : fullform[standard][e] + (bits ? "bit" : "byte") + (result[0] === 1 ? "" : "s"); |
| 153 | + } |
| 154 | + |
| 155 | + if (output === "object") { |
| 156 | + return { |
| 157 | + value: result[0], |
| 158 | + symbol: result[1] |
| 159 | + }; |
| 160 | + } |
| 161 | + |
| 162 | + return result.join(spacer); |
| 163 | + } // Partial application for functional programming |
| 164 | + |
| 165 | + |
| 166 | + filesize.partial = function (opt) { |
| 167 | + return function (arg) { |
| 168 | + return filesize(arg, opt); |
| 169 | + }; |
| 170 | + }; // CommonJS, AMD, script tag |
| 171 | + |
| 172 | + |
| 173 | + if (typeof exports !== "undefined") { |
| 174 | + module.exports = filesize; |
| 175 | + } else if (typeof define === "function" && define.amd !== void 0) { |
| 176 | + define(function () { |
| 177 | + return filesize; |
| 178 | + }); |
| 179 | + } else { |
| 180 | + global.filesize = filesize; |
| 181 | + } |
181 | 182 | })(typeof window !== "undefined" ? window : global);
|
0 commit comments