From bfa1398ffee5a0c697fa6c8c58ecf5a40df28f25 Mon Sep 17 00:00:00 2001 From: Jason Mulligan Date: Fri, 2 Nov 2012 20:27:13 -0400 Subject: [PATCH] Refactored to utilize regex tests instead of string comparisons, minor version bump Fixing order of variables --- lib/filesize.js | 13 ++++++++----- lib/filesize.min.js | 4 ++-- package.json | 2 +- src/filesize.js | 11 +++++++---- 4 files changed, 18 insertions(+), 12 deletions(-) diff --git a/lib/filesize.js b/lib/filesize.js index 4d27824..09407e1 100644 --- a/lib/filesize.js +++ b/lib/filesize.js @@ -6,7 +6,7 @@ * @license BSD-3 * @link https://github.com/avoidwork/filesize.js * @module filesize - * @version 1.7.1 + * @version 1.7.2 */ (function (global) { @@ -22,7 +22,7 @@ */ var filesize = function (arg) { var base = 10, - pos, short, num, sizes, size, result, regex, suffix, i, z; + bit, byte, i, num, pos, regex, result, short, size, sizes, suffix, z, zero; if (typeof arguments[2] !== "undefined") { pos = arguments[1]; @@ -39,18 +39,21 @@ i = sizes.length; result = ""; regex = /\.(.*)/; + bit = /b$/; + byte = /^B$/; + zero = /^0$/; while (i--) { size = sizes[i][1]; suffix = sizes[i][0]; if (i > 3) size = Number(size); if (num >= size) { - result = (suffix === "B" ? num : (num / size)).toFixed(pos); + result = (byte.test(suffix) ? num : (num / size)).toFixed(pos); if (short) { - if (/b$/.test(suffix)) suffix = suffix.toLowerCase(); + if (bit.test(suffix)) suffix = suffix.toLowerCase(); suffix = suffix.slice(0, 1); z = regex.exec(result); - if (z !== null && typeof z[1] !== "undefined" && z[1] === "0") result = parseInt(result, base); + if (z !== null && typeof z[1] !== "undefined" && zero.test(z[1])) result = parseInt(result, base); } result += suffix; break; diff --git a/lib/filesize.min.js b/lib/filesize.min.js index dc1e357..13adf26 100644 --- a/lib/filesize.min.js +++ b/lib/filesize.min.js @@ -6,6 +6,6 @@ * @license BSD-3 * @link https://github.com/avoidwork/filesize.js * @module filesize - * @version 1.7.1 + * @version 1.7.2 */ -(function(e){"use strict";var t=function(e){var t=10,n,r,i,s,o,u,a,f,l,c;typeof arguments[2]!="undefined"?(n=arguments[1],r=arguments[2]):typeof arguments[1]=="boolean"?r=arguments[1]:n=arguments[1];if(isNaN(e)||typeof n!="undefined"&&isNaN(n))throw Error("Invalid arguments");r=r===!0,n=r?1:typeof n=="undefined"?2:parseInt(n,t),i=Number(e),s=[["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"]],l=s.length,u="",a=/\.(.*)/;while(l--){o=s[l][1],f=s[l][0],l>3&&(o=Number(o));if(i>=o){u=(f==="B"?i:i/o).toFixed(n),r&&(/b$/.test(f)&&(f=f.toLowerCase()),f=f.slice(0,1),c=a.exec(u),c!==null&&typeof c[1]!="undefined"&&c[1]==="0"&&(u=parseInt(u,t))),u+=f;break}}return u};switch(!0){case typeof exports!="undefined":module.exports=t;break;case typeof define=="function":define(function(){return t});break;default:e.filesize=t}})(this); \ No newline at end of file +(function(e){"use strict";var t=function(e){var t=10,n,r,i,s,o,u,a,f,l,c,h,p,d;typeof arguments[2]!="undefined"?(o=arguments[1],f=arguments[2]):typeof arguments[1]=="boolean"?f=arguments[1]:o=arguments[1];if(isNaN(e)||typeof o!="undefined"&&isNaN(o))throw Error("Invalid arguments");f=f===!0,o=f?1:typeof o=="undefined"?2:parseInt(o,t),s=Number(e),c=[["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=c.length,a="",u=/\.(.*)/,n=/b$/,r=/^B$/,d=/^0$/;while(i--){l=c[i][1],h=c[i][0],i>3&&(l=Number(l));if(s>=l){a=(r.test(h)?s:s/l).toFixed(o),f&&(n.test(h)&&(h=h.toLowerCase()),h=h.slice(0,1),p=u.exec(a),p!==null&&typeof p[1]!="undefined"&&d.test(p[1])&&(a=parseInt(a,t))),a+=h;break}}return 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); \ No newline at end of file diff --git a/package.json b/package.json index 9332d55..1a578eb 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "filesize", "description": "JavaScript library to generate a human readable String describing the file size", - "version": "1.7.1", + "version": "1.7.2", "homepage": "https://github.com/avoidwork/filesize.js", "author": { "name": "Jason Mulligan", diff --git a/src/filesize.js b/src/filesize.js index ca2c56a..fd48c1a 100644 --- a/src/filesize.js +++ b/src/filesize.js @@ -11,7 +11,7 @@ */ var filesize = function (arg) { var base = 10, - pos, short, num, sizes, size, result, regex, suffix, i, z; + bit, byte, i, num, pos, regex, result, short, size, sizes, suffix, z, zero; if (typeof arguments[2] !== "undefined") { pos = arguments[1]; @@ -28,18 +28,21 @@ i = sizes.length; result = ""; regex = /\.(.*)/; + bit = /b$/; + byte = /^B$/; + zero = /^0$/; while (i--) { size = sizes[i][1]; suffix = sizes[i][0]; if (i > 3) size = Number(size); if (num >= size) { - result = (suffix === "B" ? num : (num / size)).toFixed(pos); + result = (byte.test(suffix) ? num : (num / size)).toFixed(pos); if (short) { - if (/b$/.test(suffix)) suffix = suffix.toLowerCase(); + if (bit.test(suffix)) suffix = suffix.toLowerCase(); suffix = suffix.slice(0, 1); z = regex.exec(result); - if (z !== null && typeof z[1] !== "undefined" && z[1] === "0") result = parseInt(result, base); + if (z !== null && typeof z[1] !== "undefined" && zero.test(z[1])) result = parseInt(result, base); } result += suffix; break;