From 329e874c8e9293e656f3383fc555c0babf5f3125 Mon Sep 17 00:00:00 2001 From: Jason Mulligan Date: Fri, 16 Mar 2012 08:19:28 -0400 Subject: [PATCH] Add parameter validation, fixed byte output, updated docblock --- debug/filesize.js | 20 +++++++++++--------- production/filesize.js | 11 +++++++++-- 2 files changed, 20 insertions(+), 11 deletions(-) diff --git a/debug/filesize.js b/debug/filesize.js index 5459fc3..7ec0a06 100644 --- a/debug/filesize.js +++ b/debug/filesize.js @@ -28,20 +28,22 @@ /** * filesize.js * + * Transforms a file size Number into a readable String + * * @author Jason Mulligan - * @version 1.3 + * @module filesize + * @version 1.4 + * + * @param {Mixed} arg String, Int or Float to transform + * @param {Number} pos Position to round to + * @return {String} Readable file size String */ (function (global) { "use strict"; - /** - * Transforms a file size into a readable String - * - * @param {Mixed} arg String, Int or Float to transform - * @param {Number} pos Position to round to - * @return {String} Readable file size String - */ var filesize = function (arg, pos) { + if (isNaN(arg) || (typeof pos !== "undefined" && isNaN(pos))) throw Error("Invalid arguments"); + var num = String(arg).indexOf(".") > -1 ? parseFloat(arg) : parseInt(arg), sizes = [{"B": 0}, {"KB": 1024}, {"MB": 1048576}, {"GB": 1073741824}, {"TB": 1099511627776}], i = sizes.length, @@ -60,7 +62,7 @@ } } if (num >= size) { - result = (suffix === "B" ? num : (num / size).toFixed(pos)) + suffix; + result = (suffix === "B" ? num : (num / size)).toFixed(pos) + suffix; break; } } diff --git a/production/filesize.js b/production/filesize.js index b58879c..b9c9135 100644 --- a/production/filesize.js +++ b/production/filesize.js @@ -28,7 +28,14 @@ /** * filesize.js * + * Transforms a file size Number into a readable String + * * @author Jason Mulligan - * @version 1.3 + * @module filesize + * @version 1.4 + * + * @param {Mixed} arg String, Int or Float to transform + * @param {Number} pos Position to round to + * @return {String} Readable file size String */ -(function(a){"use strict";var b=function(a,b){var c=String(a).indexOf(".")>-1?parseFloat(a):parseInt(a),d=[{B:0},{KB:1024},{MB:1048576},{GB:1073741824},{TB:1099511627776}],e=d.length,f="",g,h,i,j;b=typeof b=="undefined"?2:parseInt(b);while(e--){j=d[e];for(i in j)if(j.hasOwnProperty(i)){g=j[i],h=i;break}if(c>=g){f=(h==="B"?c:(c/g).toFixed(b))+h;break}}return f};typeof define=="function"?define("filesize",function(){return b}):a.filesize=b})(this) \ No newline at end of file +(function(a){"use strict";var b=function(a,b){if(isNaN(a)||typeof b!="undefined"&&isNaN(b))throw Error("Invalid arguments");var c=String(a).indexOf(".")>-1?parseFloat(a):parseInt(a),d=[{B:0},{KB:1024},{MB:1048576},{GB:1073741824},{TB:1099511627776}],e=d.length,f="",g,h,i,j;b=typeof b=="undefined"?2:parseInt(b);while(e--){j=d[e];for(i in j)if(j.hasOwnProperty(i)){g=j[i],h=i;break}if(c>=g){f=(h==="B"?c:c/g).toFixed(b)+h;break}}return f};typeof define=="function"?define("filesize",function(){return b}):a.filesize=b})(this) \ No newline at end of file