5
5
* @license BSD-3-Clause
6
6
* @version 9.0.8
7
7
*/
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
+
8
30
const strings = {
9
31
symbol : {
10
32
iec : {
@@ -30,23 +52,23 @@ const strings = {
30
52
* @param {Object } descriptor [Optional] Flags
31
53
* @return {String } Readable file size String
32
54
*/
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 } = { } ) {
34
56
let e = exponent ,
35
57
num = Number ( arg ) ,
36
58
result = [ ] ,
37
59
val = 0 ,
38
- u = "" ;
60
+ u = EMPTY ;
39
61
40
62
// Sync base & standard
41
63
if ( base === - 1 && standard . length === 0 ) {
42
64
base = 10 ;
43
- standard = "jedec" ;
65
+ standard = JEDEC ;
44
66
} 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 ;
47
69
} else {
48
70
base = base === 2 ? 2 : 10 ;
49
- standard = base === 10 ? "jedec" : "iec" ;
71
+ standard = base === 10 ? JEDEC : IEC ;
50
72
}
51
73
52
74
const ceil = base === 10 ? 1000 : 1024 ,
@@ -55,11 +77,11 @@ function filesize (arg, {bits = false, pad = false, base = -1, round = 2, locale
55
77
roundingFunc = Math [ roundingMethod ] ;
56
78
57
79
if ( isNaN ( arg ) ) {
58
- throw new TypeError ( "Invalid number" ) ;
80
+ throw new TypeError ( INVALID_NUMBER ) ;
59
81
}
60
82
61
- if ( typeof roundingFunc !== "function" ) {
62
- throw new TypeError ( "Invalid rounding method" ) ;
83
+ if ( typeof roundingFunc !== FUNCTION ) {
84
+ throw new TypeError ( INVALID_ROUND ) ;
63
85
}
64
86
65
87
// Flipping a negative number to determine the size
@@ -85,14 +107,14 @@ function filesize (arg, {bits = false, pad = false, base = -1, round = 2, locale
85
107
e = 8 ;
86
108
}
87
109
88
- if ( output === "exponent" ) {
110
+ if ( output === EXPONENT ) {
89
111
return e ;
90
112
}
91
113
92
114
// Zero is now a special case because bytes divide by 1
93
115
if ( num === 0 ) {
94
116
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 ] ;
96
118
} else {
97
119
val = num / ( base === 2 ? Math . pow ( 2 , e * 10 ) : Math . pow ( 1000 , e ) ) ;
98
120
@@ -113,7 +135,7 @@ function filesize (arg, {bits = false, pad = false, base = -1, round = 2, locale
113
135
e ++ ;
114
136
}
115
137
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 ] ;
117
139
}
118
140
119
141
// Decorating a 'diff'
@@ -134,25 +156,25 @@ function filesize (arg, {bits = false, pad = false, base = -1, round = 2, locale
134
156
} else if ( locale . length > 0 ) {
135
157
result [ 0 ] = result [ 0 ] . toLocaleString ( locale , localeOptions ) ;
136
158
} else if ( separator . length > 0 ) {
137
- result [ 0 ] = result [ 0 ] . toString ( ) . replace ( "." , separator ) ;
159
+ result [ 0 ] = result [ 0 ] . toString ( ) . replace ( PERIOD , separator ) ;
138
160
}
139
161
140
162
if ( pad && Number . isInteger ( result [ 0 ] ) === false && round > 0 ) {
141
- const x = separator || "." ,
163
+ const x = separator || PERIOD ,
142
164
tmp = result [ 0 ] . toString ( ) . split ( x ) ,
143
- s = tmp [ 1 ] || "" ,
165
+ s = tmp [ 1 ] || EMPTY ,
144
166
l = s . length ,
145
167
n = round - l ;
146
168
147
- result [ 0 ] = `${ tmp [ 0 ] } ${ x } ${ s . padEnd ( l + n , "0" ) } ` ;
169
+ result [ 0 ] = `${ tmp [ 0 ] } ${ x } ${ s . padEnd ( l + n , ZERO ) } ` ;
148
170
}
149
171
150
172
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 ) ;
152
174
}
153
175
154
176
// 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 ) ;
156
178
}
157
179
158
180
// Partial application for functional programming
0 commit comments