UNPKG

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