Skip to content

Commit e5965f0

Browse files
committedFeb 5, 2013
Optimizations; kudos to @marktucker for looking over the lib
1 parent 785ae92 commit e5965f0

File tree

5 files changed

+26
-25
lines changed

5 files changed

+26
-25
lines changed
 

‎lib/filesize.js

+4-6
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* @license BSD-3 <https://raw.github.com/avoidwork/filesize.js/master/LICENSE>
77
* @link http://filesizejs.com
88
* @module filesize
9-
* @version 1.7.6
9+
* @version 1.7.7
1010
*/
1111

1212
(function (global) {
@@ -22,7 +22,7 @@
2222
*/
2323
var filesize = function (arg) {
2424
var base = 10,
25-
bit, byte, i, neg, num, pos, regex, result, short, size, sizes, suffix, z, zero;
25+
bit, i, neg, num, pos, regex, result, short, size, sizes, suffix, z, zero;
2626

2727
if (arguments[2] !== undefined) {
2828
pos = arguments[1];
@@ -36,12 +36,11 @@
3636
pos = short ? 1 : (pos === undefined ? 2 : parseInt(pos, base));
3737
num = Number(arg);
3838
neg = (num < 0);
39-
sizes = [["B", 0], ["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"]];
39+
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]];
4040
i = sizes.length;
4141
result = "";
4242
regex = /\.(.*)/;
4343
bit = /b$/;
44-
byte = /^B$/;
4544
zero = /^0$/;
4645

4746
// Flipping a negative number to determine the size
@@ -50,9 +49,8 @@
5049
while (i--) {
5150
size = sizes[i][1];
5251
suffix = sizes[i][0];
53-
if (i > 3) size = Number(size);
5452
if (num >= size) {
55-
result = (byte.test(suffix) ? num : (num / size)).toFixed(pos);
53+
result = (num / size).toFixed(pos);
5654
if (short) {
5755
if (bit.test(suffix)) suffix = suffix.toLowerCase();
5856
suffix = suffix.slice(0, 1);

‎lib/filesize.min.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@
66
* @license BSD-3 <https://raw.github.com/avoidwork/filesize.js/master/LICENSE>
77
* @link http://filesizejs.com
88
* @module filesize
9-
* @version 1.7.6
9+
* @version 1.7.7
1010
*/
11-
(function(e){"use strict";var t=function(e){var t=10,n,r,i,s,o,u,a,f,l,c,h,p,d,v;arguments[2]!==undefined?(u=arguments[1],l=arguments[2]):typeof arguments[1]=="boolean"?l=arguments[1]:u=arguments[1];if(isNaN(e)||u!==undefined&&isNaN(u))throw Error("Invalid arguments");l=l===!0,u=l?1:u===undefined?2:parseInt(u,t),o=Number(e),s=o<0,h=[["B",0],["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"]],i=h.length,f="",a=/\.(.*)/,n=/b$/,r=/^B$/,v=/^0$/,s&&(o=-o);while(i--){c=h[i][1],p=h[i][0],i>3&&(c=Number(c));if(o>=c){f=(r.test(p)?o:o/c).toFixed(u),l&&(n.test(p)&&(p=p.toLowerCase()),p=p.slice(0,1),d=a.exec(f),d!==null&&d[1]!==undefined&&v.test(d[1])&&(f=parseInt(f,t))),f+=p;break}}return(s?"-":"")+f};switch(!0){case typeof exports!="undefined":module.exports=t;break;case typeof define=="function":define(function(){return t});break;default:e.filesize=t}})(this);
11+
(function(e){"use strict";var t=function(e){var t=10,n,r,i,s,o,u,a,f,l,c,h,p,d;arguments[2]!==undefined?(o=arguments[1],f=arguments[2]):typeof arguments[1]=="boolean"?f=arguments[1]:o=arguments[1];if(isNaN(e)||o!==undefined&&isNaN(o))throw Error("Invalid arguments");f=f===!0,o=f?1:o===undefined?2:parseInt(o,t),s=Number(e),i=s<0,c=[["B",1],["Kb",128],["KB",1024],["Mb",131072],["MB",1049e3],["Gb",1342e5],["GB",1074e6],["Tb",1374e8],["TB",11e11],["Pb",1407e11],["PB",1126e12]],r=c.length,a="",u=/\.(.*)/,n=/b$/,d=/^0$/,i&&(s=-s);while(r--){l=c[r][1],h=c[r][0];if(s>=l){a=(s/l).toFixed(o),f&&(n.test(h)&&(h=h.toLowerCase()),h=h.slice(0,1),p=u.exec(a),p!==null&&p[1]!==undefined&&d.test(p[1])&&(a=parseInt(a,t))),a+=h;break}}return(i?"-":"")+a};switch(!0){case typeof exports!="undefined":module.exports=t;break;case typeof define=="function":define(function(){return t});break;default:e.filesize=t}})(this);

‎package.json

+1-1
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.7.6",
4+
"version": "1.7.7",
55
"homepage": "http://filesizejs.com",
66
"author": {
77
"name": "Jason Mulligan",

‎src/filesize.js

+3-5
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
*/
1212
var filesize = function (arg) {
1313
var base = 10,
14-
bit, byte, i, neg, num, pos, regex, result, short, size, sizes, suffix, z, zero;
14+
bit, i, neg, num, pos, regex, result, short, size, sizes, suffix, z, zero;
1515

1616
if (arguments[2] !== undefined) {
1717
pos = arguments[1];
@@ -25,12 +25,11 @@
2525
pos = short ? 1 : (pos === undefined ? 2 : parseInt(pos, base));
2626
num = Number(arg);
2727
neg = (num < 0);
28-
sizes = [["B", 0], ["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"]];
28+
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]];
2929
i = sizes.length;
3030
result = "";
3131
regex = /\.(.*)/;
3232
bit = /b$/;
33-
byte = /^B$/;
3433
zero = /^0$/;
3534

3635
// Flipping a negative number to determine the size
@@ -39,9 +38,8 @@
3938
while (i--) {
4039
size = sizes[i][1];
4140
suffix = sizes[i][0];
42-
if (i > 3) size = Number(size);
4341
if (num >= size) {
44-
result = (byte.test(suffix) ? num : (num / size)).toFixed(pos);
42+
result = (num / size).toFixed(pos);
4543
if (short) {
4644
if (bit.test(suffix)) suffix = suffix.toLowerCase();
4745
suffix = suffix.slice(0, 1);

‎test/filesize_test.js

+16-11
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,25 @@ exports["filesize"] = {
77
this.invld = "abc";
88
this.Kb = 500;
99
this.neg = -1024;
10+
this.byte = 1;
1011
done();
1112
},
1213
valid: function (test) {
13-
test.expect(10);
14-
test.equal(filesize(this.Kb), "3.91Kb", "Should match");
15-
test.equal(filesize(this.Kb,true), "3.9k", "Should match");
16-
test.equal(filesize(this.num), "1.00KB", "Should match");
17-
test.equal(filesize(this.str), "1.00KB", "Should match");
18-
test.equal(filesize(this.num, 1), "1.0KB", "Should match");
19-
test.equal(filesize(this.str, 1), "1.0KB", "Should match");
20-
test.equal(filesize(this.num, true), "1K", "Should match");
21-
test.equal(filesize(this.str, true), "1K", "Should match");
22-
test.equal(filesize(this.neg), "-1.00KB", "Should match");
23-
test.equal(filesize(this.neg, true), "-1K", "Should match");
14+
test.expect(13);
15+
test.equal(filesize(this.Kb), "3.91Kb", "Should match");
16+
test.equal(filesize(this.Kb,true), "3.9k", "Should match");
17+
test.equal(filesize(this.num), "1.00KB", "Should match");
18+
test.equal(filesize(this.str), "1.00KB", "Should match");
19+
test.equal(filesize(this.num, 1), "1.0KB", "Should match");
20+
test.equal(filesize(this.str, 1), "1.0KB", "Should match");
21+
test.equal(filesize(this.num, true), "1K", "Should match");
22+
test.equal(filesize(this.str, true), "1K", "Should match");
23+
test.equal(filesize(this.neg), "-1.00KB", "Should match");
24+
test.equal(filesize(this.neg, true), "-1K", "Should match");
25+
test.equal(filesize(this.byte), "1.00B", "Should match");
26+
test.equal(filesize(this.byte, 1), "1.0B", "Should match");
27+
test.equal(filesize(this.byte, true), "1B", "Should match");
28+
this.byte = 1;
2429
test.done();
2530
},
2631
invalid: function (test) {

0 commit comments

Comments
 (0)
Please sign in to comment.