Skip to content

Commit 05d1fcd

Browse files
authored
Merge pull request #129 from avoidwork/addunit
Adding `unit` to `object` output
2 parents 9bf357c + 549e5a6 commit 05d1fcd

9 files changed

+20
-15
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ filesize(265318, {base: 10}); // "265.32 kB"
6262
filesize(265318); // "259.1 KB"
6363
filesize(265318, {round: 0}); // "259 KB"
6464
filesize(265318, {output: "array"}); // [259.1, "KB"]
65-
filesize(265318, {output: "object"}); // {value: 259.1, symbol: "KB", exponent: 1}
65+
filesize(265318, {output: "object"}); // {value: 259.1, symbol: "KB", exponent: 1, unit: "KB"}
6666
filesize(1, {symbols: {B: "Б"}}); // "1 Б"
6767
filesize(1024); // "1 KB"
6868
filesize(1024, {exponent: 0}); // "1024 B"

lib/filesize.es6.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
function filesize (arg, descriptor = {}) {
3939
let result = [],
4040
val = 0,
41-
e, base, bits, ceil, full, fullforms, locale, localeOptions, neg, num, output, pad, round, unix, separator, spacer, standard, symbols;
41+
e, base, bits, ceil, full, fullforms, locale, localeOptions, neg, num, output, pad, round, u, unix, separator, spacer, standard, symbols;
4242

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

112-
result[1] = base === 10 && e === 1 ? bits ? "kb" : "kB" : symbol[standard][bits ? "bits" : "bytes"][e];
112+
u = result[1] = base === 10 && e === 1 ? bits ? "kb" : "kB" : symbol[standard][bits ? "bits" : "bytes"][e];
113113

114114
if (unix) {
115115
result[1] = standard === "jedec" ? result[1].charAt(0) : e > 0 ? result[1].replace(/B$/, "") : result[1];
@@ -147,7 +147,7 @@
147147
}
148148

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

153153
if (pad && Number.isInteger(result[0]) === false && round > 0) {

lib/filesize.es6.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/filesize.es6.min.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/filesize.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
output,
5353
pad,
5454
round,
55+
u,
5556
unix,
5657
separator,
5758
spacer,
@@ -126,7 +127,7 @@
126127
e++;
127128
}
128129

129-
result[1] = base === 10 && e === 1 ? bits ? "kb" : "kB" : symbol[standard][bits ? "bits" : "bytes"][e];
130+
u = result[1] = base === 10 && e === 1 ? bits ? "kb" : "kB" : symbol[standard][bits ? "bits" : "bytes"][e];
130131

131132
if (unix) {
132133
result[1] = standard === "jedec" ? result[1].charAt(0) : e > 0 ? result[1].replace(/B$/, "") : result[1];
@@ -167,7 +168,8 @@
167168
return {
168169
value: result[0],
169170
symbol: result[1],
170-
exponent: e
171+
exponent: e,
172+
unit: u
171173
};
172174
}
173175

lib/filesize.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/filesize.min.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/filesize.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ const b = /^(b|B)$/,
2525
function filesize (arg, descriptor = {}) {
2626
let result = [],
2727
val = 0,
28-
e, base, bits, ceil, full, fullforms, locale, localeOptions, neg, num, output, pad, round, unix, separator, spacer, standard, symbols;
28+
e, base, bits, ceil, full, fullforms, locale, localeOptions, neg, num, output, pad, round, u, unix, separator, spacer, standard, symbols;
2929

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

99-
result[1] = base === 10 && e === 1 ? bits ? "kb" : "kB" : symbol[standard][bits ? "bits" : "bytes"][e];
99+
u = result[1] = base === 10 && e === 1 ? bits ? "kb" : "kB" : symbol[standard][bits ? "bits" : "bytes"][e];
100100

101101
if (unix) {
102102
result[1] = standard === "jedec" ? result[1].charAt(0) : e > 0 ? result[1].replace(/B$/, "") : result[1];
@@ -134,7 +134,7 @@ function filesize (arg, descriptor = {}) {
134134
}
135135

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

140140
if (pad && Number.isInteger(result[0]) === false && round > 0) {

test/filesize_test.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ exports.filesize = {
1616
done();
1717
},
1818
base2: function (test) {
19-
test.expect(46);
19+
test.expect(48);
2020
test.equal(filesize(this.kilobit), "500 B", "Should be '500 B'");
2121
test.equal(filesize(this.kilobit, {round: 1}), "500 B", "Should be '500 B'");
2222
test.equal(filesize(this.kilobit, {round: 1, spacer: ""}), "500B", "Should be '500B'");
@@ -36,7 +36,9 @@ exports.filesize = {
3636
test.equal(filesize(this.kilobyte, {round: 1, bits: true}), "8 Kb", "Should be '8 Kb'");
3737
test.equal(filesize(this.kilobyte, {unix: true, bits: true}), "8K", "Should be '8K'");
3838
test.equal(filesize(this.kilobyte, {exponent: 0}), "1024 B", "Should be '1024 B'");
39+
test.equal(filesize(this.kilobyte, {exponent: 0, output: "object"}).unit, "B", "Should be 'B'");
3940
test.equal(filesize(this.kilobyte, {output: "exponent"}), 1, "Should be '1'");
41+
test.equal(filesize(this.kilobyte, {output: "object"}).unit, "KB", "Should be 'KB'");
4042
test.equal(filesize(this.neg), "-1 KB", "Should be '-1 KB'");
4143
test.equal(filesize(this.neg, {round: 1}), "-1 KB", "Should be '-1 KB'");
4244
test.equal(filesize(this.neg, {round: 1, spacer: ""}), "-1KB", "Should be '-1KB'");
@@ -132,12 +134,13 @@ exports.filesize = {
132134
test.done();
133135
},
134136
fullform: function (test) {
135-
test.expect(7);
137+
test.expect(8);
136138
test.equal(filesize(0, {fullform: true}), "0 bytes", "Should be '0 bytes'");
137139
test.equal(filesize(1, {bits: true, base: 10, fullform: true}), "8 bits", "Should be '8 bits'");
138140
test.equal(filesize(1, {base: 10, fullform: true}), "1 byte", "Should be '1 byte'");
139141
test.equal(filesize(this.kilobyte, {fullform: true}), "1 kilobyte", "Should be '1 kilobyte'");
140142
test.equal(filesize(this.kilobyte, {fullform: true, standard: "iec"}), "1 kibibyte", "Should be '1 kibibyte'");
143+
test.equal(filesize(this.kilobyte, {fullform: true, standard: "iec", output: "object"}).unit, "KiB", "Should be 'KiB'");
141144
test.equal(filesize(this.kilobyte * 1.3, {
142145
fullform: true,
143146
standard: "iec"

0 commit comments

Comments
 (0)