Skip to content

Commit

Permalink
Handling bits, and base collision
Browse files Browse the repository at this point in the history
  • Loading branch information
avoidwork committed Apr 23, 2016
1 parent 1a1a18d commit 70048cb
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 18 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ _***(number)***_ Decimal place, default is `2`
_***(string)***_ Character between the `result` and `suffix`, default is `" "`

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

### symbols
_***(object)***_ Dictionary of SI/JEDEC symbols to replace for localization, defaults to english if no match is found
Expand Down
15 changes: 10 additions & 5 deletions lib/filesize.es6.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,14 @@
(function (global) {
const b = /^(b|B)$/;
const symbol = {
bits: ["b", "Kb", "Mb", "Gb", "Tb", "Pb", "Eb", "Zb", "Yb"],
"bytes-jedec": ["B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"],
"bytes-iec": ["B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB"]
iec: {
bits: ["b", "Kib", "Mib", "Gib", "Tib", "Pib", "Eib", "Zib", "Yib"],
bytes: ["B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB"]
},
jedec: {
bits: ["b", "Kb", "Mb", "Gb", "Tb", "Pb", "Eb", "Zb", "Yb"],
bytes: ["B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"]
}
};

/**
Expand All @@ -36,7 +41,7 @@ function filesize (arg, descriptor = {}) {
round = descriptor.round !== undefined ? descriptor.round : unix ? 1 : 2;
spacer = descriptor.spacer !== undefined ? descriptor.spacer : unix ? "" : " ";
symbols = descriptor.symbols || descriptor.suffixes || {};
standard = descriptor.standard || "jedec";
standard = base === 2 ? descriptor.standard || "jedec" : "jedec";
output = descriptor.output || "string";
e = descriptor.exponent !== undefined ? descriptor.exponent : -1;
num = Number(arg);
Expand Down Expand Up @@ -79,7 +84,7 @@ function filesize (arg, descriptor = {}) {
}

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

if (unix) {
result[1] = standard === "jedec" ? result[1].charAt(0) : result[1].length > 1 ? result[1].replace(/B$/, "") : result[1];
Expand Down
15 changes: 10 additions & 5 deletions lib/filesize.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,14 @@
(function (global) {
var b = /^(b|B)$/;
var symbol = {
bits: ["b", "Kb", "Mb", "Gb", "Tb", "Pb", "Eb", "Zb", "Yb"],
"bytes-jedec": ["B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"],
"bytes-iec": ["B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB"]
iec: {
bits: ["b", "Kib", "Mib", "Gib", "Tib", "Pib", "Eib", "Zib", "Yib"],
bytes: ["B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB"]
},
jedec: {
bits: ["b", "Kb", "Mb", "Gb", "Tb", "Pb", "Eb", "Zb", "Yb"],
bytes: ["B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"]
}
};

/**
Expand Down Expand Up @@ -51,7 +56,7 @@
round = descriptor.round !== undefined ? descriptor.round : unix ? 1 : 2;
spacer = descriptor.spacer !== undefined ? descriptor.spacer : unix ? "" : " ";
symbols = descriptor.symbols || descriptor.suffixes || {};
standard = descriptor.standard || "jedec";
standard = base === 2 ? descriptor.standard || "jedec" : "jedec";
output = descriptor.output || "string";
e = descriptor.exponent !== undefined ? descriptor.exponent : -1;
num = Number(arg);
Expand Down Expand Up @@ -94,7 +99,7 @@
}

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

if (unix) {
result[1] = standard === "jedec" ? result[1].charAt(0) : result[1].length > 1 ? result[1].replace(/B$/, "") : result[1];
Expand Down
2 changes: 1 addition & 1 deletion 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.

0 comments on commit 70048cb

Please sign in to comment.