| 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 | var 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 | * filesize
|
|---|
| 35 | *
|
|---|
| 36 | * @method filesize
|
|---|
| 37 | * @param {Mixed} arg String, Int or Float to transform
|
|---|
| 38 | * @param {Object} descriptor [Optional] Flags
|
|---|
| 39 | * @return {String} Readable file size String
|
|---|
| 40 | */
|
|---|
| 41 |
|
|---|
| 42 | function filesize(arg) {
|
|---|
| 43 | var descriptor = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|---|
| 44 | var result = [],
|
|---|
| 45 | val = 0,
|
|---|
| 46 | e,
|
|---|
| 47 | base,
|
|---|
| 48 | bits,
|
|---|
| 49 | ceil,
|
|---|
| 50 | full,
|
|---|
| 51 | fullforms,
|
|---|
| 52 | locale,
|
|---|
| 53 | localeOptions,
|
|---|
| 54 | neg,
|
|---|
| 55 | num,
|
|---|
| 56 | output,
|
|---|
| 57 | pad,
|
|---|
| 58 | round,
|
|---|
| 59 | u,
|
|---|
| 60 | unix,
|
|---|
| 61 | separator,
|
|---|
| 62 | spacer,
|
|---|
| 63 | standard,
|
|---|
| 64 | symbols,
|
|---|
| 65 | roundingFunc,
|
|---|
| 66 | precision;
|
|---|
| 67 |
|
|---|
| 68 | if (isNaN(arg)) {
|
|---|
| 69 | throw new TypeError("Invalid number");
|
|---|
| 70 | }
|
|---|
| 71 |
|
|---|
| 72 | bits = descriptor.bits === true;
|
|---|
| 73 | unix = descriptor.unix === true;
|
|---|
| 74 | pad = descriptor.pad === true;
|
|---|
| 75 | base = descriptor.base || 10;
|
|---|
| 76 | round = descriptor.round !== void 0 ? descriptor.round : unix ? 1 : 2;
|
|---|
| 77 | locale = descriptor.locale !== void 0 ? descriptor.locale : "";
|
|---|
| 78 | localeOptions = descriptor.localeOptions || {};
|
|---|
| 79 | separator = descriptor.separator !== void 0 ? descriptor.separator : "";
|
|---|
| 80 | spacer = descriptor.spacer !== void 0 ? descriptor.spacer : unix ? "" : " ";
|
|---|
| 81 | symbols = descriptor.symbols || {};
|
|---|
| 82 | standard = base === 2 ? descriptor.standard || "iec" : "jedec";
|
|---|
| 83 | output = descriptor.output || "string";
|
|---|
| 84 | full = descriptor.fullform === true;
|
|---|
| 85 | fullforms = descriptor.fullforms instanceof Array ? descriptor.fullforms : [];
|
|---|
| 86 | e = descriptor.exponent !== void 0 ? descriptor.exponent : -1;
|
|---|
| 87 | roundingFunc = roundingFuncs[descriptor.roundingMethod] || Math.round;
|
|---|
| 88 | num = Number(arg);
|
|---|
| 89 | neg = num < 0;
|
|---|
| 90 | ceil = base > 2 ? 1000 : 1024;
|
|---|
| 91 | precision = isNaN(descriptor.precision) === false ? parseInt(descriptor.precision, 10) : 0; // Flipping a negative number to determine the size
|
|---|
| 92 |
|
|---|
| 93 | if (neg) {
|
|---|
| 94 | num = -num;
|
|---|
| 95 | } // Determining the exponent
|
|---|
| 96 |
|
|---|
| 97 |
|
|---|
| 98 | if (e === -1 || isNaN(e)) {
|
|---|
| 99 | e = Math.floor(Math.log(num) / Math.log(ceil));
|
|---|
| 100 |
|
|---|
| 101 | if (e < 0) {
|
|---|
| 102 | e = 0;
|
|---|
| 103 | }
|
|---|
| 104 | } // Exceeding supported length, time to reduce & multiply
|
|---|
| 105 |
|
|---|
| 106 |
|
|---|
| 107 | if (e > 8) {
|
|---|
| 108 | if (precision > 0) {
|
|---|
| 109 | precision += 8 - e;
|
|---|
| 110 | }
|
|---|
| 111 |
|
|---|
| 112 | e = 8;
|
|---|
| 113 | }
|
|---|
| 114 |
|
|---|
| 115 | if (output === "exponent") {
|
|---|
| 116 | return e;
|
|---|
| 117 | } // Zero is now a special case because bytes divide by 1
|
|---|
| 118 |
|
|---|
| 119 |
|
|---|
| 120 | if (num === 0) {
|
|---|
| 121 | result[0] = 0;
|
|---|
| 122 | u = result[1] = unix ? "" : symbol[standard][bits ? "bits" : "bytes"][e];
|
|---|
| 123 | } else {
|
|---|
| 124 | val = num / (base === 2 ? Math.pow(2, e * 10) : Math.pow(1000, e));
|
|---|
| 125 |
|
|---|
| 126 | if (bits) {
|
|---|
| 127 | val = val * 8;
|
|---|
| 128 |
|
|---|
| 129 | if (val >= ceil && e < 8) {
|
|---|
| 130 | val = val / ceil;
|
|---|
| 131 | e++;
|
|---|
| 132 | }
|
|---|
| 133 | }
|
|---|
| 134 |
|
|---|
| 135 | var p = Math.pow(10, e > 0 ? round : 0);
|
|---|
| 136 | result[0] = roundingFunc(val * p) / p;
|
|---|
| 137 |
|
|---|
| 138 | if (result[0] === ceil && e < 8 && descriptor.exponent === void 0) {
|
|---|
| 139 | result[0] = 1;
|
|---|
| 140 | e++;
|
|---|
| 141 | }
|
|---|
| 142 |
|
|---|
| 143 | u = result[1] = base === 10 && e === 1 ? bits ? "kbit" : "kB" : symbol[standard][bits ? "bits" : "bytes"][e];
|
|---|
| 144 |
|
|---|
| 145 | if (unix) {
|
|---|
| 146 | result[1] = result[1].charAt(0);
|
|---|
| 147 |
|
|---|
| 148 | if (b.test(result[1])) {
|
|---|
| 149 | result[0] = Math.floor(result[0]);
|
|---|
| 150 | result[1] = "";
|
|---|
| 151 | }
|
|---|
| 152 | }
|
|---|
| 153 | } // Decorating a 'diff'
|
|---|
| 154 |
|
|---|
| 155 |
|
|---|
| 156 | if (neg) {
|
|---|
| 157 | result[0] = -result[0];
|
|---|
| 158 | } // Setting optional precision
|
|---|
| 159 |
|
|---|
| 160 |
|
|---|
| 161 | if (precision > 0) {
|
|---|
| 162 | result[0] = result[0].toPrecision(precision);
|
|---|
| 163 | } // Applying custom symbol
|
|---|
| 164 |
|
|---|
| 165 |
|
|---|
| 166 | result[1] = symbols[result[1]] || result[1];
|
|---|
| 167 |
|
|---|
| 168 | if (locale === true) {
|
|---|
| 169 | result[0] = result[0].toLocaleString();
|
|---|
| 170 | } else if (locale.length > 0) {
|
|---|
| 171 | result[0] = result[0].toLocaleString(locale, localeOptions);
|
|---|
| 172 | } else if (separator.length > 0) {
|
|---|
| 173 | result[0] = result[0].toString().replace(".", separator);
|
|---|
| 174 | }
|
|---|
| 175 |
|
|---|
| 176 | if (pad && Number.isInteger(result[0]) === false && round > 0) {
|
|---|
| 177 | var x = separator || ".",
|
|---|
| 178 | tmp = result[0].toString().split(x),
|
|---|
| 179 | s = tmp[1] || "",
|
|---|
| 180 | l = s.length,
|
|---|
| 181 | n = round - l;
|
|---|
| 182 | result[0] = "".concat(tmp[0]).concat(x).concat(s.padEnd(l + n, "0"));
|
|---|
| 183 | }
|
|---|
| 184 |
|
|---|
| 185 | if (full) {
|
|---|
| 186 | result[1] = fullforms[e] ? fullforms[e] : fullform[standard][e] + (bits ? "bit" : "byte") + (result[0] === 1 ? "" : "s");
|
|---|
| 187 | } // Returning Array, Object, or String (default)
|
|---|
| 188 |
|
|---|
| 189 |
|
|---|
| 190 | return output === "array" ? result : output === "object" ? {
|
|---|
| 191 | value: result[0],
|
|---|
| 192 | symbol: result[1],
|
|---|
| 193 | exponent: e,
|
|---|
| 194 | unit: u
|
|---|
| 195 | } : result.join(spacer);
|
|---|
| 196 | } // Partial application for functional programming
|
|---|
| 197 |
|
|---|
| 198 |
|
|---|
| 199 | filesize.partial = function (opt) {
|
|---|
| 200 | return function (arg) {
|
|---|
| 201 | return filesize(arg, opt);
|
|---|
| 202 | };
|
|---|
| 203 | };
|
|---|
| 204 |
|
|---|
| 205 | return filesize;
|
|---|
| 206 |
|
|---|
| 207 | }));
|
|---|