-
-
Notifications
You must be signed in to change notification settings - Fork 97
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added an optional third boolean parameter to emulate "ls -lh" output,…
… which overrides "pos" parameter
- Loading branch information
Showing
2 changed files
with
31 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,25 +32,33 @@ | |
* | ||
* @author Jason Mulligan <[email protected]> | ||
* @module filesize | ||
* @version 1.4 | ||
* @version 1.5 | ||
* | ||
* @param {Mixed} arg String, Int or Float to transform | ||
* @param {Number} pos Position to round to | ||
* @param {Mixed} arg String, Int or Float to transform | ||
* @param {Number} pos [Optional] Position to round to, defaults to 2 if short is ommitted | ||
* @param {Boolean} short [Optional] Shorthand output, similar to "ls -lh", overrides pos to 1 | ||
* @return {String} Readable file size String | ||
*/ | ||
(function (global) { | ||
"use strict"; | ||
|
||
var filesize = function (arg, pos) { | ||
if (isNaN(arg) || (typeof pos !== "undefined" && isNaN(pos))) throw Error("Invalid arguments"); | ||
var filesize = function (arg) { | ||
var pos, short, num, sizes, size, result, suffix, i, n, x, z; | ||
|
||
var num = String(arg).indexOf(".") > -1 ? parseFloat(arg) : parseInt(arg), | ||
sizes = [{"B": 0}, {"KB": 1024}, {"MB": 1048576}, {"GB": 1073741824}, {"TB": 1099511627776}], | ||
i = sizes.length, | ||
result = "", | ||
size, suffix, n, x; | ||
if (typeof arguments[2] !== "undefined") { | ||
pos = arguments[1]; | ||
short = arguments[2]; | ||
} | ||
else typeof arguments[1] === "boolean" ? short = arguments[1] : pos = arguments[1]; | ||
|
||
pos = typeof pos === "undefined" ? 2 : parseInt(pos); | ||
if (isNaN(arg) || (typeof pos !== "undefined" && isNaN(pos))) throw Error("Invalid arguments"); | ||
|
||
short = (short === true); | ||
pos = short ? 1 : (typeof pos === "undefined" ? 2 : parseInt(pos)); | ||
num = String(arg).indexOf(".") > -1 ? parseFloat(arg) : parseInt(arg); | ||
sizes = [{"B": 0}, {"KB": 1024}, {"MB": 1048576}, {"GB": 1073741824}, {"TB": 1099511627776}]; | ||
i = sizes.length; | ||
result = ""; | ||
|
||
while (i--) { | ||
x = sizes[i]; | ||
|
@@ -62,7 +70,13 @@ | |
} | ||
} | ||
if (num >= size) { | ||
result = (suffix === "B" ? num : (num / size)).toFixed(pos) + suffix; | ||
result = (suffix === "B" ? num : (num / size)).toFixed(pos); | ||
if (short) { | ||
suffix = suffix.slice(0, 1); | ||
z = /\.(.*)/.exec(result); | ||
if (z !== null && typeof z[1] !== "undefined" && z[1] === "0") result = parseInt(result); | ||
} | ||
result += suffix; | ||
break; | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,10 +32,11 @@ | |
* | ||
* @author Jason Mulligan <[email protected]> | ||
* @module filesize | ||
* @version 1.4 | ||
* @version 1.5 | ||
* | ||
* @param {Mixed} arg String, Int or Float to transform | ||
* @param {Number} pos Position to round to | ||
* @param {Mixed} arg String, Int or Float to transform | ||
* @param {Number} pos [Optional] Position to round to, defaults to 2 if short is ommitted | ||
* @param {Boolean} short [Optional] Shorthand output, similar to "ls -lh", overrides pos to 1 | ||
* @return {String} Readable file size String | ||
*/ | ||
(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) | ||
(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) |