Skip to content

Commit 1a1a18d

Browse files
committed
Adding support for IEC standard via new standard descriptor property (defaults to jedec), fixes #80
1 parent 0521336 commit 1a1a18d

8 files changed

+39
-28
lines changed

README.md

+4
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ _***(number)***_ Decimal place, default is `2`
2626
### spacer
2727
_***(string)***_ Character between the `result` and `suffix`, default is `" "`
2828

29+
### standard
30+
_***(string)***_ Standard unit of measure, can be `iec` or `jedec`, default is `jedec`
31+
2932
### symbols
3033
_***(object)***_ Dictionary of SI/JEDEC symbols to replace for localization, defaults to english if no match is found
3134

@@ -49,6 +52,7 @@ filesize(1, {symbols: {B: "Б"}}); // "1 Б"
4952
filesize(1024); // "1 KB"
5053
filesize(1024, {exponent: 0}); // "1024 B"
5154
filesize(1024, {output: "exponent"}); // 1
55+
filesize(265318, {standard: "iec"}); // "259.1 KiB"
5256
```
5357

5458
## How can I load filesize.js?

lib/filesize.es6.js

+7-5
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@
33
*
44
* @copyright 2016 Jason Mulligan <[email protected]>
55
* @license BSD-3-Clause
6-
* @version 3.2.1
6+
* @version 3.3.0
77
*/
88
(function (global) {
99
const b = /^(b|B)$/;
1010
const symbol = {
1111
bits: ["b", "Kb", "Mb", "Gb", "Tb", "Pb", "Eb", "Zb", "Yb"],
12-
bytes: ["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"]
1314
};
1415

1516
/**
@@ -23,7 +24,7 @@ const symbol = {
2324
function filesize (arg, descriptor = {}) {
2425
let result = [],
2526
val = 0,
26-
e, base, bits, ceil, neg, num, output, round, unix, spacer, symbols;
27+
e, base, bits, ceil, neg, num, output, round, unix, spacer, standard, symbols;
2728

2829
if (isNaN(arg)) {
2930
throw new Error("Invalid arguments");
@@ -35,6 +36,7 @@ function filesize (arg, descriptor = {}) {
3536
round = descriptor.round !== undefined ? descriptor.round : unix ? 1 : 2;
3637
spacer = descriptor.spacer !== undefined ? descriptor.spacer : unix ? "" : " ";
3738
symbols = descriptor.symbols || descriptor.suffixes || {};
39+
standard = descriptor.standard || "jedec";
3840
output = descriptor.output || "string";
3941
e = descriptor.exponent !== undefined ? descriptor.exponent : -1;
4042
num = Number(arg);
@@ -77,10 +79,10 @@ function filesize (arg, descriptor = {}) {
7779
}
7880

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

8284
if (unix) {
83-
result[1] = result[1].charAt(0);
85+
result[1] = standard === "jedec" ? result[1].charAt(0) : result[1].length > 1 ? result[1].replace(/B$/, "") : result[1];
8486

8587
if (b.test(result[1])) {
8688
result[0] = Math.floor(result[0]);

lib/filesize.js

+18-15
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,14 @@
55
*
66
* @copyright 2016 Jason Mulligan <[email protected]>
77
* @license BSD-3-Clause
8-
* @version 3.2.1
8+
* @version 3.3.0
99
*/
1010
(function (global) {
1111
var b = /^(b|B)$/;
1212
var symbol = {
1313
bits: ["b", "Kb", "Mb", "Gb", "Tb", "Pb", "Eb", "Zb", "Yb"],
14-
bytes: ["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"]
1516
};
1617

1718
/**
@@ -27,17 +28,18 @@
2728

2829
var result = [],
2930
val = 0,
30-
e = undefined,
31-
base = undefined,
32-
bits = undefined,
33-
ceil = undefined,
34-
neg = undefined,
35-
num = undefined,
36-
output = undefined,
37-
round = undefined,
38-
unix = undefined,
39-
spacer = undefined,
40-
symbols = undefined;
31+
e = void 0,
32+
base = void 0,
33+
bits = void 0,
34+
ceil = void 0,
35+
neg = void 0,
36+
num = void 0,
37+
output = void 0,
38+
round = void 0,
39+
unix = void 0,
40+
spacer = void 0,
41+
standard = void 0,
42+
symbols = void 0;
4143

4244
if (isNaN(arg)) {
4345
throw new Error("Invalid arguments");
@@ -49,6 +51,7 @@
4951
round = descriptor.round !== undefined ? descriptor.round : unix ? 1 : 2;
5052
spacer = descriptor.spacer !== undefined ? descriptor.spacer : unix ? "" : " ";
5153
symbols = descriptor.symbols || descriptor.suffixes || {};
54+
standard = descriptor.standard || "jedec";
5255
output = descriptor.output || "string";
5356
e = descriptor.exponent !== undefined ? descriptor.exponent : -1;
5457
num = Number(arg);
@@ -91,10 +94,10 @@
9194
}
9295

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

9699
if (unix) {
97-
result[1] = result[1].charAt(0);
100+
result[1] = standard === "jedec" ? result[1].charAt(0) : result[1].length > 1 ? result[1].replace(/B$/, "") : result[1];
98101

99102
if (b.test(result[1])) {
100103
result[0] = Math.floor(result[0]);

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

0 commit comments

Comments
 (0)