1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
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 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
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 | spacer = void 0,
|
52 | standard = void 0,
|
53 | symbols = void 0;
|
54 |
|
55 | if (isNaN(arg)) {
|
56 | throw new Error("Invalid arguments");
|
57 | }
|
58 |
|
59 | bits = descriptor.bits === true;
|
60 | unix = descriptor.unix === true;
|
61 | base = descriptor.base || 2;
|
62 | round = descriptor.round !== undefined ? descriptor.round : unix ? 1 : 2;
|
63 | spacer = descriptor.spacer !== undefined ? descriptor.spacer : unix ? "" : " ";
|
64 | symbols = descriptor.symbols || descriptor.suffixes || {};
|
65 | standard = base === 2 ? descriptor.standard || "jedec" : "jedec";
|
66 | output = descriptor.output || "string";
|
67 | full = descriptor.fullform === true;
|
68 | fullforms = descriptor.fullforms instanceof Array ? descriptor.fullforms : [];
|
69 | e = descriptor.exponent !== undefined ? descriptor.exponent : -1;
|
70 | num = Number(arg);
|
71 | neg = num < 0;
|
72 | ceil = base > 2 ? 1000 : 1024;
|
73 |
|
74 |
|
75 | if (neg) {
|
76 | num = -num;
|
77 | }
|
78 |
|
79 |
|
80 | if (e === -1 || isNaN(e)) {
|
81 | e = Math.floor(Math.log(num) / Math.log(ceil));
|
82 |
|
83 | if (e < 0) {
|
84 | e = 0;
|
85 | }
|
86 | }
|
87 |
|
88 |
|
89 | if (e > 8) {
|
90 | e = 8;
|
91 | }
|
92 |
|
93 |
|
94 | if (num === 0) {
|
95 | result[0] = 0;
|
96 | result[1] = unix ? "" : symbol[standard][bits ? "bits" : "bytes"][e];
|
97 | } else {
|
98 | val = num / (base === 2 ? Math.pow(2, e * 10) : Math.pow(1000, e));
|
99 |
|
100 | if (bits) {
|
101 | val = val * 8;
|
102 |
|
103 | if (val >= ceil && e < 8) {
|
104 | val = val / ceil;
|
105 | e++;
|
106 | }
|
107 | }
|
108 |
|
109 | result[0] = Number(val.toFixed(e > 0 ? round : 0));
|
110 | result[1] = base === 10 && e === 1 ? bits ? "kb" : "kB" : symbol[standard][bits ? "bits" : "bytes"][e];
|
111 |
|
112 | if (unix) {
|
113 | result[1] = standard === "jedec" ? result[1].charAt(0) : e > 0 ? result[1].replace(/B$/, "") : result[1];
|
114 |
|
115 | if (b.test(result[1])) {
|
116 | result[0] = Math.floor(result[0]);
|
117 | result[1] = "";
|
118 | }
|
119 | }
|
120 | }
|
121 |
|
122 |
|
123 | if (neg) {
|
124 | result[0] = -result[0];
|
125 | }
|
126 |
|
127 |
|
128 | result[1] = symbols[result[1]] || result[1];
|
129 |
|
130 |
|
131 | if (output === "array") {
|
132 | return result;
|
133 | }
|
134 |
|
135 | if (output === "exponent") {
|
136 | return e;
|
137 | }
|
138 |
|
139 | if (output === "object") {
|
140 | return { value: result[0], suffix: result[1], symbol: result[1] };
|
141 | }
|
142 |
|
143 | if (full) {
|
144 | result[1] = fullforms[e] ? fullforms[e] : fullform[standard][e] + (bits ? "bit" : "byte") + (result[0] === 1 ? "" : "s");
|
145 | }
|
146 |
|
147 | return result.join(spacer);
|
148 | }
|
149 |
|
150 |
|
151 | filesize.partial = function (opt) {
|
152 | return function (arg) {
|
153 | return filesize(arg, opt);
|
154 | };
|
155 | };
|
156 |
|
157 |
|
158 | if (typeof exports !== "undefined") {
|
159 | module.exports = filesize;
|
160 | } else if (typeof define === "function" && define.amd) {
|
161 | define(function () {
|
162 | return filesize;
|
163 | });
|
164 | } else {
|
165 | global.filesize = filesize;
|
166 | }
|
167 | })(typeof window !== "undefined" ? window : global);
|