Skip to content

Commit 329e874

Browse files
committed
Add parameter validation, fixed byte output, updated docblock
1 parent 21e2aa7 commit 329e874

File tree

2 files changed

+20
-11
lines changed

2 files changed

+20
-11
lines changed

debug/filesize.js

+11-9
Original file line numberDiff line numberDiff line change
@@ -28,20 +28,22 @@
2828
/**
2929
* filesize.js
3030
*
31+
* Transforms a file size Number into a readable String
32+
*
3133
* @author Jason Mulligan <[email protected]>
32-
* @version 1.3
34+
* @module filesize
35+
* @version 1.4
36+
*
37+
* @param {Mixed} arg String, Int or Float to transform
38+
* @param {Number} pos Position to round to
39+
* @return {String} Readable file size String
3340
*/
3441
(function (global) {
3542
"use strict";
3643

37-
/**
38-
* Transforms a file size into a readable String
39-
*
40-
* @param {Mixed} arg String, Int or Float to transform
41-
* @param {Number} pos Position to round to
42-
* @return {String} Readable file size String
43-
*/
4444
var filesize = function (arg, pos) {
45+
if (isNaN(arg) || (typeof pos !== "undefined" && isNaN(pos))) throw Error("Invalid arguments");
46+
4547
var num = String(arg).indexOf(".") > -1 ? parseFloat(arg) : parseInt(arg),
4648
sizes = [{"B": 0}, {"KB": 1024}, {"MB": 1048576}, {"GB": 1073741824}, {"TB": 1099511627776}],
4749
i = sizes.length,
@@ -60,7 +62,7 @@
6062
}
6163
}
6264
if (num >= size) {
63-
result = (suffix === "B" ? num : (num / size).toFixed(pos)) + suffix;
65+
result = (suffix === "B" ? num : (num / size)).toFixed(pos) + suffix;
6466
break;
6567
}
6668
}

production/filesize.js

+9-2
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,14 @@
2828
/**
2929
* filesize.js
3030
*
31+
* Transforms a file size Number into a readable String
32+
*
3133
* @author Jason Mulligan <[email protected]>
32-
* @version 1.3
34+
* @module filesize
35+
* @version 1.4
36+
*
37+
* @param {Mixed} arg String, Int or Float to transform
38+
* @param {Number} pos Position to round to
39+
* @return {String} Readable file size String
3340
*/
34-
(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)
41+
(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 commit comments

Comments
 (0)