Skip to content

Commit 7a3aa08

Browse files
committed
Added a third optional boolean argument to disable bit sizes, fixes #25
Added a line return
1 parent 7648fee commit 7a3aa08

File tree

5 files changed

+221
-89
lines changed

5 files changed

+221
-89
lines changed

lib/filesize.js

+91-34
Original file line numberDiff line numberDiff line change
@@ -6,74 +6,131 @@
66
* @license BSD-3 <https://raw.github.com/avoidwork/filesize.js/master/LICENSE>
77
* @link http://filesizejs.com
88
* @module filesize
9-
* @version 1.8.0
9+
* @version 1.9.0
1010
*/
11-
(function (global) {
11+
( function ( global ) {
1212
"use strict";
1313

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+
};
2028

2129
/**
2230
* filesize
2331
*
2432
* @param {Mixed} arg String, Int or Float to transform
2533
* @param {Number} pos [Optional] Position to round to, defaults to 2 if short is ommitted
2634
* @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
2736
* @return {String} Readable file size String
2837
*/
2938
function filesize (arg) {
3039
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;
3342

34-
if (arguments[2] !== undefined) {
43+
// Determining arguments
44+
if (arguments[3] !== undefined) {
3545
pos = arguments[1];
3646
short = arguments[2];
47+
bits = arguments[3];
3748
}
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];
3951

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+
}
4160

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 );
4666

4767
// Flipping a negative number to determine the size
48-
if (neg) num = -num;
68+
if ( neg ) {
69+
num = -num;
70+
}
4971

5072
// 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";
5479
}
5580
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-- ) {
5791
size = sizes[i][1];
5892
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+
}
66108
}
109+
67110
result += suffix;
68111
break;
69112
}
70113
}
71114
}
72115

73-
return (neg ? "-" : "") + result;
116+
// Decorating a 'diff'
117+
if ( neg ) {
118+
result = "-" + result;
119+
}
120+
121+
return result;
74122
};
75123

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

