UNPKG

5.39 kBJavaScriptView Raw
1"use strict";
2
3/**
4 * filesize
5 *
6 * @copyright 2020 Jason Mulligan <jason.mulligan@avoidwork.com>
7 * @license BSD-3-Clause
8 * @version 6.1.0
9 */
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 * 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
119 if (result[0] === ceil && e < 8 && descriptor.exponent === void 0) {
120 result[0] = 1;
121 e++;
122 }
123
124 result[1] = base === 10 && e === 1 ? bits ? "kb" : "kB" : symbol[standard][bits ? "bits" : "bytes"][e];
125
126 if (unix) {
127 result[1] = standard === "jedec" ? result[1].charAt(0) : e > 0 ? result[1].replace(/B$/, "") : result[1];
128
129 if (b.test(result[1])) {
130 result[0] = Math.floor(result[0]);
131 result[1] = "";
132 }
133 }
134 } // Decorating a 'diff'
135
136
137 if (neg) {
138 result[0] = -result[0];
139 } // Applying custom symbol
140
141
142 result[1] = symbols[result[1]] || result[1];
143
144 if (locale === true) {
145 result[0] = result[0].toLocaleString();
146 } else if (locale.length > 0) {
147 result[0] = result[0].toLocaleString(locale, localeOptions);
148 } else if (separator.length > 0) {
149 result[0] = result[0].toString().replace(".", separator);
150 } // Returning Array, Object, or String (default)
151
152
153 if (output === "array") {
154 return result;
155 }
156
157 if (full) {
158 result[1] = fullforms[e] ? fullforms[e] : fullform[standard][e] + (bits ? "bit" : "byte") + (result[0] === 1 ? "" : "s");
159 }
160
161 if (output === "object") {
162 return {
163 value: result[0],
164 symbol: result[1],
165 exponent: e
166 };
167 }
168
169 return result.join(spacer);
170 } // Partial application for functional programming
171
172
173 filesize.partial = function (opt) {
174 return function (arg) {
175 return filesize(arg, opt);
176 };
177 }; // CommonJS, AMD, script tag
178
179
180 if (typeof exports !== "undefined") {
181 module.exports = filesize;
182 } else if (typeof define === "function" && define.amd !== void 0) {
183 define(function () {
184 return filesize;
185 });
186 } else {
187 global.filesize = filesize;
188 }
189})(typeof window !== "undefined" ? window : global);