Skip to content

Commit

Permalink
Optimizations; kudos to @marktucker for looking over the lib
Browse files Browse the repository at this point in the history
  • Loading branch information
avoidwork committed Feb 5, 2013
1 parent 785ae92 commit e5965f0
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 25 deletions.
10 changes: 4 additions & 6 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.6
* @version 1.7.7
*/

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

if (arguments[2] !== undefined) {
pos = arguments[1];
Expand All @@ -36,12 +36,11 @@
pos = short ? 1 : (pos === undefined ? 2 : parseInt(pos, base));
num = Number(arg);
neg = (num < 0);
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"]];
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]];
i = sizes.length;
result = "";
regex = /\.(.*)/;
bit = /b$/;
byte = /^B$/;
zero = /^0$/;

// Flipping a negative number to determine the size
Expand All @@ -50,9 +49,8 @@
while (i--) {
size = sizes[i][1];
suffix = sizes[i][0];
if (i > 3) size = Number(size);
if (num >= size) {
result = (byte.test(suffix) ? num : (num / size)).toFixed(pos);
result = (num / size).toFixed(pos);
if (short) {
if (bit.test(suffix)) suffix = suffix.toLowerCase();
suffix = suffix.slice(0, 1);
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.6
* @version 1.7.7
*/
(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);
(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);
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.6",
"version": "1.7.7",
"homepage": "http://filesizejs.com",
"author": {
"name": "Jason Mulligan",
Expand Down
8 changes: 3 additions & 5 deletions src/filesize.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
*/
var filesize = function (arg) {
var base = 10,
bit, byte, i, neg, num, pos, regex, result, short, size, sizes, suffix, z, zero;
bit, i, neg, num, pos, regex, result, short, size, sizes, suffix, z, zero;

if (arguments[2] !== undefined) {
pos = arguments[1];
Expand All @@ -25,12 +25,11 @@
pos = short ? 1 : (pos === undefined ? 2 : parseInt(pos, base));
num = Number(arg);
neg = (num < 0);
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"]];
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]];
i = sizes.length;
result = "";
regex = /\.(.*)/;
bit = /b$/;
byte = /^B$/;
zero = /^0$/;

// Flipping a negative number to determine the size
Expand All @@ -39,9 +38,8 @@
while (i--) {
size = sizes[i][1];
suffix = sizes[i][0];
if (i > 3) size = Number(size);
if (num >= size) {
result = (byte.test(suffix) ? num : (num / size)).toFixed(pos);
result = (num / size).toFixed(pos);
if (short) {
if (bit.test(suffix)) suffix = suffix.toLowerCase();
suffix = suffix.slice(0, 1);
Expand Down
27 changes: 16 additions & 11 deletions test/filesize_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,25 @@ exports["filesize"] = {
this.invld = "abc";
this.Kb = 500;
this.neg = -1024;
this.byte = 1;
done();
},
valid: function (test) {
test.expect(10);
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");
test.equal(filesize(this.str), "1.00KB", "Should match");
test.equal(filesize(this.num, 1), "1.0KB", "Should match");
test.equal(filesize(this.str, 1), "1.0KB", "Should match");
test.equal(filesize(this.num, true), "1K", "Should match");
test.equal(filesize(this.str, true), "1K", "Should match");
test.equal(filesize(this.neg), "-1.00KB", "Should match");
test.equal(filesize(this.neg, true), "-1K", "Should match");
test.expect(13);
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");
test.equal(filesize(this.str), "1.00KB", "Should match");
test.equal(filesize(this.num, 1), "1.0KB", "Should match");
test.equal(filesize(this.str, 1), "1.0KB", "Should match");
test.equal(filesize(this.num, true), "1K", "Should match");
test.equal(filesize(this.str, true), "1K", "Should match");
test.equal(filesize(this.neg), "-1.00KB", "Should match");
test.equal(filesize(this.neg, true), "-1K", "Should match");
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");
this.byte = 1;
test.done();
},
invalid: function (test) {
Expand Down

0 comments on commit e5965f0

Please sign in to comment.