Skip to content

Commit a833bb5

Browse files
committed
Updating the signature to use destructuring, removing edge case assignments on invalid inputs (fail by design)
1 parent 3c6bcb5 commit a833bb5

13 files changed

+357
-425
lines changed

lib/filesize.es6.js

+29-40
Original file line numberDiff line numberDiff line change
@@ -3,31 +3,34 @@
33
*
44
* @copyright 2022 Jason Mulligan <[email protected]>
55
* @license BSD-3-Clause
6-
* @version 9.0.0
6+
* @version 9.0.1
77
*/
88
(function (global, factory) {
99
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
1010
typeof define === 'function' && define.amd ? define(factory) :
1111
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.filesize = factory());
1212
})(this, (function () { 'use strict';
1313

14-
const symbol = {
15-
iec: {
16-
bits: ["bit", "Kibit", "Mibit", "Gibit", "Tibit", "Pibit", "Eibit", "Zibit", "Yibit"],
17-
bytes: ["B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB"]
14+
const strings = {
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+
}
1824
},
19-
jedec: {
20-
bits: ["bit", "Kbit", "Mbit", "Gbit", "Tbit", "Pbit", "Ebit", "Zbit", "Ybit"],
21-
bytes: ["B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"]
25+
fullform: {
26+
iec: ["", "kibi", "mebi", "gibi", "tebi", "pebi", "exbi", "zebi", "yobi"],
27+
jedec: ["", "kilo", "mega", "giga", "tera", "peta", "exa", "zetta", "yotta"]
2228
}
2329
},
24-
fullform = {
25-
iec: ["", "kibi", "mebi", "gibi", "tebi", "pebi", "exbi", "zebi", "yobi"],
26-
jedec: ["", "kilo", "mega", "giga", "tera", "peta", "exa", "zetta", "yotta"]
27-
},
2830
roundingFuncs = {
31+
ceil: Math.ceil,
2932
floor: Math.floor,
30-
ceil: Math.ceil
33+
round: Math.round
3134
};
3235

3336
/**
@@ -38,35 +41,21 @@
3841
* @param {Object} descriptor [Optional] Flags
3942
* @return {String} Readable file size String
4043
*/
41-
function filesize (arg, descriptor = {}) {
42-
let result = [],
44+
function filesize (arg, {bits = false, pad = false, base = 10, round = 2, locale = "", localeOptions = {}, separator = "", spacer = " ", symbols = {}, standard = "iec", output = "string", fullform = false, fullforms = [], exponent = -1, roundingMethod = "round", precision = 0} = {}) {
45+
let e = exponent,
46+
num = Number(arg),
47+
result = [],
4348
val = 0,
44-
e, base, bits, ceil, full, fullforms, locale, localeOptions, neg, num, output, pad, round, u, separator, spacer, standard, symbols, roundingFunc, precision;
49+
u = "";
50+
const ceil = base === 10 ? 1000 : 1024,
51+
full = fullform === true,
52+
neg = num < 0,
53+
roundingFunc = roundingFuncs[roundingMethod];
4554

4655
if (isNaN(arg)) {
4756
throw new TypeError("Invalid number");
4857
}
4958

50-
bits = descriptor.bits === true;
51-
pad = descriptor.pad === true;
52-
base = descriptor.base || 10;
53-
round = descriptor.round !== void 0 ? descriptor.round : 2;
54-
locale = descriptor.locale !== void 0 ? descriptor.locale : "";
55-
localeOptions = descriptor.localeOptions || {};
56-
separator = descriptor.separator !== void 0 ? descriptor.separator : "";
57-
spacer = descriptor.spacer !== void 0 ? descriptor.spacer : " ";
58-
symbols = descriptor.symbols || {};
59-
standard = descriptor.standard in symbol ? descriptor.standard : "iec";
60-
output = descriptor.output || "string";
61-
full = descriptor.fullform === true;
62-
fullforms = descriptor.fullforms instanceof Array ? descriptor.fullforms : [];
63-
e = descriptor.exponent !== void 0 ? descriptor.exponent : -1;
64-
roundingFunc = roundingFuncs[descriptor.roundingMethod] || Math.round;
65-
num = Number(arg);
66-
neg = num < 0;
67-
ceil = base > 2 ? 1000 : 1024;
68-
precision = isNaN(descriptor.precision) === false ? parseInt(descriptor.precision, 10) : 0;
69-
7059
// Flipping a negative number to determine the size
7160
if (neg) {
7261
num = -num;
@@ -97,7 +86,7 @@
9786
// Zero is now a special case because bytes divide by 1
9887
if (num === 0) {
9988
result[0] = 0;
100-
u = result[1] = symbol[standard][bits ? "bits" : "bytes"][e];
89+
u = result[1] = strings.symbol[standard][bits ? "bits" : "bytes"][e];
10190
} else {
10291
val = num / (base === 2 ? Math.pow(2, e * 10) : Math.pow(1000, e));
10392

@@ -113,12 +102,12 @@
113102
const p = Math.pow(10, e > 0 ? round : 0);
114103
result[0] = roundingFunc(val * p) / p;
115104

116-
if (result[0] === ceil && e < 8 && descriptor.exponent === void 0) {
105+
if (result[0] === ceil && e < 8 && exponent === -1) {
117106
result[0] = 1;
118107
e++;
119108
}
120109

121-
u = result[1] = symbol[standard][bits ? "bits" : "bytes"][e];
110+
u = result[1] = strings.symbol[standard][bits ? "bits" : "bytes"][e];
122111
}
123112

124113
// Decorating a 'diff'
@@ -153,7 +142,7 @@
153142
}
154143

155144
if (full) {
156-
result[1] = fullforms[e] ? fullforms[e] : fullform[standard][e] + (bits ? "bit" : "byte") + (result[0] === 1 ? "" : "s");
145+
result[1] = fullforms[e] ? fullforms[e] : strings.fullform[standard][e] + (bits ? "bit" : "byte") + (result[0] === 1 ? "" : "s");
157146
}
158147

159148
// Returning Array, Object, or String (default)

lib/filesize.es6.min.js

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)