UNPKG

4.61 kBJavaScriptView Raw
1/**
2 * filesize
3 *
4 * @copyright 2020 Jason Mulligan <jason.mulligan@avoidwork.com>
5 * @license BSD-3-Clause
6 * @version 6.1.0
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 * filesize
27 *
28 * @method filesize
29 * @param {Mixed} arg String, Int or Float to transform
30 * @param {Object} descriptor [Optional] Flags
31 * @return {String} Readable file size String
32 */
33 function filesize (arg, descriptor = {}) {
34 let result = [],
35 val = 0,
36 e, base, bits, ceil, full, fullforms, locale, localeOptions, neg, num, output, round, unix, separator, spacer, standard, symbols;
37
38 if (isNaN(arg)) {
39 throw new TypeError("Invalid number");
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 locale = descriptor.locale !== void 0 ? descriptor.locale : "";
47 localeOptions = descriptor.localeOptions || {};
48 separator = descriptor.separator !== void 0 ? descriptor.separator : "";
49 spacer = descriptor.spacer !== void 0 ? descriptor.spacer : unix ? "" : " ";
50 symbols = descriptor.symbols || {};
51 standard = base === 2 ? descriptor.standard || "jedec" : "jedec";
52 output = descriptor.output || "string";
53 full = descriptor.fullform === true;
54 fullforms = descriptor.fullforms instanceof Array ? descriptor.fullforms : [];
55 e = descriptor.exponent !== void 0 ? descriptor.exponent : -1;
56 num = Number(arg);
57 neg = num < 0;
58 ceil = base > 2 ? 1000 : 1024;
59
60 // Flipping a negative number to determine the size
61 if (neg) {
62 num = -num;
63 }
64
65 // Determining the exponent
66 if (e === -1 || isNaN(e)) {
67 e = Math.floor(Math.log(num) / Math.log(ceil));
68
69 if (e < 0) {
70 e = 0;
71 }
72 }
73
74 // Exceeding supported length, time to reduce & multiply
75 if (e > 8) {
76 e = 8;
77 }
78
79 if (output === "exponent") {
80 return e;
81 }
82
83 // Zero is now a special case because bytes divide by 1
84 if (num === 0) {
85 result[0] = 0;
86 result[1] = unix ? "" : symbol[standard][bits ? "bits" : "bytes"][e];
87 } else {
88 val = num / (base === 2 ? Math.pow(2, e * 10) : Math.pow(1000, e));
89
90 if (bits) {
91 val = val * 8;
92
93 if (val >= ceil && e < 8) {
94 val = val / ceil;
95 e++;
96 }
97 }
98
99 result[0] = Number(val.toFixed(e > 0 ? round : 0));
100
101 if (result[0] === ceil && e < 8 && descriptor.exponent === void 0) {
102 result[0] = 1;
103 e++;
104 }
105
106 result[1] = base === 10 && e === 1 ? bits ? "kb" : "kB" : symbol[standard][bits ? "bits" : "bytes"][e];
107
108 if (unix) {
109 result[1] = standard === "jedec" ? result[1].charAt(0) : e > 0 ? result[1].replace(/B$/, "") : result[1];
110
111 if (b.test(result[1])) {
112 result[0] = Math.floor(result[0]);
113 result[1] = "";
114 }
115 }
116 }
117
118 // Decorating a 'diff'
119 if (neg) {
120 result[0] = -result[0];
121 }
122
123 // Applying custom symbol
124 result[1] = symbols[result[1]] || result[1];
125
126 if (locale === true) {
127 result[0] = result[0].toLocaleString();
128 } else if (locale.length > 0) {
129 result[0] = result[0].toLocaleString(locale, localeOptions);
130 } else if (separator.length > 0) {
131 result[0] = result[0].toString().replace(".", separator);
132 }
133
134 // Returning Array, Object, or String (default)
135 if (output === "array") {
136 return result;
137 }
138
139 if (full) {
140 result[1] = fullforms[e] ? fullforms[e] : fullform[standard][e] + (bits ? "bit" : "byte") + (result[0] === 1 ? "" : "s");
141 }
142
143 if (output === "object") {
144 return {value: result[0], symbol: result[1], exponent: e};
145 }
146
147 return result.join(spacer);
148 }
149
150 // Partial application for functional programming
151 filesize.partial = opt => arg => filesize(arg, opt);
152
153 // CommonJS, AMD, script tag
154 if (typeof exports !== "undefined") {
155 module.exports = filesize;
156 } else if (typeof define === "function" && define.amd !== void 0) {
157 define(() => filesize);
158 } else {
159 global.filesize = filesize;
160 }
161}(typeof window !== "undefined" ? window : global));