Skip to content

Commit

Permalink
Add parameter validation, fixed byte output, updated docblock
Browse files Browse the repository at this point in the history
  • Loading branch information
avoidwork committed Mar 16, 2012
1 parent 21e2aa7 commit 329e874
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 11 deletions.
20 changes: 11 additions & 9 deletions debug/filesize.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,22 @@
/**
* filesize.js
*
* Transforms a file size Number into a readable String
*
* @author Jason Mulligan <jason.mulligan@avoidwork.com>
* @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,
Expand All @@ -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;
}
}
Expand Down
11 changes: 9 additions & 2 deletions production/filesize.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,14 @@
/**
* filesize.js
*
* Transforms a file size Number into a readable String
*
* @author Jason Mulligan <jason.mulligan@avoidwork.com>
* @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)
(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)

0 comments on commit 329e874

Please sign in to comment.