Skip to content

Commit 26d4972

Browse files
committed
Adding locale option which overrides separator, fixes #96
1 parent 81e5499 commit 26d4972

11 files changed

+40
-20
lines changed

README.md

+4
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ _*(boolean)*_ Enables full form of unit of measure, default is `false`
2323
### fullforms
2424
_*(array)*_ Array of full form overrides, default is `[]`
2525

26+
### locale (overrides 'separator')
27+
_*(string)*_ BCP 47 language tag, default is `""`
28+
2629
### output
2730
_*(string)*_ Output of function (`array`, `exponent`, `object`, or `string`), default is `string`
2831

@@ -62,6 +65,7 @@ filesize(265318, {standard: "iec"}); // "259.1 KiB"
6265
filesize(265318, {standard: "iec", fullform: true}); // "259.1 kibibytes"
6366
filesize(12, {fullform: true, fullforms: ["байтов"]}); // "12 байтов"
6467
filesize(265318, {separator: ","}); // "259,1 KB"
68+
filesize(265318, {locale: "de"}); // "259,1 KB"
6569
```
6670

6771
## Partial Application

lib/filesize.es6.js

+7-4
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.0.0
6+
* @version 4.1.0
77
*/
88
(function (global) {
99
const b = /^(b|B)$/,
@@ -33,7 +33,7 @@
3333
function filesize (arg, descriptor = {}) {
3434
let result = [],
3535
val = 0,
36-
e, base, bits, ceil, full, fullforms, neg, num, output, round, unix, separator, spacer, standard, symbols;
36+
e, base, bits, ceil, full, fullforms, locale, neg, num, output, round, unix, separator, spacer, standard, symbols;
3737

3838
if (isNaN(arg)) {
3939
throw new Error("Invalid arguments");
@@ -43,7 +43,8 @@
4343
unix = descriptor.unix === true;
4444
base = descriptor.base || 2;
4545
round = descriptor.round !== void 0 ? descriptor.round : unix ? 1 : 2;
46-
separator = descriptor.separator !== void 0 ? descriptor.separator || "" : "";
46+
locale = descriptor.locale !== void 0 ? descriptor.locale : "";
47+
separator = descriptor.separator !== void 0 ? descriptor.separator : "";
4748
spacer = descriptor.spacer !== void 0 ? descriptor.spacer : unix ? "" : " ";
4849
symbols = descriptor.symbols || {};
4950
standard = base === 2 ? descriptor.standard || "jedec" : "jedec";
@@ -128,7 +129,9 @@
128129
result[1] = fullforms[e] ? fullforms[e] : fullform[standard][e] + (bits ? "bit" : "byte") + (result[0] === 1 ? "" : "s");
129130
}
130131

131-
if (separator.length > 0) {
132+
if (locale.length > 0) {
133+
result[0] = result[0].toLocaleString(locale);
134+
} else if (separator.length > 0) {
132135
result[0] = result[0].toString().replace(".", separator);
133136
}
134137

lib/filesize.es6.min.js

+4-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

+7-3
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.0.0
8+
* @version 4.1.0
99
*/
1010
(function (global) {
1111
var b = /^(b|B)$/,
@@ -43,6 +43,7 @@
4343
ceil = void 0,
4444
full = void 0,
4545
fullforms = void 0,
46+
locale = void 0,
4647
neg = void 0,
4748
num = void 0,
4849
output = void 0,
@@ -61,7 +62,8 @@
6162
unix = descriptor.unix === true;
6263
base = descriptor.base || 2;
6364
round = descriptor.round !== void 0 ? descriptor.round : unix ? 1 : 2;
64-
separator = descriptor.separator !== void 0 ? descriptor.separator || "" : "";
65+
locale = descriptor.locale !== void 0 ? descriptor.locale : "";
66+
separator = descriptor.separator !== void 0 ? descriptor.separator : "";
6567
spacer = descriptor.spacer !== void 0 ? descriptor.spacer : unix ? "" : " ";
6668
symbols = descriptor.symbols || {};
6769
standard = base === 2 ? descriptor.standard || "jedec" : "jedec";
@@ -146,7 +148,9 @@
146148
result[1] = fullforms[e] ? fullforms[e] : fullform[standard][e] + (bits ? "bit" : "byte") + (result[0] === 1 ? "" : "s");
147149
}
148150

149-
if (separator.length > 0) {
151+
if (locale.length > 0) {
152+
result[0] = result[0].toLocaleString(locale);
153+
} else if (separator.length > 0) {
150154
result[0] = result[0].toString().replace(".", separator);
151155
}
152156

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.0.0",
4+
"version": "4.1.0",
55
"homepage": "https://filesizejs.com",
66
"author": "Jason Mulligan <[email protected]>",
77
"repository": {

src/filesize.js

+6-3
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
function filesize (arg, descriptor = {}) {
1010
let result = [],
1111
val = 0,
12-
e, base, bits, ceil, full, fullforms, neg, num, output, round, unix, separator, spacer, standard, symbols;
12+
e, base, bits, ceil, full, fullforms, locale, neg, num, output, round, unix, separator, spacer, standard, symbols;
1313

1414
if (isNaN(arg)) {
1515
throw new Error("Invalid arguments");
@@ -19,7 +19,8 @@
1919
unix = descriptor.unix === true;
2020
base = descriptor.base || 2;
2121
round = descriptor.round !== void 0 ? descriptor.round : unix ? 1 : 2;
22-
separator = descriptor.separator !== void 0 ? descriptor.separator || "" : "";
22+
locale = descriptor.locale !== void 0 ? descriptor.locale : "";
23+
separator = descriptor.separator !== void 0 ? descriptor.separator : "";
2324
spacer = descriptor.spacer !== void 0 ? descriptor.spacer : unix ? "" : " ";
2425
symbols = descriptor.symbols || {};
2526
standard = base === 2 ? descriptor.standard || "jedec" : "jedec";
@@ -104,7 +105,9 @@
104105
result[1] = fullforms[e] ? fullforms[e] : fullform[standard][e] + (bits ? "bit" : "byte") + (result[0] === 1 ? "" : "s");
105106
}
106107

107-
if (separator.length > 0) {
108+
if (locale.length > 0) {
109+
result[0] = result[0].toLocaleString(locale);
110+
} else if (separator.length > 0) {
108111
result[0] = result[0].toString().replace(".", separator);
109112
}
110113

test/filesize_test.js

+6
Original file line numberDiff line numberDiff line change
@@ -154,5 +154,11 @@ exports.filesize = {
154154
test.equal(filesize(1040, {separator: ""}), "1.02 KB", "Should be '1.02 KB'");
155155
test.equal(filesize(1040, {separator: ","}), "1,02 KB", "Should be '1,02 KB'");
156156
test.done();
157+
},
158+
locale: function (test) {
159+
test.expect(2);
160+
test.equal(filesize(1040, {locale: ""}), "1.02 KB", "Should be '1.02 KB'");
161+
test.equal(filesize(1040, {locale: "de"}), Number(1.02).toLocaleString("de") + " KB", "Should be '" + Number(1.02).toLocaleString("de") + " KB'");
162+
test.done();
157163
}
158164
};

0 commit comments

Comments
 (0)