Skip to content

Commit

Permalink
Adding support for zero as a parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
avoidwork committed Feb 5, 2013
1 parent e5965f0 commit 5075979
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 7 deletions.
10 changes: 8 additions & 2 deletions lib/filesize.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* @license BSD-3 <https://raw.github.com/avoidwork/filesize.js/master/LICENSE>
* @link http://filesizejs.com
* @module filesize
* @version 1.7.7
* @version 1.7.8
*/

(function (global) {
Expand Down Expand Up @@ -53,7 +53,7 @@
result = (num / size).toFixed(pos);
if (short) {
if (bit.test(suffix)) suffix = suffix.toLowerCase();
suffix = suffix.slice(0, 1);
suffix = suffix.charAt(0);
z = regex.exec(result);
if (z !== null && z[1] !== undefined && zero.test(z[1])) result = parseInt(result, base);
}
Expand All @@ -62,6 +62,12 @@
}
}

// Zero
if (result === "") {
if (short) pos = 0;
result = Number(0).toFixed(pos) + suffix;
}

This comment has been minimized.

Copy link
@avoidwork

avoidwork Feb 5, 2013

Author Owner

This is needed because 1.7.7 introduced dividing bytes / 1, so num is smaller than size now, and requires this special case.

return (neg ? "-" : "") + result;
};

Expand Down
4 changes: 2 additions & 2 deletions lib/filesize.min.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@
* @license BSD-3 <https://raw.github.com/avoidwork/filesize.js/master/LICENSE>
* @link http://filesizejs.com
* @module filesize
* @version 1.7.7
* @version 1.7.8
*/
(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);
(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.charAt(0),p=u.exec(a),p!==null&&p[1]!==undefined&&d.test(p[1])&&(a=parseInt(a,t))),a+=h;break}}return a===""&&(f&&(o=0),a=Number(0).toFixed(o)+h),(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);
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "filesize",
"description": "JavaScript library to generate a human readable String describing the file size",
"version": "1.7.7",
"version": "1.7.8",
"homepage": "http://filesizejs.com",
"author": {
"name": "Jason Mulligan",
Expand Down
8 changes: 7 additions & 1 deletion src/filesize.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
result = (num / size).toFixed(pos);
if (short) {
if (bit.test(suffix)) suffix = suffix.toLowerCase();
suffix = suffix.slice(0, 1);
suffix = suffix.charAt(0);
z = regex.exec(result);
if (z !== null && z[1] !== undefined && zero.test(z[1])) result = parseInt(result, base);
}
Expand All @@ -51,6 +51,12 @@
}
}

// Zero
if (result === "") {
if (short) pos = 0;
result = Number(0).toFixed(pos) + suffix;
}

return (neg ? "-" : "") + result;
};

Expand Down
6 changes: 5 additions & 1 deletion test/filesize_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ exports["filesize"] = {
this.Kb = 500;
this.neg = -1024;
this.byte = 1;
this.zero = 0;
done();
},
valid: function (test) {
test.expect(13);
test.expect(16);
test.equal(filesize(this.Kb), "3.91Kb", "Should match");
test.equal(filesize(this.Kb,true), "3.9k", "Should match");
test.equal(filesize(this.num), "1.00KB", "Should match");
Expand All @@ -25,6 +26,9 @@ exports["filesize"] = {
test.equal(filesize(this.byte), "1.00B", "Should match");
test.equal(filesize(this.byte, 1), "1.0B", "Should match");
test.equal(filesize(this.byte, true), "1B", "Should match");
test.equal(filesize(this.zero), "0.00B", "Should match");
test.equal(filesize(this.zero, 1), "0.0B", "Should match");
test.equal(filesize(this.zero, true), "0B", "Should match");
this.byte = 1;
test.done();
},
Expand Down

0 comments on commit 5075979

Please sign in to comment.