Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix regression of output from 9.x.x #155

Merged
merged 2 commits into from
Jun 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 40 additions & 18 deletions lib/filesize.es6.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,28 @@
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.filesize = factory());
})(this, (function () { 'use strict';

const ARRAY = "array";
const BIT = "bit";
const BITS = "bits";
const BYTE = "byte";
const BYTES = "bytes";
const EMPTY = "";
const EXPONENT = "exponent";
const FUNCTION = "function";
const IEC = "iec";
const INVALID_NUMBER = "Invalid number";
const INVALID_ROUND = "Invalid rounding method";
const JEDEC = "jedec";
const OBJECT = "object";
const PERIOD = ".";
const ROUND = "round";
const S = "s";
const SI_KBIT = "kbit";
const SI_KBYTE = "kB";
const SPACE = " ";
const STRING = "string";
const ZERO = "0";

const strings = {
symbol: {
iec: {
Expand All @@ -36,23 +58,23 @@
* @param {Object} descriptor [Optional] Flags
* @return {String} Readable file size String
*/
function filesize (arg, {bits = false, pad = false, base = -1, round = 2, locale = "", localeOptions = {}, separator = "", spacer = " ", symbols = {}, standard = "", output = "string", fullform = false, fullforms = [], exponent = -1, roundingMethod = "round", precision = 0} = {}) {
function filesize (arg, {bits = false, pad = false, base = -1, round = 2, locale = EMPTY, localeOptions = {}, separator = EMPTY, spacer = SPACE, symbols = {}, standard = EMPTY, output = STRING, fullform = false, fullforms = [], exponent = -1, roundingMethod = ROUND, precision = 0} = {}) {
let e = exponent,
num = Number(arg),
result = [],
val = 0,
u = "";
u = EMPTY;

// Sync base & standard
if (base === -1 && standard.length === 0) {
base = 10;
standard = "iec";
standard = JEDEC;
} else if (base === -1 && standard.length > 0) {
standard = standard === "iec" ? "iec" : "jedec";
base = standard === "iec" ? 10 : 2;
standard = standard === IEC ? IEC : JEDEC;
base = standard === IEC ? 2 : 10;
} else {
base = base === 2 ? 2 : 10;
standard = base === 10 ? "iec" : "jedec";
standard = base === 10 ? JEDEC : IEC;
}

const ceil = base === 10 ? 1000 : 1024,
Expand All @@ -61,11 +83,11 @@
roundingFunc = Math[roundingMethod];

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

if (typeof roundingFunc !== "function") {
throw new TypeError("Invalid rounding method");
if (typeof roundingFunc !== FUNCTION) {
throw new TypeError(INVALID_ROUND);
}

// Flipping a negative number to determine the size
Expand All @@ -91,14 +113,14 @@
e = 8;
}

if (output === "exponent") {
if (output === EXPONENT) {
return e;
}

// Zero is now a special case because bytes divide by 1
if (num === 0) {
result[0] = 0;
u = result[1] = strings.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 @@ -119,7 +141,7 @@
e++;
}

u = result[1] = strings.symbol[standard][bits ? "bits" : "bytes"][e];
u = result[1] = base === 10 && e === 1 ? bits ? SI_KBIT : SI_KBYTE : strings.symbol[standard][bits ? BITS : BYTES][e];
}

// Decorating a 'diff'
Expand All @@ -140,25 +162,25 @@
} else if (locale.length > 0) {
result[0] = result[0].toLocaleString(locale, localeOptions);
} else if (separator.length > 0) {
result[0] = result[0].toString().replace(".", separator);
result[0] = result[0].toString().replace(PERIOD, separator);
}

if (pad && Number.isInteger(result[0]) === false && round > 0) {
const x = separator || ".",
const x = separator || PERIOD,
tmp = result[0].toString().split(x),
s = tmp[1] || "",
s = tmp[1] || EMPTY,
l = s.length,
n = round - l;

result[0] = `${tmp[0]}${x}${s.padEnd(l + n, "0")}`;
result[0] = `${tmp[0]}${x}${s.padEnd(l + n, ZERO)}`;
}

if (full) {
result[1] = fullforms[e] ? fullforms[e] : strings.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 ? EMPTY : S);
}

// Returning Array, Object, or String (default)
return output === "array" ? result : output === "object" ? {value: result[0], symbol: result[1], exponent: e, unit: u} : result.join(spacer);
return output === ARRAY ? result : output === OBJECT ? {value: result[0], symbol: result[1], exponent: e, unit: u} : result.join(spacer);
}

// Partial application for functional programming
Expand Down
2 changes: 1 addition & 1 deletion lib/filesize.es6.min.js

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

2 changes: 1 addition & 1 deletion lib/filesize.es6.min.js.map

Large diffs are not rendered by default.

58 changes: 40 additions & 18 deletions lib/filesize.esm.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,28 @@
* @license BSD-3-Clause
* @version 9.0.8
*/
const ARRAY = "array";
const BIT = "bit";
const BITS = "bits";
const BYTE = "byte";
const BYTES = "bytes";
const EMPTY = "";
const EXPONENT = "exponent";
const FUNCTION = "function";
const IEC = "iec";
const INVALID_NUMBER = "Invalid number";
const INVALID_ROUND = "Invalid rounding method";
const JEDEC = "jedec";
const OBJECT = "object";
const PERIOD = ".";
const ROUND = "round";
const S = "s";
const SI_KBIT = "kbit";
const SI_KBYTE = "kB";
const SPACE = " ";
const STRING = "string";
const ZERO = "0";

const strings = {
symbol: {
iec: {
Expand All @@ -30,23 +52,23 @@ const strings = {
* @param {Object} descriptor [Optional] Flags
* @return {String} Readable file size String
*/
function filesize (arg, {bits = false, pad = false, base = -1, round = 2, locale = "", localeOptions = {}, separator = "", spacer = " ", symbols = {}, standard = "", output = "string", fullform = false, fullforms = [], exponent = -1, roundingMethod = "round", precision = 0} = {}) {
function filesize (arg, {bits = false, pad = false, base = -1, round = 2, locale = EMPTY, localeOptions = {}, separator = EMPTY, spacer = SPACE, symbols = {}, standard = EMPTY, output = STRING, fullform = false, fullforms = [], exponent = -1, roundingMethod = ROUND, precision = 0} = {}) {
let e = exponent,
num = Number(arg),
result = [],
val = 0,
u = "";
u = EMPTY;

// Sync base & standard
if (base === -1 && standard.length === 0) {
base = 10;
standard = "iec";
standard = JEDEC;
} else if (base === -1 && standard.length > 0) {
standard = standard === "iec" ? "iec" : "jedec";
base = standard === "iec" ? 10 : 2;
standard = standard === IEC ? IEC : JEDEC;
base = standard === IEC ? 2 : 10;
} else {
base = base === 2 ? 2 : 10;
standard = base === 10 ? "iec" : "jedec";
standard = base === 10 ? JEDEC : IEC;
}

const ceil = base === 10 ? 1000 : 1024,
Expand All @@ -55,11 +77,11 @@ function filesize (arg, {bits = false, pad = false, base = -1, round = 2, locale
roundingFunc = Math[roundingMethod];

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

if (typeof roundingFunc !== "function") {
throw new TypeError("Invalid rounding method");
if (typeof roundingFunc !== FUNCTION) {
throw new TypeError(INVALID_ROUND);
}

// Flipping a negative number to determine the size
Expand All @@ -85,14 +107,14 @@ function filesize (arg, {bits = false, pad = false, base = -1, round = 2, locale
e = 8;
}

if (output === "exponent") {
if (output === EXPONENT) {
return e;
}

// Zero is now a special case because bytes divide by 1
if (num === 0) {
result[0] = 0;
u = result[1] = strings.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,7 +135,7 @@ function filesize (arg, {bits = false, pad = false, base = -1, round = 2, locale
e++;
}

u = result[1] = strings.symbol[standard][bits ? "bits" : "bytes"][e];
u = result[1] = base === 10 && e === 1 ? bits ? SI_KBIT : SI_KBYTE : strings.symbol[standard][bits ? BITS : BYTES][e];
}

// Decorating a 'diff'
Expand All @@ -134,25 +156,25 @@ function filesize (arg, {bits = false, pad = false, base = -1, round = 2, locale
} else if (locale.length > 0) {
result[0] = result[0].toLocaleString(locale, localeOptions);
} else if (separator.length > 0) {
result[0] = result[0].toString().replace(".", separator);
result[0] = result[0].toString().replace(PERIOD, separator);
}

if (pad && Number.isInteger(result[0]) === false && round > 0) {
const x = separator || ".",
const x = separator || PERIOD,
tmp = result[0].toString().split(x),
s = tmp[1] || "",
s = tmp[1] || EMPTY,
l = s.length,
n = round - l;

result[0] = `${tmp[0]}${x}${s.padEnd(l + n, "0")}`;
result[0] = `${tmp[0]}${x}${s.padEnd(l + n, ZERO)}`;
}

if (full) {
result[1] = fullforms[e] ? fullforms[e] : strings.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 ? EMPTY : S);
}

// Returning Array, Object, or String (default)
return output === "array" ? result : output === "object" ? {value: result[0], symbol: result[1], exponent: e, unit: u} : result.join(spacer);
return output === ARRAY ? result : output === OBJECT ? {value: result[0], symbol: result[1], exponent: e, unit: u} : result.join(spacer);
}

// Partial application for functional programming
Expand Down
2 changes: 1 addition & 1 deletion lib/filesize.esm.min.js

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

Loading