Skip to content

Commit

Permalink
Refactoring locale option to accept String or Boolean values, r…
Browse files Browse the repository at this point in the history
…e-organizing code such that an `exponent` output skips unneeded ops
  • Loading branch information
avoidwork committed Feb 15, 2019
1 parent 3b71615 commit dd8f754
Show file tree
Hide file tree
Showing 11 changed files with 55 additions and 47 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ _*(boolean)*_ Enables full form of unit of measure, default is `false`
_*(array)*_ Array of full form overrides, default is `[]`

### locale (overrides 'separator')
_*(string)*_ BCP 47 language tag, default is `""`
_*(string || boolean)*_ BCP 47 language tag to specify a locale, or `true` to use default locale, default is `""`

### output
_*(string)*_ Output of function (`array`, `exponent`, `object`, or `string`), default is `string`
Expand Down
26 changes: 14 additions & 12 deletions lib/filesize.es6.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*
* @copyright 2019 Jason Mulligan <jason.mulligan@avoidwork.com>
* @license BSD-3-Clause
* @version 4.1.1
* @version 4.1.2
*/
(function (global) {
const b = /^(b|B)$/,
Expand Down Expand Up @@ -36,7 +36,7 @@
e, base, bits, ceil, full, fullforms, locale, neg, num, output, round, unix, separator, spacer, standard, symbols;

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

bits = descriptor.bits === true;
Expand Down Expand Up @@ -75,6 +75,10 @@
e = 8;
}

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

// Zero is now a special case because bytes divide by 1
if (num === 0) {
result[0] = 0;
Expand Down Expand Up @@ -112,25 +116,23 @@
// Applying custom symbol
result[1] = symbols[result[1]] || result[1];

if (locale === true) {
result[0] = result[0].toLocaleString();
} else if (locale.length > 0) {
result[0] = result[0].toLocaleString(locale);
} else if (separator.length > 0) {
result[0] = result[0].toString().replace(".", separator);
}

// Returning Array, Object, or String (default)
if (output === "array") {
return result;
}

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

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

if (locale.length > 0) {
result[0] = result[0].toLocaleString(locale);
} else if (separator.length > 0) {
result[0] = result[0].toString().replace(".", separator);
}

if (output === "object") {
return {value: result[0], symbol: result[1]};
}
Expand Down
9 changes: 5 additions & 4 deletions 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.

26 changes: 14 additions & 12 deletions lib/filesize.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*
* @copyright 2019 Jason Mulligan <jason.mulligan@avoidwork.com>
* @license BSD-3-Clause
* @version 4.1.1
* @version 4.1.2
*/
(function (global) {
var b = /^(b|B)$/,
Expand Down Expand Up @@ -55,7 +55,7 @@
symbols = void 0;

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

bits = descriptor.bits === true;
Expand Down Expand Up @@ -94,6 +94,10 @@
e = 8;
}

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

// Zero is now a special case because bytes divide by 1
if (num === 0) {
result[0] = 0;
Expand Down Expand Up @@ -131,25 +135,23 @@
// Applying custom symbol
result[1] = symbols[result[1]] || result[1];

if (locale === true) {
result[0] = result[0].toLocaleString();
} else if (locale.length > 0) {
result[0] = result[0].toLocaleString(locale);
} else if (separator.length > 0) {
result[0] = result[0].toString().replace(".", separator);
}

// Returning Array, Object, or String (default)
if (output === "array") {
return result;
}

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

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

if (locale.length > 0) {
result[0] = result[0].toLocaleString(locale);
} else if (separator.length > 0) {
result[0] = result[0].toString().replace(".", separator);
}

if (output === "object") {
return { value: result[0], symbol: result[1] };
}
Expand Down
4 changes: 2 additions & 2 deletions lib/filesize.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.min.js.map

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

2 changes: 1 addition & 1 deletion package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "filesize",
"description": "JavaScript library to generate a human readable String describing the file size",
"version": "4.1.1",
"version": "4.1.2",
"homepage": "https://filesizejs.com",
"author": "Jason Mulligan <jason.mulligan@avoidwork.com>",
"repository": {
Expand Down
24 changes: 13 additions & 11 deletions src/filesize.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
e, base, bits, ceil, full, fullforms, locale, neg, num, output, round, unix, separator, spacer, standard, symbols;

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

bits = descriptor.bits === true;
Expand Down Expand Up @@ -51,6 +51,10 @@
e = 8;
}

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

// Zero is now a special case because bytes divide by 1
if (num === 0) {
result[0] = 0;
Expand Down Expand Up @@ -88,25 +92,23 @@
// Applying custom symbol
result[1] = symbols[result[1]] || result[1];

if (locale === true) {
result[0] = result[0].toLocaleString();
} else if (locale.length > 0) {
result[0] = result[0].toLocaleString(locale);
} else if (separator.length > 0) {
result[0] = result[0].toString().replace(".", separator);
}

// Returning Array, Object, or String (default)
if (output === "array") {
return result;
}

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

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

if (locale.length > 0) {
result[0] = result[0].toLocaleString(locale);
} else if (separator.length > 0) {
result[0] = result[0].toString().replace(".", separator);
}

if (output === "object") {
return {value: result[0], symbol: result[1]};
}
Expand Down
3 changes: 2 additions & 1 deletion test/filesize_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,9 @@ exports.filesize = {
test.done();
},
locale: function (test) {
test.expect(2);
test.expect(3);
test.equal(filesize(1040, {locale: ""}), "1.02 KB", "Should be '1.02 KB'");
test.equal(filesize(1040, {locale: true}), Number(1.02).toLocaleString() + " KB", "Should be '" + Number(1.02).toLocaleString() + " KB'");
test.equal(filesize(1040, {locale: "de"}), Number(1.02).toLocaleString("de") + " KB", "Should be '" + Number(1.02).toLocaleString("de") + " KB'");
test.done();
}
Expand Down

0 comments on commit dd8f754

Please sign in to comment.