source: frontend/node_modules/filesize/lib/filesize.es6.js

Last change on this file was 9af201e, checked in by MBK <marija.karapandzova@…>, 12 days ago

Fix frontend appearance

  • Property mode set to 100644
File size: 5.1 KB
Line 
1/**
2 * filesize
3 *
4 * @copyright 2022 Jason Mulligan <jason.mulligan@avoidwork.com>
5 * @license BSD-3-Clause
6 * @version 8.0.7
7 */
8(function (global, factory) {
9 typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
10 typeof define === 'function' && define.amd ? define(factory) :
11 (global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.filesize = factory());
12})(this, (function () { 'use strict';
13
14 const b = /^(b|B)$/,
15 symbol = {
16 iec: {
17 bits: ["bit", "Kibit", "Mibit", "Gibit", "Tibit", "Pibit", "Eibit", "Zibit", "Yibit"],
18 bytes: ["B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB"]
19 },
20 jedec: {
21 bits: ["bit", "Kbit", "Mbit", "Gbit", "Tbit", "Pbit", "Ebit", "Zbit", "Ybit"],
22 bytes: ["B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"]
23 }
24 },
25 fullform = {
26 iec: ["", "kibi", "mebi", "gibi", "tebi", "pebi", "exbi", "zebi", "yobi"],
27 jedec: ["", "kilo", "mega", "giga", "tera", "peta", "exa", "zetta", "yotta"]
28 },
29 roundingFuncs = {
30 floor: Math.floor,
31 ceil: Math.ceil
32 };
33
34 /**
35 * filesize
36 *
37 * @method filesize
38 * @param {Mixed} arg String, Int or Float to transform
39 * @param {Object} descriptor [Optional] Flags
40 * @return {String} Readable file size String
41 */
42 function filesize (arg, descriptor = {}) {
43 let result = [],
44 val = 0,
45 e, base, bits, ceil, full, fullforms, locale, localeOptions, neg, num, output, pad, round, u, unix, separator, spacer, standard, symbols, roundingFunc, precision;
46
47 if (isNaN(arg)) {
48 throw new TypeError("Invalid number");
49 }
50
51 bits = descriptor.bits === true;
52 unix = descriptor.unix === true;
53 pad = descriptor.pad === true;
54 base = descriptor.base || 10;
55 round = descriptor.round !== void 0 ? descriptor.round : unix ? 1 : 2;
56 locale = descriptor.locale !== void 0 ? descriptor.locale : "";
57 localeOptions = descriptor.localeOptions || {};
58 separator = descriptor.separator !== void 0 ? descriptor.separator : "";
59 spacer = descriptor.spacer !== void 0 ? descriptor.spacer : unix ? "" : " ";
60 symbols = descriptor.symbols || {};
61 standard = base === 2 ? descriptor.standard || "iec" : "jedec";
62 output = descriptor.output || "string";
63 full = descriptor.fullform === true;
64 fullforms = descriptor.fullforms instanceof Array ? descriptor.fullforms : [];
65 e = descriptor.exponent !== void 0 ? descriptor.exponent : -1;
66 roundingFunc = roundingFuncs[descriptor.roundingMethod] || Math.round;
67 num = Number(arg);
68 neg = num < 0;
69 ceil = base > 2 ? 1000 : 1024;
70 precision = isNaN(descriptor.precision) === false ? parseInt(descriptor.precision, 10) : 0;
71
72 // Flipping a negative number to determine the size
73 if (neg) {
74 num = -num;
75 }
76
77 // Determining the exponent
78 if (e === -1 || isNaN(e)) {
79 e = Math.floor(Math.log(num) / Math.log(ceil));
80
81 if (e < 0) {
82 e = 0;
83 }
84 }
85
86 // Exceeding supported length, time to reduce & multiply
87 if (e > 8) {
88 if (precision > 0) {
89 precision += 8 - e;
90 }
91
92 e = 8;
93 }
94
95 if (output === "exponent") {
96 return e;
97 }
98
99 // Zero is now a special case because bytes divide by 1
100 if (num === 0) {
101 result[0] = 0;
102 u = result[1] = unix ? "" : symbol[standard][bits ? "bits" : "bytes"][e];
103 } else {
104 val = num / (base === 2 ? Math.pow(2, e * 10) : Math.pow(1000, e));
105
106 if (bits) {
107 val = val * 8;
108
109 if (val >= ceil && e < 8) {
110 val = val / ceil;
111 e++;
112 }
113 }
114
115 const p = Math.pow(10, e > 0 ? round : 0);
116 result[0] = roundingFunc(val * p) / p;
117
118 if (result[0] === ceil && e < 8 && descriptor.exponent === void 0) {
119 result[0] = 1;
120 e++;
121 }
122
123 u = result[1] = base === 10 && e === 1 ? bits ? "kbit" : "kB" : symbol[standard][bits ? "bits" : "bytes"][e];
124
125 if (unix) {
126 result[1] = result[1].charAt(0);
127
128 if (b.test(result[1])) {
129 result[0] = Math.floor(result[0]);
130 result[1] = "";
131 }
132 }
133 }
134
135 // Decorating a 'diff'
136 if (neg) {
137 result[0] = -result[0];
138 }
139
140 // Setting optional precision
141 if (precision > 0) {
142 result[0] = result[0].toPrecision(precision);
143 }
144
145 // Applying custom symbol
146 result[1] = symbols[result[1]] || result[1];
147
148 if (locale === true) {
149 result[0] = result[0].toLocaleString();
150 } else if (locale.length > 0) {
151 result[0] = result[0].toLocaleString(locale, localeOptions);
152 } else if (separator.length > 0) {
153 result[0] = result[0].toString().replace(".", separator);
154 }
155
156 if (pad && Number.isInteger(result[0]) === false && round > 0) {
157 const x = separator || ".",
158 tmp = result[0].toString().split(x),
159 s = tmp[1] || "",
160 l = s.length,
161 n = round - l;
162
163 result[0] = `${tmp[0]}${x}${s.padEnd(l + n, "0")}`;
164 }
165
166 if (full) {
167 result[1] = fullforms[e] ? fullforms[e] : fullform[standard][e] + (bits ? "bit" : "byte") + (result[0] === 1 ? "" : "s");
168 }
169
170 // Returning Array, Object, or String (default)
171 return output === "array" ? result : output === "object" ? {value: result[0], symbol: result[1], exponent: e, unit: u} : result.join(spacer);
172 }
173
174 // Partial application for functional programming
175 filesize.partial = opt => arg => filesize(arg, opt);
176
177 return filesize;
178
179}));
Note: See TracBrowser for help on using the repository browser.