Skip to content

Commit

Permalink
Updating the signature to use destructuring, removing edge case assig…
Browse files Browse the repository at this point in the history
…nments on invalid inputs (fail by design)
  • Loading branch information
avoidwork committed Jun 2, 2022
1 parent 3c6bcb5 commit a833bb5
Show file tree
Hide file tree
Showing 13 changed files with 357 additions and 425 deletions.
69 changes: 29 additions & 40 deletions lib/filesize.es6.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,34 @@
*
* @copyright 2022 Jason Mulligan <jason.mulligan@avoidwork.com>
* @license BSD-3-Clause
* @version 9.0.0
* @version 9.0.1
*/
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
typeof define === 'function' && define.amd ? define(factory) :
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.filesize = factory());
})(this, (function () { 'use strict';

const symbol = {
iec: {
bits: ["bit", "Kibit", "Mibit", "Gibit", "Tibit", "Pibit", "Eibit", "Zibit", "Yibit"],
bytes: ["B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB"]
const strings = {
symbol: {
iec: {
bits: ["bit", "Kibit", "Mibit", "Gibit", "Tibit", "Pibit", "Eibit", "Zibit", "Yibit"],
bytes: ["B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB"]
},
jedec: {
bits: ["bit", "Kbit", "Mbit", "Gbit", "Tbit", "Pbit", "Ebit", "Zbit", "Ybit"],
bytes: ["B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"]
}
},
jedec: {
bits: ["bit", "Kbit", "Mbit", "Gbit", "Tbit", "Pbit", "Ebit", "Zbit", "Ybit"],
bytes: ["B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"]
fullform: {
iec: ["", "kibi", "mebi", "gibi", "tebi", "pebi", "exbi", "zebi", "yobi"],
jedec: ["", "kilo", "mega", "giga", "tera", "peta", "exa", "zetta", "yotta"]
}
},
fullform = {
iec: ["", "kibi", "mebi", "gibi", "tebi", "pebi", "exbi", "zebi", "yobi"],
jedec: ["", "kilo", "mega", "giga", "tera", "peta", "exa", "zetta", "yotta"]
},
roundingFuncs = {
ceil: Math.ceil,
floor: Math.floor,
ceil: Math.ceil
round: Math.round
};

/**
Expand All @@ -38,35 +41,21 @@
* @param {Object} descriptor [Optional] Flags
* @return {String} Readable file size String
*/
function filesize (arg, descriptor = {}) {
let result = [],
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} = {}) {
let e = exponent,
num = Number(arg),
result = [],
val = 0,
e, base, bits, ceil, full, fullforms, locale, localeOptions, neg, num, output, pad, round, u, separator, spacer, standard, symbols, roundingFunc, precision;
u = "";
const ceil = base === 10 ? 1000 : 1024,
full = fullform === true,
neg = num < 0,
roundingFunc = roundingFuncs[roundingMethod];

if (isNaN(arg)) {
throw new TypeError("Invalid number");
}

bits = descriptor.bits === true;
pad = descriptor.pad === true;
base = descriptor.base || 10;
round = descriptor.round !== void 0 ? descriptor.round : 2;
locale = descriptor.locale !== void 0 ? descriptor.locale : "";
localeOptions = descriptor.localeOptions || {};
separator = descriptor.separator !== void 0 ? descriptor.separator : "";
spacer = descriptor.spacer !== void 0 ? descriptor.spacer : " ";
symbols = descriptor.symbols || {};
standard = descriptor.standard in symbol ? descriptor.standard : "iec";
output = descriptor.output || "string";
full = descriptor.fullform === true;
fullforms = descriptor.fullforms instanceof Array ? descriptor.fullforms : [];
e = descriptor.exponent !== void 0 ? descriptor.exponent : -1;
roundingFunc = roundingFuncs[descriptor.roundingMethod] || Math.round;
num = Number(arg);
neg = num < 0;
ceil = base > 2 ? 1000 : 1024;
precision = isNaN(descriptor.precision) === false ? parseInt(descriptor.precision, 10) : 0;

// Flipping a negative number to determine the size
if (neg) {
num = -num;
Expand Down Expand Up @@ -97,7 +86,7 @@
// Zero is now a special case because bytes divide by 1
if (num === 0) {
result[0] = 0;
u = result[1] = symbol[standard][bits ? "bits" : "bytes"][e];
u = result[1] = strings.symbol[standard][bits ? "bits" : "bytes"][e];
} else {
val = num / (base === 2 ? Math.pow(2, e * 10) : Math.pow(1000, e));

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

if (result[0] === ceil && e < 8 && descriptor.exponent === void 0) {
if (result[0] === ceil && e < 8 && exponent === -1) {
result[0] = 1;
e++;
}

u = result[1] = symbol[standard][bits ? "bits" : "bytes"][e];
u = result[1] = strings.symbol[standard][bits ? "bits" : "bytes"][e];
}

// Decorating a 'diff'
Expand Down Expand Up @@ -153,7 +142,7 @@
}

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

// Returning Array, Object, or String (default)
Expand Down
4 changes: 2 additions & 2 deletions lib/filesize.es6.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit a833bb5

Please sign in to comment.