lib/filesize.min.js

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

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "filesize",
33
"description": "JavaScript library to generate a human readable String describing the file size",
4-
"version": "1.8.0",
4+
"version": "1.9.0",
55
"homepage": "http://filesizejs.com",
66
"author": {
77
"name": "Jason Mulligan",
@@ -22,7 +22,7 @@
2222
],
2323
"main": "lib/filesize",
2424
"engines": {
25-
"node": ">= 0.8.0"
25+
"node": ">= 0.4.0"
2626
},
2727
"scripts": {
2828
"test": "grunt test"

src/filesize.js

+90-33
Original file line numberDiff line numberDiff line change
@@ -1,69 +1,126 @@
1-
(function (global) {
1+
( function ( global ) {
22
"use strict";
33

4-
var base = 10,
5-
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]],
6-
nth = sizes.length,
7-
regex = /\.(.*)/,
8-
bit = /b$/,
9-
zero = /^0$/;
4+
var base = 10,
5+
right = /\.(.*)/,
6+
bit = /b$/,
7+
zero = /^0$/,
8+
options = {
9+
all : {
10+
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]],
11+
nth : 11
12+
},
13+
bitless : {
14+
increments : [["B", 1], ["KB", 1024], ["MB", 1.049e+6], ["GB", 1.074e+9], ["TB", 1.1e+12], ["PB", 1.126e+15]],
15+
nth : 6
16+
}
17+
};
1018

1119
/**
1220
* filesize
1321
*
1422
* @param {Mixed} arg String, Int or Float to transform
1523
* @param {Number} pos [Optional] Position to round to, defaults to 2 if short is ommitted
1624
* @param {Boolean} short [Optional] Shorthand output, similar to "ls -lh", overrides pos to 1
25+
* @param {Boolean} bits [Optional] Determines if `bit` sizes are used for result calculation, default is true
1726
* @return {String} Readable file size String
1827
*/
1928
function filesize (arg) {
2029
var result = "",
21-
i = nth,
22-
neg, num, pos, short, size, suffix, z;
30+
bits = true,
31+
i, neg, num, pos, short, size, sizes, suffix, z;
2332

24-
if (arguments[2] !== undefined) {
33+
// Determining arguments
34+
if (arguments[3] !== undefined) {
2535
pos = arguments[1];
2636
short = arguments[2];
37+
bits = arguments[3];
2738
}
28-
else typeof arguments[1] === "boolean" ? short = arguments[1] : pos = arguments[1];
39+
else {
40+
typeof arguments[1] === "boolean" ? short = arguments[1] : pos = arguments[1];
2941

30-
if (isNaN(arg) || (pos !== undefined && isNaN(pos))) throw Error("Invalid arguments");
42+
if ( typeof arguments[2] === "boolean" ) {
43+
bits = arguments[2];
44+
}
45+
}
46+
47+
if ( isNaN( arg ) || ( pos !== undefined && isNaN( pos ) ) ) {
48+
throw Error("Invalid arguments");
49+
}
3150

32-
short = (short === true);
33-
pos = short ? 1 : (pos === undefined ? 2 : parseInt(pos, base));
34-
num = Number(arg);
35-
neg = (num < 0);
51+
short = ( short === true );
52+
bits = ( bits === true );
53+
pos = short ? 1 : ( pos === undefined ? 2 : parseInt( pos, base ) );
54+
num = Number( arg );
55+
neg = ( num < 0 );
3656

3757
// Flipping a negative number to determine the size
38-
if (neg) num = -num;
58+
if ( neg ) {
59+
num = -num;
60+
}
3961

4062
// Zero is now a special case because bytes divide by 1
41-
if (num === 0) {
42-
if (short) pos = 0;
43-
result = Number(0).toFixed(pos) + "B";
63+
if ( num === 0 ) {
64+
if ( short ) {
65+
pos = 0;
66+
}
67+
68+
result = Number( 0 ).toFixed( pos ) + "B";
4469
}
4570
else {
46-
while (i--) {
71+
if ( bits ) {
72+
sizes = options.all.increments;
73+
i = options.all.nth;
74+
}
75+
else {
76+
sizes = options.bitless.increments;
77+
i = options.bitless.nth;
78+
}
79+
80+
while ( i-- ) {
4781
size = sizes[i][1];
4882
suffix = sizes[i][0];
49-
if (num >= size) {
50-
result = (num / size).toFixed(pos);
51-
if (short) {
52-
if (bit.test(suffix)) suffix = suffix.toLowerCase();
53-
suffix = suffix.charAt(0);
54-
z = regex.exec(result);
55-
if (z !== null && z[1] !== undefined && zero.test(z[1])) result = parseInt(result, base);
83+
84+
if ( num >= size ) {
85+
result = ( num / size ).toFixed( pos );
86+
87+
if ( short ) {
88+
if ( bits && bit.test( suffix ) ) {
89+
suffix = suffix.toLowerCase();
90+
}
91+
92+
suffix = suffix.charAt( 0 );
93+
z = right.exec( result );
94+
95+
if ( z !== null && z[1] !== undefined && zero.test( z[1] ) ) {
96+
result = parseInt( result, base );
97+
}
5698
}
99+
57100
result += suffix;
58101
break;
59102
}
60103
}
61104
}
62105

63-
return (neg ? "-" : "") + result;
106+
// Decorating a 'diff'
107+
if ( neg ) {
108+
result = "-" + result;
109+
}
110+
111+
return result;
64112
};
65113

66-
if (typeof exports !== "undefined") module.exports = filesize;
67-
else if (typeof define === "function") define(function () { return filesize; });
68-
else global.filesize = filesize;
69-
})(this);
114+
// CommonJS, AMD, script tag
115+
if ( typeof exports !== "undefined" ) {
116+
module.exports = filesize;
117+
}
118+
else if ( typeof define === "function" ) {
119+
define( function () {
120+
return filesize;
121+
});
122+
}
123+
else {
124+
global.filesize = filesize;
125+
}
126+
})( this );

0 commit comments

Comments
 (0)