Skip to content

Commit 33b074c

Browse files
committed
Added an optional third boolean parameter to emulate "ls -lh" output, which overrides "pos" parameter
1 parent 329e874 commit 33b074c

File tree

2 files changed

+31
-16
lines changed

2 files changed

+31
-16
lines changed

debug/filesize.js

+26-12
Original file line numberDiff line numberDiff line change
@@ -32,25 +32,33 @@
3232
*
3333
* @author Jason Mulligan <[email protected]>
3434
* @module filesize
35-
* @version 1.4
35+
* @version 1.5
3636
*
37-
* @param {Mixed} arg String, Int or Float to transform
38-
* @param {Number} pos Position to round to
37+
* @param {Mixed} arg String, Int or Float to transform
38+
* @param {Number} pos [Optional] Position to round to, defaults to 2 if short is ommitted
39+
* @param {Boolean} short [Optional] Shorthand output, similar to "ls -lh", overrides pos to 1
3940
* @return {String} Readable file size String
4041
*/
4142
(function (global) {
4243
"use strict";
4344

44-
var filesize = function (arg, pos) {
45-
if (isNaN(arg) || (typeof pos !== "undefined" && isNaN(pos))) throw Error("Invalid arguments");
45+
var filesize = function (arg) {
46+
var pos, short, num, sizes, size, result, suffix, i, n, x, z;
4647

47-
var num = String(arg).indexOf(".") > -1 ? parseFloat(arg) : parseInt(arg),
48-
sizes = [{"B": 0}, {"KB": 1024}, {"MB": 1048576}, {"GB": 1073741824}, {"TB": 1099511627776}],
49-
i = sizes.length,
50-
result = "",
51-
size, suffix, n, x;
48+
if (typeof arguments[2] !== "undefined") {
49+
pos = arguments[1];
50+
short = arguments[2];
51+
}
52+
else typeof arguments[1] === "boolean" ? short = arguments[1] : pos = arguments[1];
5253

53-
pos = typeof pos === "undefined" ? 2 : parseInt(pos);
54+
if (isNaN(arg) || (typeof pos !== "undefined" && isNaN(pos))) throw Error("Invalid arguments");
55+
56+
short = (short === true);
57+
pos = short ? 1 : (typeof pos === "undefined" ? 2 : parseInt(pos));
58+
num = String(arg).indexOf(".") > -1 ? parseFloat(arg) : parseInt(arg);
59+
sizes = [{"B": 0}, {"KB": 1024}, {"MB": 1048576}, {"GB": 1073741824}, {"TB": 1099511627776}];
60+
i = sizes.length;
61+
result = "";
5462

5563
while (i--) {
5664
x = sizes[i];
@@ -62,7 +70,13 @@
6270
}
6371
}
6472
if (num >= size) {
65-
result = (suffix === "B" ? num : (num / size)).toFixed(pos) + suffix;
73+
result = (suffix === "B" ? num : (num / size)).toFixed(pos);
74+
if (short) {
75+
suffix = suffix.slice(0, 1);
76+
z = /\.(.*)/.exec(result);
77+
if (z !== null && typeof z[1] !== "undefined" && z[1] === "0") result = parseInt(result);
78+
}
79+
result += suffix;
6680
break;
6781
}
6882
}

production/filesize.js

+5-4
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,11 @@
3232
*
3333
* @author Jason Mulligan <[email protected]>
3434
* @module filesize
35-
* @version 1.4
35+
* @version 1.5
3636
*
37-
* @param {Mixed} arg String, Int or Float to transform
38-
* @param {Number} pos Position to round to
37+
* @param {Mixed} arg String, Int or Float to transform
38+
* @param {Number} pos [Optional] Position to round to, defaults to 2 if short is ommitted
39+
* @param {Boolean} short [Optional] Shorthand output, similar to "ls -lh", overrides pos to 1
3940
* @return {String} Readable file size String
4041
*/
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)
42+
(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};typeof define=="function"?define("filesize",function(){return b}):a.filesize=b})(this)

0 commit comments

Comments
 (0)