diff --git a/grunt.js b/grunt.js index 741d91e..9b8bfc9 100644 --- a/grunt.js +++ b/grunt.js @@ -19,7 +19,7 @@ module.exports = function(grunt) { "", "src/filesize.js" ], - dest : "dist/filesize.js" + dest : "lib/filesize.js" } }, test : { @@ -29,7 +29,7 @@ module.exports = function(grunt) { files : ["grunt.js"] }, min : { - "dist/filesize.min.js" : ["", "dist/filesize.js"] + "lib/filesize.min.js" : ["", "lib/filesize.js"] }, watch : { files : "", diff --git a/dist/filesize.js b/lib/dist/filesize.js similarity index 100% rename from dist/filesize.js rename to lib/dist/filesize.js diff --git a/dist/filesize.min.js b/lib/dist/filesize.min.js similarity index 100% rename from dist/filesize.min.js rename to lib/dist/filesize.min.js diff --git a/lib/filesize.js b/lib/filesize.js new file mode 100644 index 0000000..3ecd529 --- /dev/null +++ b/lib/filesize.js @@ -0,0 +1,70 @@ +/** + * filesize + * + * @author Jason Mulligan + * @copyright Jason Mulligan 2012 + * @license BSD-3 + * @link https://github.com/avoidwork/filesize.js + * @module filesize + * @version 1.6.6 + */ + +(function (global) { + "use strict"; + + /** + * filesize + * + * @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 + */ + var filesize = function (arg) { + var pos, short, num, sizes, size, result, regex, suffix, i, z; + + if (typeof arguments[2] !== "undefined") { + pos = arguments[1]; + short = arguments[2]; + } + else typeof arguments[1] === "boolean" ? short = arguments[1] : pos = arguments[1]; + + 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 = ""; + regex = /\.(.*)/; + + while (i--) { + size = sizes[i][1]; + suffix = sizes[i][0]; + if (num >= size) { + result = (suffix === "B" ? num : (num / size)).toFixed(pos); + if (short) { + suffix = suffix.slice(0, 1); + z = regex.exec(result); + if (z !== null && typeof z[1] !== "undefined" && z[1] === "0") result = parseInt(result); + } + result += suffix; + break; + } + } + + return result; + }; + + switch (true) { + case typeof exports !== "undefined": + module.exports = filesize; + break; + case typeof define === "function": + define(function () { return filesize; }); + break; + default: + global.filesize = filesize; + } +})(this); \ No newline at end of file diff --git a/lib/filesize.min.js b/lib/filesize.min.js new file mode 100644 index 0000000..3f1fe91 --- /dev/null +++ b/lib/filesize.min.js @@ -0,0 +1,11 @@ +/** + * filesize + * + * @author Jason Mulligan + * @copyright Jason Mulligan 2012 + * @license BSD-3 + * @link https://github.com/avoidwork/filesize.js + * @module filesize + * @version 1.6.6 + */ +(function(a){"use strict";var b=function(a){var b,c,d,e,f,g,h,i,j,k;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]],j=e.length,g="",h=/\.(.*)/;while(j--){f=e[j][1],i=e[j][0];if(d>=f){g=(i==="B"?d:d/f).toFixed(b),c&&(i=i.slice(0,1),k=h.exec(g),k!==null&&typeof k[1]!="undefined"&&k[1]==="0"&&(g=parseInt(g))),g+=i;break}}return g};switch(!0){case typeof exports!="undefined":module.exports=b;break;case typeof define=="function":define(function(){return b});break;default:a.filesize=b}})(this); \ No newline at end of file diff --git a/package.json b/package.json index e71d96d..e98d7f2 100644 --- a/package.json +++ b/package.json @@ -20,7 +20,7 @@ "url": "http://opensource.org/licenses/BSD-3-Clause" } ], - "main": "dist/filesize", + "main": "lib/filesize", "engines": { "node": ">= 0.6.0" }, diff --git a/test/filesize_test.js b/test/filesize_test.js index 857d677..4e62030 100644 --- a/test/filesize_test.js +++ b/test/filesize_test.js @@ -1,4 +1,4 @@ -var filesize = require("../dist/filesize.js"); +var filesize = require("../lib/filesize.js"); exports["filesize"] = { setUp: function (done) {