You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
1 line
9.0 KiB
1 line
9.0 KiB
2 years ago
|
{"version":3,"file":"filesize.es6.min.js","sources":["../src/filesize.js"],"sourcesContent":["const b = /^(b|B)$/,\r\n\tsymbol = {\r\n\t\tiec: {\r\n\t\t\tbits: [\"bit\", \"Kibit\", \"Mibit\", \"Gibit\", \"Tibit\", \"Pibit\", \"Eibit\", \"Zibit\", \"Yibit\"],\r\n\t\t\tbytes: [\"B\", \"KiB\", \"MiB\", \"GiB\", \"TiB\", \"PiB\", \"EiB\", \"ZiB\", \"YiB\"]\r\n\t\t},\r\n\t\tjedec: {\r\n\t\t\tbits: [\"bit\", \"Kbit\", \"Mbit\", \"Gbit\", \"Tbit\", \"Pbit\", \"Ebit\", \"Zbit\", \"Ybit\"],\r\n\t\t\tbytes: [\"B\", \"KB\", \"MB\", \"GB\", \"TB\", \"PB\", \"EB\", \"ZB\", \"YB\"]\r\n\t\t}\r\n\t},\r\n\tfullform = {\r\n\t\tiec: [\"\", \"kibi\", \"mebi\", \"gibi\", \"tebi\", \"pebi\", \"exbi\", \"zebi\", \"yobi\"],\r\n\t\tjedec: [\"\", \"kilo\", \"mega\", \"giga\", \"tera\", \"peta\", \"exa\", \"zetta\", \"yotta\"]\r\n\t},\r\n\troundingFuncs = {\r\n\t\tfloor: Math.floor,\r\n\t\tceil: Math.ceil\r\n\t};\r\n\r\n/**\r\n * filesize\r\n *\r\n * @method filesize\r\n * @param {Mixed} arg String, Int or Float to transform\r\n * @param {Object} descriptor [Optional] Flags\r\n * @return {String} Readable file size String\r\n */\r\nfunction filesize (arg, descriptor = {}) {\r\n\tlet result = [],\r\n\t\tval = 0,\r\n\t\te, base, bits, ceil, full, fullforms, locale, localeOptions, neg, num, output, pad, round, u, unix, separator, spacer, standard, symbols, roundingFunc, precision;\r\n\r\n\tif (isNaN(arg)) {\r\n\t\tthrow new TypeError(\"Invalid number\");\r\n\t}\r\n\r\n\tbits = descriptor.bits === true;\r\n\tunix = descriptor.unix === true;\r\n\tpad = descriptor.pad === true;\r\n\tbase = descriptor.base || 10;\r\n\tround = descriptor.round !== void 0 ? descriptor.round : unix ? 1 : 2;\r\n\tlocale = descriptor.locale !== void 0 ? descriptor.locale : \"\";\r\n\tlocaleOptions = descriptor.localeOptions || {};\r\n\tseparator = descriptor.separator !== void 0 ? descriptor.separator : \"\";\r\n\tspacer = descriptor.spacer !== void 0 ? descriptor.spacer : unix ? \"\" : \" \";\r\n\tsymbols = descriptor.symbols || {};\r\n\tstandard = base === 2 ? descriptor.standard || \"iec\" : \"jedec\";\r\n\toutput = descriptor.output || \"string\";\r\n\tfull = descriptor.fullform === true;\r\n\tfullforms = descriptor.fullforms instanceof Array ? descriptor.fullforms : [];\r\n\te = descriptor.exponent !== void 0 ? descriptor.exponent : -1;\r\n\troundingFunc = roundingFuncs[descriptor.roundingMethod] || Math.round;\r\n\tnum = Number(arg);\r\n\tneg = num < 0;\r\n\tceil = base > 2 ? 1000 : 1024;\r\n\tprecision = isNaN(descriptor.precision) === false ? parseInt(descriptor.precision, 10) : 0;\r\n\r\n\t// Flipping a negative number to determine the size\r\n\tif (neg) {\r\n\t\tnum = -num;\r\n\t}\r\n\r\n\t// Determining the exponent\r\n\tif (e === -1 || isNaN(e)) {\r\n\t\te = Math.floor(Math.log(num) / Math.log(ceil));\r\n\r\n\t\tif (e < 0) {\r\n\t\t\te = 0;\r\n\t\t}\r\n\t}\r\n\r\n\t// Exceeding supported length, time to reduce & multiply\r\n\tif (e > 8) {\r\n\t\tif (precision > 0) {\r\n\t\t\tprecision += 8 - e;\r\n\t\t}\r\n\r\n\t\te = 8;\r\n\t}\r\n\r\n\tif (output === \"exponent\") {\r\n\t\treturn e;\r\n\t}\r\n\r\n\t// Zero is now a special case because bytes divide by 1\r\n\tif (num === 0) {\r\n\t\tresult[0] = 0;\r\n\t\tu = result[1] = unix ? \"\" : symbol[standard][bits ? \"bits\" : \"bytes\"][e];\r\n\t} else {\r\n\t\tval = num / (base === 2 ? Math.pow(2, e * 10) : Math.pow(1000, e));\r\n\r\n\t\tif (bits) {\r\n\t\t\tval = val * 8;\r\n\r\n\t\t\tif (val >= ceil && e < 8) {\r\n\t\t\t\tval = val / ceil;\r\n\t\t\t\te++;\r\n\t\t\t}\r\n\t\t}\r\n\r\n\t\tconst p = Math.pow(10, e > 0 ? round : 0);\r\n\t\tresult[0] = roundingFunc(val * p) / p;\r\n\r\n\t\tif (result[0] === ceil && e < 8 && descriptor.exponent === void 0) {\r\n\t\t\tresult[0] = 1;\r\n\t\t\te++;\r\n\t\t}\r\n\r\n\t\tu = result[1] = base === 10 && e === 1 ? bits ? \"kbit\" : \"kB\" : symbol[standard][bits ? \"bits\" : \"bytes\"][e];\r\n\r\n\t\tif (unix) {\r\n\t\t\tresult[1] = result[1].charAt(0);\r\n\r\n\t\t\tif (b.test(result[1])) {\r\n\t\t\t\tresult[0] = Math.floor(result[0]);\r\n\t\t\t\tresult[1] = \
|