6
6
* @license BSD-3 <https://raw.github.com/avoidwork/filesize.js/master/LICENSE>
7
7
* @link http://filesizejs.com
8
8
* @module filesize
9
- * @version 1.8 .0
9
+ * @version 1.9 .0
10
10
*/
11
- ( function ( global ) {
11
+ ( function ( global ) {
12
12
"use strict" ;
13
13
14
- var base = 10 ,
15
- sizes = [ [ "B" , 1 ] , [ "Kb" , 128 ] , [ "KB" , 1024 ] , [ "Mb" , 131072 ] , [ "MB" , 1.049e+6 ] , [ "Gb" , 1.342e+8 ] , [ "GB" , 1.074e+9 ] , [ "Tb" , 1.374e+11 ] , [ "TB" , 1.1e+12 ] , [ "Pb" , 1.407e+14 ] , [ "PB" , 1.126e+15 ] ] ,
16
- nth = sizes . length ,
17
- regex = / \. ( .* ) / ,
18
- bit = / b $ / ,
19
- zero = / ^ 0 $ / ;
14
+ var base = 10 ,
15
+ right = / \. ( .* ) / ,
16
+ bit = / b $ / ,
17
+ zero = / ^ 0 $ / ,
18
+ options = {
19
+ all : {
20
+ increments : [ [ "B" , 1 ] , [ "Kb" , 128 ] , [ "KB" , 1024 ] , [ "Mb" , 131072 ] , [ "MB" , 1.049e+6 ] , [ "Gb" , 1.342e+8 ] , [ "GB" , 1.074e+9 ] , [ "Tb" , 1.374e+11 ] , [ "TB" , 1.1e+12 ] , [ "Pb" , 1.407e+14 ] , [ "PB" , 1.126e+15 ] ] ,
21
+ nth : 11
22
+ } ,
23
+ bitless : {
24
+ increments : [ [ "B" , 1 ] , [ "KB" , 1024 ] , [ "MB" , 1.049e+6 ] , [ "GB" , 1.074e+9 ] , [ "TB" , 1.1e+12 ] , [ "PB" , 1.126e+15 ] ] ,
25
+ nth : 6
26
+ }
27
+ } ;
20
28
21
29
/**
22
30
* filesize
23
31
*
24
32
* @param {Mixed } arg String, Int or Float to transform
25
33
* @param {Number } pos [Optional] Position to round to, defaults to 2 if short is ommitted
26
34
* @param {Boolean } short [Optional] Shorthand output, similar to "ls -lh", overrides pos to 1
35
+ * @param {Boolean } bits [Optional] Determines if `bit` sizes are used for result calculation, default is true
27
36
* @return {String } Readable file size String
28
37
*/
29
38
function filesize ( arg ) {
30
39
var result = "" ,
31
- i = nth ,
32
- neg , num , pos , short , size , suffix , z ;
40
+ bits = true ,
41
+ i , neg , num , pos , short , size , sizes , suffix , z ;
33
42
34
- if ( arguments [ 2 ] !== undefined ) {
43
+ // Determining arguments
44
+ if ( arguments [ 3 ] !== undefined ) {
35
45
pos = arguments [ 1 ] ;
36
46
short = arguments [ 2 ] ;
47
+ bits = arguments [ 3 ] ;
37
48
}
38
- else typeof arguments [ 1 ] === "boolean" ? short = arguments [ 1 ] : pos = arguments [ 1 ] ;
49
+ else {
50
+ typeof arguments [ 1 ] === "boolean" ? short = arguments [ 1 ] : pos = arguments [ 1 ] ;
39
51
40
- if ( isNaN ( arg ) || ( pos !== undefined && isNaN ( pos ) ) ) throw Error ( "Invalid arguments" ) ;
52
+ if ( typeof arguments [ 2 ] === "boolean" ) {
53
+ bits = arguments [ 2 ] ;
54
+ }
55
+ }
56
+
57
+ if ( isNaN ( arg ) || ( pos !== undefined && isNaN ( pos ) ) ) {
58
+ throw Error ( "Invalid arguments" ) ;
59
+ }
41
60
42
- short = ( short === true ) ;
43
- pos = short ? 1 : ( pos === undefined ? 2 : parseInt ( pos , base ) ) ;
44
- num = Number ( arg ) ;
45
- neg = ( num < 0 ) ;
61
+ short = ( short === true ) ;
62
+ bits = ( bits === true ) ;
63
+ pos = short ? 1 : ( pos === undefined ? 2 : parseInt ( pos , base ) ) ;
64
+ num = Number ( arg ) ;
65
+ neg = ( num < 0 ) ;
46
66
47
67
// Flipping a negative number to determine the size
48
- if ( neg ) num = - num ;
68
+ if ( neg ) {
69
+ num = - num ;
70
+ }
49
71
50
72
// Zero is now a special case because bytes divide by 1
51
- if ( num === 0 ) {
52
- if ( short ) pos = 0 ;
53
- result = Number ( 0 ) . toFixed ( pos ) + "B" ;
73
+ if ( num === 0 ) {
74
+ if ( short ) {
75
+ pos = 0 ;
76
+ }
77
+
78
+ result = Number ( 0 ) . toFixed ( pos ) + "B" ;
54
79
}
55
80
else {
56
- while ( i -- ) {
81
+ if ( bits ) {
82
+ sizes = options . all . increments ;
83
+ i = options . all . nth ;
84
+ }
85
+ else {
86
+ sizes = options . bitless . increments ;
87
+ i = options . bitless . nth ;
88
+ }
89
+
90
+ while ( i -- ) {
57
91
size = sizes [ i ] [ 1 ] ;
58
92
suffix = sizes [ i ] [ 0 ] ;
59
- if ( num >= size ) {
60
- result = ( num / size ) . toFixed ( pos ) ;
61
- if ( short ) {
62
- if ( bit . test ( suffix ) ) suffix = suffix . toLowerCase ( ) ;
63
- suffix = suffix . charAt ( 0 ) ;
64
- z = regex . exec ( result ) ;
65
- if ( z !== null && z [ 1 ] !== undefined && zero . test ( z [ 1 ] ) ) result = parseInt ( result , base ) ;
93
+
94
+ if ( num >= size ) {
95
+ result = ( num / size ) . toFixed ( pos ) ;
96
+
97
+ if ( short ) {
98
+ if ( bits && bit . test ( suffix ) ) {
99
+ suffix = suffix . toLowerCase ( ) ;
100
+ }
101
+
102
+ suffix = suffix . charAt ( 0 ) ;
103
+ z = right . exec ( result ) ;
104
+
105
+ if ( z !== null && z [ 1 ] !== undefined && zero . test ( z [ 1 ] ) ) {
106
+ result = parseInt ( result , base ) ;
107
+ }
66
108
}
109
+
67
110
result += suffix ;
68
111
break ;
69
112
}
70
113
}
71
114
}
72
115
73
- return ( neg ? "-" : "" ) + result ;
116
+ // Decorating a 'diff'
117
+ if ( neg ) {
118
+ result = "-" + result ;
119
+ }
120
+
121
+ return result ;
74
122
} ;
75
123
76
- if ( typeof exports !== "undefined" ) module . exports = filesize ;
77
- else if ( typeof define === "function" ) define ( function ( ) { return filesize ; } ) ;
78
- else global . filesize = filesize ;
79
- } ) ( this ) ;
124
+ // CommonJS, AMD, script tag
125
+ if ( typeof exports !== "undefined" ) {
126
+ module . exports = filesize ;
127
+ }
128
+ else if ( typeof define === "function" ) {
129
+ define ( function ( ) {
130
+ return filesize ;
131
+ } ) ;
132
+ }
133
+ else {
134
+ global . filesize = filesize ;
135
+ }
136
+ } ) ( this ) ;
0 commit comments