UNPKG

4.17 kBJavaScriptView Raw
1/**
2 * filesize
3 *
4 * @copyright 2019 Jason Mulligan <jason.mulligan@avoidwork.com>
5 * @license BSD-3-Clause
6 * @version 4.0.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, 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 // Flipping a negative number to determine the size
59 if (neg) {
60 num = -num;
61 }
62
63 // Determining the exponent
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 // Exceeding supported length, time to reduce & multiply
73 if (e > 8) {
74 e = 8;
75 }
76
77 // Zero is now a special case because bytes divide by 1
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 // Decorating a 'diff'
107 if (neg) {
108 result[0] = -result[0];
109 }
110
111 // Applying custom symbol
112 result[1] = symbols[result[1]] || result[1];
113
114 // Returning Array, Object, or String (default)
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 // Partial application for functional programming
139 filesize.partial = opt => arg => filesize(arg, opt);
140
141 // CommonJS, AMD, script tag
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));