1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 | (function (global) {
|
9 | const b = /^(b|B)$/,
|
10 | symbol = {
|
11 | iec: {
|
12 | bits: ["b", "Kib", "Mib", "Gib", "Tib", "Pib", "Eib", "Zib", "Yib"],
|
13 | bytes: ["B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB"]
|
14 | },
|
15 | jedec: {
|
16 | bits: ["b", "Kb", "Mb", "Gb", "Tb", "Pb", "Eb", "Zb", "Yb"],
|
17 | bytes: ["B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"]
|
18 | }
|
19 | },
|
20 | fullform = {
|
21 | iec: ["", "kibi", "mebi", "gibi", "tebi", "pebi", "exbi", "zebi", "yobi"],
|
22 | jedec: ["", "kilo", "mega", "giga", "tera", "peta", "exa", "zetta", "yotta"]
|
23 | };
|
24 |
|
25 | |
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 | function filesize (arg, descriptor = {}) {
|
34 | let result = [],
|
35 | val = 0,
|
36 | e, base, bits, ceil, full, fullforms, neg, num, output, round, unix, separator, spacer, standard, symbols;
|
37 |
|
38 | if (isNaN(arg)) {
|
39 | throw new Error("Invalid arguments");
|
40 | }
|
41 |
|
42 | bits = descriptor.bits === true;
|
43 | unix = descriptor.unix === true;
|
44 | base = descriptor.base || 2;
|
45 | round = descriptor.round !== void 0 ? descriptor.round : unix ? 1 : 2;
|
46 | separator = descriptor.separator !== void 0 ? descriptor.separator || "" : "";
|
47 | spacer = descriptor.spacer !== void 0 ? descriptor.spacer : unix ? "" : " ";
|
48 | symbols = descriptor.symbols || {};
|
49 | standard = base === 2 ? descriptor.standard || "jedec" : "jedec";
|
50 | output = descriptor.output || "string";
|
51 | full = descriptor.fullform === true;
|
52 | fullforms = descriptor.fullforms instanceof Array ? descriptor.fullforms : [];
|
53 | e = descriptor.exponent !== void 0 ? descriptor.exponent : -1;
|
54 | num = Number(arg);
|
55 | neg = num < 0;
|
56 | ceil = base > 2 ? 1000 : 1024;
|
57 |
|
58 |
|
59 | if (neg) {
|
60 | num = -num;
|
61 | }
|
62 |
|
63 |
|
64 | if (e === -1 || isNaN(e)) {
|
65 | e = Math.floor(Math.log(num) / Math.log(ceil));
|
66 |
|
67 | if (e < 0) {
|
68 | e = 0;
|
69 | }
|
70 | }
|
71 |
|
72 |
|
73 | if (e > 8) {
|
74 | e = 8;
|
75 | }
|
76 |
|
77 |
|
78 | if (num === 0) {
|
79 | result[0] = 0;
|
80 | result[1] = unix ? "" : symbol[standard][bits ? "bits" : "bytes"][e];
|
81 | } else {
|
82 | val = num / (base === 2 ? Math.pow(2, e * 10) : Math.pow(1000, e));
|
83 |
|
84 | if (bits) {
|
85 | val = val * 8;
|
86 |
|
87 | if (val >= ceil && e < 8) {
|
88 | val = val / ceil;
|
89 | e++;
|
90 | }
|
91 | }
|
92 |
|
93 | result[0] = Number(val.toFixed(e > 0 ? round : 0));
|
94 | result[1] = base === 10 && e === 1 ? bits ? "kb" : "kB" : symbol[standard][bits ? "bits" : "bytes"][e];
|
95 |
|
96 | if (unix) {
|
97 | result[1] = standard === "jedec" ? result[1].charAt(0) : e > 0 ? result[1].replace(/B$/, "") : result[1];
|
98 |
|
99 | if (b.test(result[1])) {
|
100 | result[0] = Math.floor(result[0]);
|
101 | result[1] = "";
|
102 | }
|
103 | }
|
104 | }
|
105 |
|
106 |
|
107 | if (neg) {
|
108 | result[0] = -result[0];
|
109 | }
|
110 |
|
111 |
|
112 | result[1] = symbols[result[1]] || result[1];
|
113 |
|
114 |
|
115 | if (output === "array") {
|
116 | return result;
|
117 | }
|
118 |
|
119 | if (output === "exponent") {
|
120 | return e;
|
121 | }
|
122 |
|
123 | if (output === "object") {
|
124 | return {value: result[0], symbol: result[1]};
|
125 | }
|
126 |
|
127 | if (full) {
|
128 | result[1] = fullforms[e] ? fullforms[e] : fullform[standard][e] + (bits ? "bit" : "byte") + (result[0] === 1 ? "" : "s");
|
129 | }
|
130 |
|
131 | if (separator.length > 0) {
|
132 | result[0] = result[0].toString().replace(".", separator);
|
133 | }
|
134 |
|
135 | return result.join(spacer);
|
136 | }
|
137 |
|
138 |
|
139 | filesize.partial = opt => arg => filesize(arg, opt);
|
140 |
|
141 |
|
142 | if (typeof exports !== "undefined") {
|
143 | module.exports = filesize;
|
144 | } else if (typeof define === "function" && define.amd !== void 0) {
|
145 | define(() => filesize);
|
146 | } else {
|
147 | global.filesize = filesize;
|
148 | }
|
149 | }(typeof window !== "undefined" ? window : global));
|