Skip to content

Commit dd8f754

Browse files
committed
Refactoring locale option to accept String or Boolean values, re-organizing code such that an exponent output skips unneeded ops
1 parent 3b71615 commit dd8f754

11 files changed

+55
-47
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ _*(boolean)*_ Enables full form of unit of measure, default is `false`
2424
_*(array)*_ Array of full form overrides, default is `[]`
2525

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

2929
### output
3030
_*(string)*_ Output of function (`array`, `exponent`, `object`, or `string`), default is `string`

lib/filesize.es6.js

+14-12
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*
44
* @copyright 2019 Jason Mulligan <[email protected]>
55
* @license BSD-3-Clause
6-
* @version 4.1.1
6+
* @version 4.1.2
77
*/
88
(function (global) {
99
const b = /^(b|B)$/,
@@ -36,7 +36,7 @@
3636
e, base, bits, ceil, full, fullforms, locale, neg, num, output, round, unix, separator, spacer, standard, symbols;
3737

3838
if (isNaN(arg)) {
39-
throw new Error("Invalid arguments");
39+
throw new TypeError("Invalid number");
4040
}
4141

4242
bits = descriptor.bits === true;
@@ -75,6 +75,10 @@
7575
e = 8;
7676
}
7777

78+
if (output === "exponent") {
79+
return e;
80+
}
81+
7882
// Zero is now a special case because bytes divide by 1
7983
if (num === 0) {
8084
result[0] = 0;
@@ -112,25 +116,23 @@
112116
// Applying custom symbol
113117
result[1] = symbols[result[1]] || result[1];
114118

119+
if (locale === true) {
120+
result[0] = result[0].toLocaleString();
121+
} else if (locale.length > 0) {
122+
result[0] = result[0].toLocaleString(locale);
123+
} else if (separator.length > 0) {
124+
result[0] = result[0].toString().replace(".", separator);
125+
}
126+
115127
// Returning Array, Object, or String (default)
116128
if (output === "array") {
117129
return result;
118130
}
119131

120-
if (output === "exponent") {
121-
return e;
122-
}
123-
124132
if (full) {
125133
result[1] = fullforms[e] ? fullforms[e] : fullform[standard][e] + (bits ? "bit" : "byte") + (result[0] === 1 ? "" : "s");
126134
}
127135

128-
if (locale.length > 0) {
129-
result[0] = result[0].toLocaleString(locale);
130-
} else if (separator.length > 0) {
131-
result[0] = result[0].toString().replace(".", separator);
132-
}
133-
134136
if (output === "object") {
135137
return {value: result[0], symbol: result[1]};
136138
}

lib/filesize.es6.min.js

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

lib/filesize.es6.min.js.map

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

lib/filesize.js

+14-12
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
*
66
* @copyright 2019 Jason Mulligan <[email protected]>
77
* @license BSD-3-Clause
8-
* @version 4.1.1
8+
* @version 4.1.2
99
*/
1010
(function (global) {
1111
var b = /^(b|B)$/,
@@ -55,7 +55,7 @@
5555
symbols = void 0;
5656

5757
if (isNaN(arg)) {
58-
throw new Error("Invalid arguments");
58+
throw new TypeError("Invalid number");
5959
}
6060

6161
bits = descriptor.bits === true;
@@ -94,6 +94,10 @@
9494
e = 8;
9595
}
9696

97+
if (output === "exponent") {
98+
return e;
99+
}
100+
97101
// Zero is now a special case because bytes divide by 1
98102
if (num === 0) {
99103
result[0] = 0;
@@ -131,25 +135,23 @@
131135
// Applying custom symbol
132136
result[1] = symbols[result[1]] || result[1];
133137

138+
if (locale === true) {
139+
result[0] = result[0].toLocaleString();
140+
} else if (locale.length > 0) {
141+
result[0] = result[0].toLocaleString(locale);
142+
} else if (separator.length > 0) {
143+
result[0] = result[0].toString().replace(".", separator);
144+
}
145+
134146
// Returning Array, Object, or String (default)
135147
if (output === "array") {
136148
return result;
137149
}
138150

139-
if (output === "exponent") {
140-
return e;
141-
}
142-
143151
if (full) {
144152
result[1] = fullforms[e] ? fullforms[e] : fullform[standard][e] + (bits ? "bit" : "byte") + (result[0] === 1 ? "" : "s");
145153
}
146154

147-
if (locale.length > 0) {
148-
result[0] = result[0].toLocaleString(locale);
149-
} else if (separator.length > 0) {
150-
result[0] = result[0].toString().replace(".", separator);
151-
}
152-
153155
if (output === "object") {
154156
return { value: result[0], symbol: result[1] };
155157
}

lib/filesize.min.js

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

lib/filesize.min.js.map

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

package-lock.json

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

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "filesize",
33
"description": "JavaScript library to generate a human readable String describing the file size",
4-
"version": "4.1.1",
4+
"version": "4.1.2",
55
"homepage": "https://filesizejs.com",
66
"author": "Jason Mulligan <[email protected]>",
77
"repository": {

src/filesize.js

+13-11
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
e, base, bits, ceil, full, fullforms, locale, neg, num, output, round, unix, separator, spacer, standard, symbols;
1313

1414
if (isNaN(arg)) {
15-
throw new Error("Invalid arguments");
15+
throw new TypeError("Invalid number");
1616
}
1717

1818
bits = descriptor.bits === true;
@@ -51,6 +51,10 @@
5151
e = 8;
5252
}
5353

54+
if (output === "exponent") {
55+
return e;
56+
}
57+
5458
// Zero is now a special case because bytes divide by 1
5559
if (num === 0) {
5660
result[0] = 0;
@@ -88,25 +92,23 @@
8892
// Applying custom symbol
8993
result[1] = symbols[result[1]] || result[1];
9094

95+
if (locale === true) {
96+
result[0] = result[0].toLocaleString();
97+
} else if (locale.length > 0) {
98+
result[0] = result[0].toLocaleString(locale);
99+
} else if (separator.length > 0) {
100+
result[0] = result[0].toString().replace(".", separator);
101+
}
102+
91103
// Returning Array, Object, or String (default)
92104
if (output === "array") {
93105
return result;
94106
}
95107

96-
if (output === "exponent") {
97-
return e;
98-
}
99-
100108
if (full) {
101109
result[1] = fullforms[e] ? fullforms[e] : fullform[standard][e] + (bits ? "bit" : "byte") + (result[0] === 1 ? "" : "s");
102110
}
103111

104-
if (locale.length > 0) {
105-
result[0] = result[0].toLocaleString(locale);
106-
} else if (separator.length > 0) {
107-
result[0] = result[0].toString().replace(".", separator);
108-
}
109-
110112
if (output === "object") {
111113
return {value: result[0], symbol: result[1]};
112114
}

test/filesize_test.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -156,8 +156,9 @@ exports.filesize = {
156156
test.done();
157157
},
158158
locale: function (test) {
159-
test.expect(2);
159+
test.expect(3);
160160
test.equal(filesize(1040, {locale: ""}), "1.02 KB", "Should be '1.02 KB'");
161+
test.equal(filesize(1040, {locale: true}), Number(1.02).toLocaleString() + " KB", "Should be '" + Number(1.02).toLocaleString() + " KB'");
161162
test.equal(filesize(1040, {locale: "de"}), Number(1.02).toLocaleString("de") + " KB", "Should be '" + Number(1.02).toLocaleString("de") + " KB'");
162163
test.done();
163164
}

0 commit comments

Comments
 (0)