Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding unit to object output #129

Merged
merged 1 commit into from
Apr 13, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -62,7 +62,7 @@ filesize(265318, {base: 10}); // "265.32 kB"
filesize(265318); // "259.1 KB"
filesize(265318, {round: 0}); // "259 KB"
filesize(265318, {output: "array"}); // [259.1, "KB"]
filesize(265318, {output: "object"}); // {value: 259.1, symbol: "KB", exponent: 1}
filesize(265318, {output: "object"}); // {value: 259.1, symbol: "KB", exponent: 1, unit: "KB"}
filesize(1, {symbols: {B: "Б"}}); // "1 Б"
filesize(1024); // "1 KB"
filesize(1024, {exponent: 0}); // "1024 B"
6 changes: 3 additions & 3 deletions lib/filesize.es6.js
Original file line number Diff line number Diff line change
@@ -38,7 +38,7 @@
function filesize (arg, descriptor = {}) {
let result = [],
val = 0,
e, base, bits, ceil, full, fullforms, locale, localeOptions, neg, num, output, pad, round, unix, separator, spacer, standard, symbols;
e, base, bits, ceil, full, fullforms, locale, localeOptions, neg, num, output, pad, round, u, unix, separator, spacer, standard, symbols;

if (isNaN(arg)) {
throw new TypeError("Invalid number");
@@ -109,7 +109,7 @@
e++;
}

result[1] = base === 10 && e === 1 ? bits ? "kb" : "kB" : symbol[standard][bits ? "bits" : "bytes"][e];
u = 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) : e > 0 ? result[1].replace(/B$/, "") : result[1];
@@ -147,7 +147,7 @@
}

if (output === "object") {
return {value: result[0], symbol: result[1], exponent: e};
return {value: result[0], symbol: result[1], exponent: e, unit: u};
}

if (pad && Number.isInteger(result[0]) === false && round > 0) {
2 changes: 1 addition & 1 deletion lib/filesize.es6.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.es6.min.js.map

Large diffs are not rendered by default.

6 changes: 4 additions & 2 deletions lib/filesize.js
Original file line number Diff line number Diff line change
@@ -52,6 +52,7 @@
output,
pad,
round,
u,
unix,
separator,
spacer,
@@ -126,7 +127,7 @@
e++;
}

result[1] = base === 10 && e === 1 ? bits ? "kb" : "kB" : symbol[standard][bits ? "bits" : "bytes"][e];
u = 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) : e > 0 ? result[1].replace(/B$/, "") : result[1];
@@ -167,7 +168,8 @@
return {
value: result[0],
symbol: result[1],
exponent: e
exponent: e,
unit: u
};
}

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

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions src/filesize.js
Original file line number Diff line number Diff line change
@@ -25,7 +25,7 @@ const b = /^(b|B)$/,
function filesize (arg, descriptor = {}) {
let result = [],
val = 0,
e, base, bits, ceil, full, fullforms, locale, localeOptions, neg, num, output, pad, round, unix, separator, spacer, standard, symbols;
e, base, bits, ceil, full, fullforms, locale, localeOptions, neg, num, output, pad, round, u, unix, separator, spacer, standard, symbols;

if (isNaN(arg)) {
throw new TypeError("Invalid number");
@@ -96,7 +96,7 @@ function filesize (arg, descriptor = {}) {
e++;
}

result[1] = base === 10 && e === 1 ? bits ? "kb" : "kB" : symbol[standard][bits ? "bits" : "bytes"][e];
u = 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) : e > 0 ? result[1].replace(/B$/, "") : result[1];
@@ -134,7 +134,7 @@ function filesize (arg, descriptor = {}) {
}

if (output === "object") {
return {value: result[0], symbol: result[1], exponent: e};
return {value: result[0], symbol: result[1], exponent: e, unit: u};
}

if (pad && Number.isInteger(result[0]) === false && round > 0) {
7 changes: 5 additions & 2 deletions test/filesize_test.js
Original file line number Diff line number Diff line change
@@ -16,7 +16,7 @@ exports.filesize = {
done();
},
base2: function (test) {
test.expect(46);
test.expect(48);
test.equal(filesize(this.kilobit), "500 B", "Should be '500 B'");
test.equal(filesize(this.kilobit, {round: 1}), "500 B", "Should be '500 B'");
test.equal(filesize(this.kilobit, {round: 1, spacer: ""}), "500B", "Should be '500B'");
@@ -36,7 +36,9 @@ exports.filesize = {
test.equal(filesize(this.kilobyte, {round: 1, bits: true}), "8 Kb", "Should be '8 Kb'");
test.equal(filesize(this.kilobyte, {unix: true, bits: true}), "8K", "Should be '8K'");
test.equal(filesize(this.kilobyte, {exponent: 0}), "1024 B", "Should be '1024 B'");
test.equal(filesize(this.kilobyte, {exponent: 0, output: "object"}).unit, "B", "Should be 'B'");
test.equal(filesize(this.kilobyte, {output: "exponent"}), 1, "Should be '1'");
test.equal(filesize(this.kilobyte, {output: "object"}).unit, "KB", "Should be 'KB'");
test.equal(filesize(this.neg), "-1 KB", "Should be '-1 KB'");
test.equal(filesize(this.neg, {round: 1}), "-1 KB", "Should be '-1 KB'");
test.equal(filesize(this.neg, {round: 1, spacer: ""}), "-1KB", "Should be '-1KB'");
@@ -132,12 +134,13 @@ exports.filesize = {
test.done();
},
fullform: function (test) {
test.expect(7);
test.expect(8);
test.equal(filesize(0, {fullform: true}), "0 bytes", "Should be '0 bytes'");
test.equal(filesize(1, {bits: true, base: 10, fullform: true}), "8 bits", "Should be '8 bits'");
test.equal(filesize(1, {base: 10, fullform: true}), "1 byte", "Should be '1 byte'");
test.equal(filesize(this.kilobyte, {fullform: true}), "1 kilobyte", "Should be '1 kilobyte'");
test.equal(filesize(this.kilobyte, {fullform: true, standard: "iec"}), "1 kibibyte", "Should be '1 kibibyte'");
test.equal(filesize(this.kilobyte, {fullform: true, standard: "iec", output: "object"}).unit, "KiB", "Should be 'KiB'");
test.equal(filesize(this.kilobyte * 1.3, {
fullform: true,
standard: "iec"