Skip to content

Commit 22c3911

Browse files
committed
Returning original logic, taking tests from 8.x.x, creating a constants.js & moving all strings into it as named exports
1 parent 70bb6a1 commit 22c3911

12 files changed

+501
-359
lines changed

lib/filesize.es6.js

+40-18
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,28 @@
1111
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.filesize = factory());
1212
})(this, (function () { 'use strict';
1313

14+
const ARRAY = "array";
15+
const BIT = "bit";
16+
const BITS = "bits";
17+
const BYTE = "byte";
18+
const BYTES = "bytes";
19+
const EMPTY = "";
20+
const EXPONENT = "exponent";
21+
const FUNCTION = "function";
22+
const IEC = "iec";
23+
const INVALID_NUMBER = "Invalid number";
24+
const INVALID_ROUND = "Invalid rounding method";
25+
const JEDEC = "jedec";
26+
const OBJECT = "object";
27+
const PERIOD = ".";
28+
const ROUND = "round";
29+
const S = "s";
30+
const SI_KBIT = "kbit";
31+
const SI_KBYTE = "kB";
32+
const SPACE = " ";
33+
const STRING = "string";
34+
const ZERO = "0";
35+
1436
const strings = {
1537
symbol: {
1638
iec: {
@@ -36,23 +58,23 @@
3658
* @param {Object} descriptor [Optional] Flags
3759
* @return {String} Readable file size String
3860
*/
39-
function filesize (arg, {bits = false, pad = false, base = -1, round = 2, locale = "", localeOptions = {}, separator = "", spacer = " ", symbols = {}, standard = "", output = "string", fullform = false, fullforms = [], exponent = -1, roundingMethod = "round", precision = 0} = {}) {
61+
function filesize (arg, {bits = false, pad = false, base = -1, round = 2, locale = EMPTY, localeOptions = {}, separator = EMPTY, spacer = SPACE, symbols = {}, standard = EMPTY, output = STRING, fullform = false, fullforms = [], exponent = -1, roundingMethod = ROUND, precision = 0} = {}) {
4062
let e = exponent,
4163
num = Number(arg),
4264
result = [],
4365
val = 0,
44-
u = "";
66+
u = EMPTY;
4567

4668
// Sync base & standard
4769
if (base === -1 && standard.length === 0) {
4870
base = 10;
49-
standard = "jedec";
71+
standard = JEDEC;
5072
} else if (base === -1 && standard.length > 0) {
51-
standard = standard === "iec" ? "iec" : "jedec";
52-
base = standard === "iec" ? 2 : 10;
73+
standard = standard === IEC ? IEC : JEDEC;
74+
base = standard === IEC ? 2 : 10;
5375
} else {
5476
base = base === 2 ? 2 : 10;
55-
standard = base === 10 ? "jedec" : "iec";
77+
standard = base === 10 ? JEDEC : IEC;
5678
}
5779

5880
const ceil = base === 10 ? 1000 : 1024,
@@ -61,11 +83,11 @@
6183
roundingFunc = Math[roundingMethod];
6284

6385
if (isNaN(arg)) {
64-
throw new TypeError("Invalid number");
86+
throw new TypeError(INVALID_NUMBER);
6587
}
6688

67-
if (typeof roundingFunc !== "function") {
68-
throw new TypeError("Invalid rounding method");
89+
if (typeof roundingFunc !== FUNCTION) {
90+
throw new TypeError(INVALID_ROUND);
6991
}
7092

7193
// Flipping a negative number to determine the size
@@ -91,14 +113,14 @@
91113
e = 8;
92114
}
93115

94-
if (output === "exponent") {
116+
if (output === EXPONENT) {
95117
return e;
96118
}
97119

98120
// Zero is now a special case because bytes divide by 1
99121
if (num === 0) {
100122
result[0] = 0;
101-
u = result[1] = strings.symbol[standard][bits ? "bits" : "bytes"][e];
123+
u = result[1] = strings.symbol[standard][bits ? BITS : BYTES][e];
102124
} else {
103125
val = num / (base === 2 ? Math.pow(2, e * 10) : Math.pow(1000, e));
104126

@@ -119,7 +141,7 @@
119141
e++;
120142
}
121143

122-
u = result[1] = strings.symbol[standard][bits ? "bits" : "bytes"][e];
144+
u = result[1] = base === 10 && e === 1 ? bits ? SI_KBIT : SI_KBYTE : strings.symbol[standard][bits ? BITS : BYTES][e];
123145
}
124146

125147
// Decorating a 'diff'
@@ -140,25 +162,25 @@
140162
} else if (locale.length > 0) {
141163
result[0] = result[0].toLocaleString(locale, localeOptions);
142164
} else if (separator.length > 0) {
143-
result[0] = result[0].toString().replace(".", separator);
165+
result[0] = result[0].toString().replace(PERIOD, separator);
144166
}
145167

146168
if (pad && Number.isInteger(result[0]) === false && round > 0) {
147-
const x = separator || ".",
169+
const x = separator || PERIOD,
148170
tmp = result[0].toString().split(x),
149-
s = tmp[1] || "",
171+
s = tmp[1] || EMPTY,
150172
l = s.length,
151173
n = round - l;
152174

153-
result[0] = `${tmp[0]}${x}${s.padEnd(l + n, "0")}`;
175+
result[0] = `${tmp[0]}${x}${s.padEnd(l + n, ZERO)}`;
154176
}
155177

156178
if (full) {
157-
result[1] = fullforms[e] ? fullforms[e] : strings.fullform[standard][e] + (bits ? "bit" : "byte") + (result[0] === 1 ? "" : "s");
179+
result[1] = fullforms[e] ? fullforms[e] : strings.fullform[standard][e] + (bits ? BIT : BYTE) + (result[0] === 1 ? EMPTY : S);
158180
}
159181

160182
// Returning Array, Object, or String (default)
161-
return output === "array" ? result : output === "object" ? {value: result[0], symbol: result[1], exponent: e, unit: u} : result.join(spacer);
183+
return output === ARRAY ? result : output === OBJECT ? {value: result[0], symbol: result[1], exponent: e, unit: u} : result.join(spacer);
162184
}
163185

164186
// Partial application for functional programming

lib/filesize.es6.min.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/filesize.es6.min.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/filesize.esm.js

+40-18
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,28 @@
55
* @license BSD-3-Clause
66
* @version 9.0.8
77
*/
8+
const ARRAY = "array";
9+
const BIT = "bit";
10+
const BITS = "bits";
11+
const BYTE = "byte";
12+
const BYTES = "bytes";
13+
const EMPTY = "";
14+
const EXPONENT = "exponent";
15+
const FUNCTION = "function";
16+
const IEC = "iec";
17+
const INVALID_NUMBER = "Invalid number";
18+
const INVALID_ROUND = "Invalid rounding method";
19+
const JEDEC = "jedec";
20+
const OBJECT = "object";
21+
const PERIOD = ".";
22+
const ROUND = "round";
23+
const S = "s";
24+
const SI_KBIT = "kbit";
25+
const SI_KBYTE = "kB";
26+
const SPACE = " ";
27+
const STRING = "string";
28+
const ZERO = "0";
29+
830
const strings = {
931
symbol: {
1032
iec: {
@@ -30,23 +52,23 @@ const strings = {
3052
* @param {Object} descriptor [Optional] Flags
3153
* @return {String} Readable file size String
3254
*/
33-
function filesize (arg, {bits = false, pad = false, base = -1, round = 2, locale = "", localeOptions = {}, separator = "", spacer = " ", symbols = {}, standard = "", output = "string", fullform = false, fullforms = [], exponent = -1, roundingMethod = "round", precision = 0} = {}) {
55+
function filesize (arg, {bits = false, pad = false, base = -1, round = 2, locale = EMPTY, localeOptions = {}, separator = EMPTY, spacer = SPACE, symbols = {}, standard = EMPTY, output = STRING, fullform = false, fullforms = [], exponent = -1, roundingMethod = ROUND, precision = 0} = {}) {
3456
let e = exponent,
3557
num = Number(arg),
3658
result = [],
3759
val = 0,
38-
u = "";
60+
u = EMPTY;
3961

4062
// Sync base & standard
4163
if (base === -1 && standard.length === 0) {
4264
base = 10;
43-
standard = "jedec";
65+
standard = JEDEC;
4466
} else if (base === -1 && standard.length > 0) {
45-
standard = standard === "iec" ? "iec" : "jedec";
46-
base = standard === "iec" ? 2 : 10;
67+
standard = standard === IEC ? IEC : JEDEC;
68+
base = standard === IEC ? 2 : 10;
4769
} else {
4870
base = base === 2 ? 2 : 10;
49-
standard = base === 10 ? "jedec" : "iec";
71+
standard = base === 10 ? JEDEC : IEC;
5072
}
5173

5274
const ceil = base === 10 ? 1000 : 1024,
@@ -55,11 +77,11 @@ function filesize (arg, {bits = false, pad = false, base = -1, round = 2, locale
5577
roundingFunc = Math[roundingMethod];
5678

5779
if (isNaN(arg)) {
58-
throw new TypeError("Invalid number");
80+
throw new TypeError(INVALID_NUMBER);
5981
}
6082

61-
if (typeof roundingFunc !== "function") {
62-
throw new TypeError("Invalid rounding method");
83+
if (typeof roundingFunc !== FUNCTION) {
84+
throw new TypeError(INVALID_ROUND);
6385
}
6486

6587
// Flipping a negative number to determine the size
@@ -85,14 +107,14 @@ function filesize (arg, {bits = false, pad = false, base = -1, round = 2, locale
85107
e = 8;
86108
}
87109

88-
if (output === "exponent") {
110+
if (output === EXPONENT) {
89111
return e;
90112
}
91113

92114
// Zero is now a special case because bytes divide by 1
93115
if (num === 0) {
94116
result[0] = 0;
95-
u = result[1] = strings.symbol[standard][bits ? "bits" : "bytes"][e];
117+
u = result[1] = strings.symbol[standard][bits ? BITS : BYTES][e];
96118
} else {
97119
val = num / (base === 2 ? Math.pow(2, e * 10) : Math.pow(1000, e));
98120

@@ -113,7 +135,7 @@ function filesize (arg, {bits = false, pad = false, base = -1, round = 2, locale
113135
e++;
114136
}
115137

116-
u = result[1] = strings.symbol[standard][bits ? "bits" : "bytes"][e];
138+
u = result[1] = base === 10 && e === 1 ? bits ? SI_KBIT : SI_KBYTE : strings.symbol[standard][bits ? BITS : BYTES][e];
117139
}
118140

119141
// Decorating a 'diff'
@@ -134,25 +156,25 @@ function filesize (arg, {bits = false, pad = false, base = -1, round = 2, locale
134156
} else if (locale.length > 0) {
135157
result[0] = result[0].toLocaleString(locale, localeOptions);
136158
} else if (separator.length > 0) {
137-
result[0] = result[0].toString().replace(".", separator);
159+
result[0] = result[0].toString().replace(PERIOD, separator);
138160
}
139161

140162
if (pad && Number.isInteger(result[0]) === false && round > 0) {
141-
const x = separator || ".",
163+
const x = separator || PERIOD,
142164
tmp = result[0].toString().split(x),
143-
s = tmp[1] || "",
165+
s = tmp[1] || EMPTY,
144166
l = s.length,
145167
n = round - l;
146168

147-
result[0] = `${tmp[0]}${x}${s.padEnd(l + n, "0")}`;
169+
result[0] = `${tmp[0]}${x}${s.padEnd(l + n, ZERO)}`;
148170
}
149171

150172
if (full) {
151-
result[1] = fullforms[e] ? fullforms[e] : strings.fullform[standard][e] + (bits ? "bit" : "byte") + (result[0] === 1 ? "" : "s");
173+
result[1] = fullforms[e] ? fullforms[e] : strings.fullform[standard][e] + (bits ? BIT : BYTE) + (result[0] === 1 ? EMPTY : S);
152174
}
153175

154176
// Returning Array, Object, or String (default)
155-
return output === "array" ? result : output === "object" ? {value: result[0], symbol: result[1], exponent: e, unit: u} : result.join(spacer);
177+
return output === ARRAY ? result : output === OBJECT ? {value: result[0], symbol: result[1], exponent: e, unit: u} : result.join(spacer);
156178
}
157179

158180
// Partial application for functional programming

lib/filesize.esm.min.js

+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)