Skip to content

Commit 70048cb

Browse files
committed
Handling bits, and base collision
1 parent 1a1a18d commit 70048cb

7 files changed

+33
-18
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ _***(number)***_ Decimal place, default is `2`
2727
_***(string)***_ Character between the `result` and `suffix`, default is `" "`
2828

2929
### standard
30-
_***(string)***_ Standard unit of measure, can be `iec` or `jedec`, default is `jedec`
30+
_***(string)***_ Standard unit of measure, can be `iec` or `jedec`, default is `jedec`; can be overruled by `base`
3131

3232
### symbols
3333
_***(object)***_ Dictionary of SI/JEDEC symbols to replace for localization, defaults to english if no match is found

lib/filesize.es6.js

+10-5
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,14 @@
88
(function (global) {
99
const b = /^(b|B)$/;
1010
const symbol = {
11-
bits: ["b", "Kb", "Mb", "Gb", "Tb", "Pb", "Eb", "Zb", "Yb"],
12-
"bytes-jedec": ["B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"],
13-
"bytes-iec": ["B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB"]
11+
iec: {
12+
bits: ["b", "Kib", "Mib", "Gib", "Tib", "Pib", "Eib", "Zib", "Yib"],
13+
bytes: ["B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB"]
14+
},
15+
jedec: {
16+
bits: ["b", "Kb", "Mb", "Gb", "Tb", "Pb", "Eb", "Zb", "Yb"],
17+
bytes: ["B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"]
18+
}
1419
};
1520

1621
/**
@@ -36,7 +41,7 @@ function filesize (arg, descriptor = {}) {
3641
round = descriptor.round !== undefined ? descriptor.round : unix ? 1 : 2;
3742
spacer = descriptor.spacer !== undefined ? descriptor.spacer : unix ? "" : " ";
3843
symbols = descriptor.symbols || descriptor.suffixes || {};
39-
standard = descriptor.standard || "jedec";
44+
standard = base === 2 ? descriptor.standard || "jedec" : "jedec";
4045
output = descriptor.output || "string";
4146
e = descriptor.exponent !== undefined ? descriptor.exponent : -1;
4247
num = Number(arg);
@@ -79,7 +84,7 @@ function filesize (arg, descriptor = {}) {
7984
}
8085

8186
result[0] = Number(val.toFixed(e > 0 ? round : 0));
82-
result[1] = base === 10 && e === 1 ? bits ? "kb" : "kB" : symbol[bits ? "bits" : "bytes-" + standard][e];
87+
result[1] = base === 10 && e === 1 ? bits ? "kb" : "kB" : symbol[standard][bits ? "bits" : "bytes"][e];
8388

8489
if (unix) {
8590
result[1] = standard === "jedec" ? result[1].charAt(0) : result[1].length > 1 ? result[1].replace(/B$/, "") : result[1];

lib/filesize.js

+10-5
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,14 @@
1010
(function (global) {
1111
var b = /^(b|B)$/;
1212
var symbol = {
13-
bits: ["b", "Kb", "Mb", "Gb", "Tb", "Pb", "Eb", "Zb", "Yb"],
14-
"bytes-jedec": ["B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"],
15-
"bytes-iec": ["B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB"]
13+
iec: {
14+
bits: ["b", "Kib", "Mib", "Gib", "Tib", "Pib", "Eib", "Zib", "Yib"],
15+
bytes: ["B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB"]
16+
},
17+
jedec: {
18+
bits: ["b", "Kb", "Mb", "Gb", "Tb", "Pb", "Eb", "Zb", "Yb"],
19+
bytes: ["B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"]
20+
}
1621
};
1722

1823
/**
@@ -51,7 +56,7 @@
5156
round = descriptor.round !== undefined ? descriptor.round : unix ? 1 : 2;
5257
spacer = descriptor.spacer !== undefined ? descriptor.spacer : unix ? "" : " ";
5358
symbols = descriptor.symbols || descriptor.suffixes || {};
54-
standard = descriptor.standard || "jedec";
59+
standard = base === 2 ? descriptor.standard || "jedec" : "jedec";
5560
output = descriptor.output || "string";
5661
e = descriptor.exponent !== undefined ? descriptor.exponent : -1;
5762
num = Number(arg);
@@ -94,7 +99,7 @@
9499
}
95100

96101
result[0] = Number(val.toFixed(e > 0 ? round : 0));
97-
result[1] = base === 10 && e === 1 ? bits ? "kb" : "kB" : symbol[bits ? "bits" : "bytes-" + standard][e];
102+
result[1] = base === 10 && e === 1 ? bits ? "kb" : "kB" : symbol[standard][bits ? "bits" : "bytes"][e];
98103

99104
if (unix) {
100105
result[1] = standard === "jedec" ? result[1].charAt(0) : result[1].length > 1 ? result[1].replace(/B$/, "") : result[1];

lib/filesize.min.js

+1-1
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.

0 commit comments

Comments
 (0)