Skip to content

Commit

Permalink
Made it faster
Browse files Browse the repository at this point in the history
  • Loading branch information
avoidwork committed Jun 5, 2012
1 parent a2eefc4 commit f2126d0
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 14 deletions.
20 changes: 8 additions & 12 deletions debug/filesize.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
*
* @author Jason Mulligan <jason.mulligan@avoidwork.com>
* @module filesize
* @version 1.6.3
* @version 1.6.4
*
* @param {Mixed} arg String, Int or Float to transform
* @param {Number} pos [Optional] Position to round to, defaults to 2 if short is ommitted
Expand All @@ -43,7 +43,7 @@
"use strict";

var filesize = function (arg) {
var pos, short, num, sizes, size, result, suffix, i, n, x, z;
var pos, short, num, sizes, size, result, regex, suffix, i, n, x, z;

if (typeof arguments[2] !== "undefined") {
pos = arguments[1];
Expand All @@ -56,24 +56,20 @@
short = (short === true);
pos = short ? 1 : (typeof pos === "undefined" ? 2 : parseInt(pos));
num = String(arg).indexOf(".") > -1 ? parseFloat(arg) : parseInt(arg);
sizes = [{"B": 0}, {"KB": 1024}, {"MB": 1048576}, {"GB": 1073741824}, {"TB": 1099511627776}];
sizes = ["B:0", "KB:1024", "MB:1048576", "GB:1073741824", "TB:1099511627776"]
i = sizes.length;
result = "";
regex = /\.(.*)/;

while (i--) {
x = sizes[i];
for (n in x) {
if (x.hasOwnProperty(n)) {
size = x[n];
suffix = n
break;
}
}
x = sizes[i].split(":");
size = parseInt(x[1]);
suffix = x[0];
if (num >= size) {
result = (suffix === "B" ? num : (num / size)).toFixed(pos);
if (short) {
suffix = suffix.slice(0, 1);
z = /\.(.*)/.exec(result);
z = regex.exec(result);
if (z !== null && typeof z[1] !== "undefined" && z[1] === "0") result = parseInt(result);
}
result += suffix;
Expand Down
4 changes: 2 additions & 2 deletions production/filesize.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@
*
* @author Jason Mulligan <jason.mulligan@avoidwork.com>
* @module filesize
* @version 1.6.3
* @version 1.6.4
*
* @param {Mixed} arg String, Int or Float to transform
* @param {Number} pos [Optional] Position to round to, defaults to 2 if short is ommitted
* @param {Boolean} short [Optional] Shorthand output, similar to "ls -lh", overrides pos to 1
* @return {String} Readable file size String
*/
(function(a){"use strict";var b=function(a){var b,c,d,e,f,g,h,i,j,k,l;typeof arguments[2]!="undefined"?(b=arguments[1],c=arguments[2]):typeof arguments[1]=="boolean"?c=arguments[1]:b=arguments[1];if(isNaN(a)||typeof b!="undefined"&&isNaN(b))throw Error("Invalid arguments");c=c===!0,b=c?1:typeof b=="undefined"?2:parseInt(b),d=String(a).indexOf(".")>-1?parseFloat(a):parseInt(a),e=[{B:0},{KB:1024},{MB:1048576},{GB:1073741824},{TB:1099511627776}],i=e.length,g="";while(i--){k=e[i];for(j in k)if(k.hasOwnProperty(j)){f=k[j],h=j;break}if(d>=f){g=(h==="B"?d:d/f).toFixed(b),c&&(h=h.slice(0,1),l=/\.(.*)/.exec(g),l!==null&&typeof l[1]!="undefined"&&l[1]==="0"&&(g=parseInt(g))),g+=h;break}}return g};switch(!0){case typeof exports!="undefined":module.exports=b;break;case typeof define=="function":define("filesize",function(){return b});break;default:a.filesize=b}})(this)
(function(a){"use strict";var b=function(a){var b,c,d,e,f,g,h,i,j,k,l,m;typeof arguments[2]!="undefined"?(b=arguments[1],c=arguments[2]):typeof arguments[1]=="boolean"?c=arguments[1]:b=arguments[1];if(isNaN(a)||typeof b!="undefined"&&isNaN(b))throw Error("Invalid arguments");c=c===!0,b=c?1:typeof b=="undefined"?2:parseInt(b),d=String(a).indexOf(".")>-1?parseFloat(a):parseInt(a),e=["B:0","KB:1024","MB:1048576","GB:1073741824","TB:1099511627776"],j=e.length,g="",h=/\.(.*)/;while(j--){l=e[j].split(":"),f=parseInt(l[1]),i=l[0];if(d>=f){g=(i==="B"?d:d/f).toFixed(b),c&&(i=i.slice(0,1),m=h.exec(g),m!==null&&typeof m[1]!="undefined"&&m[1]==="0"&&(g=parseInt(g))),g+=i;break}}return g};switch(!0){case typeof exports!="undefined":module.exports=b;break;case typeof define=="function":define("filesize",function(){return b});break;default:a.filesize=b}})(this)

0 comments on commit f2126d0

Please sign in to comment.