From 7a3aa089fd3cfd17244146e70699083406550cdb Mon Sep 17 00:00:00 2001 From: Jason Mulligan Date: Sun, 7 Apr 2013 08:05:19 -0400 Subject: [PATCH] Added a `third` optional boolean argument to disable `bit` sizes, fixes #25 Added a line return --- lib/filesize.js | 125 ++++++++++++++++++++++++++++++------------ lib/filesize.min.js | 4 +- package.json | 4 +- src/filesize.js | 123 ++++++++++++++++++++++++++++++----------- test/filesize_test.js | 54 ++++++++++++------ 5 files changed, 221 insertions(+), 89 deletions(-) diff --git a/lib/filesize.js b/lib/filesize.js index 4b3444b..e292fcf 100644 --- a/lib/filesize.js +++ b/lib/filesize.js @@ -6,17 +6,25 @@ * @license BSD-3 * @link http://filesizejs.com * @module filesize - * @version 1.8.0 + * @version 1.9.0 */ -(function (global) { +( function ( global ) { "use strict"; - var base = 10, - sizes = [["B", 1], ["Kb", 128], ["KB", 1024], ["Mb", 131072], ["MB", 1.049e+6], ["Gb", 1.342e+8], ["GB", 1.074e+9], ["Tb", 1.374e+11], ["TB", 1.1e+12], ["Pb", 1.407e+14], ["PB", 1.126e+15]], - nth = sizes.length, - regex = /\.(.*)/, - bit = /b$/, - zero = /^0$/; + var base = 10, + right = /\.(.*)/, + bit = /b$/, + zero = /^0$/, + options = { + all : { + increments : [["B", 1], ["Kb", 128], ["KB", 1024], ["Mb", 131072], ["MB", 1.049e+6], ["Gb", 1.342e+8], ["GB", 1.074e+9], ["Tb", 1.374e+11], ["TB", 1.1e+12], ["Pb", 1.407e+14], ["PB", 1.126e+15]], + nth : 11 + }, + bitless : { + increments : [["B", 1], ["KB", 1024], ["MB", 1.049e+6], ["GB", 1.074e+9], ["TB", 1.1e+12], ["PB", 1.126e+15]], + nth : 6 + } + }; /** * filesize @@ -24,56 +32,105 @@ * @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 + * @param {Boolean} bits [Optional] Determines if `bit` sizes are used for result calculation, default is true * @return {String} Readable file size String */ function filesize (arg) { var result = "", - i = nth, - neg, num, pos, short, size, suffix, z; + bits = true, + i, neg, num, pos, short, size, sizes, suffix, z; - if (arguments[2] !== undefined) { + // Determining arguments + if (arguments[3] !== undefined) { pos = arguments[1]; short = arguments[2]; + bits = arguments[3]; } - else typeof arguments[1] === "boolean" ? short = arguments[1] : pos = arguments[1]; + else { + typeof arguments[1] === "boolean" ? short = arguments[1] : pos = arguments[1]; - if (isNaN(arg) || (pos !== undefined && isNaN(pos))) throw Error("Invalid arguments"); + if ( typeof arguments[2] === "boolean" ) { + bits = arguments[2]; + } + } + + if ( isNaN( arg ) || ( pos !== undefined && isNaN( pos ) ) ) { + throw Error("Invalid arguments"); + } - short = (short === true); - pos = short ? 1 : (pos === undefined ? 2 : parseInt(pos, base)); - num = Number(arg); - neg = (num < 0); + short = ( short === true ); + bits = ( bits === true ); + pos = short ? 1 : ( pos === undefined ? 2 : parseInt( pos, base ) ); + num = Number( arg ); + neg = ( num < 0 ); // Flipping a negative number to determine the size - if (neg) num = -num; + if ( neg ) { + num = -num; + } // Zero is now a special case because bytes divide by 1 - if (num === 0) { - if (short) pos = 0; - result = Number(0).toFixed(pos) + "B"; + if ( num === 0 ) { + if ( short ) { + pos = 0; + } + + result = Number( 0 ).toFixed( pos ) + "B"; } else { - while (i--) { + if ( bits ) { + sizes = options.all.increments; + i = options.all.nth; + } + else { + sizes = options.bitless.increments; + i = options.bitless.nth; + } + + while ( i-- ) { size = sizes[i][1]; suffix = sizes[i][0]; - if (num >= size) { - result = (num / size).toFixed(pos); - if (short) { - if (bit.test(suffix)) suffix = suffix.toLowerCase(); - suffix = suffix.charAt(0); - z = regex.exec(result); - if (z !== null && z[1] !== undefined && zero.test(z[1])) result = parseInt(result, base); + + if ( num >= size ) { + result = ( num / size ).toFixed( pos ); + + if ( short ) { + if ( bits && bit.test( suffix ) ) { + suffix = suffix.toLowerCase(); + } + + suffix = suffix.charAt( 0 ); + z = right.exec( result ); + + if ( z !== null && z[1] !== undefined && zero.test( z[1] ) ) { + result = parseInt( result, base ); + } } + result += suffix; break; } } } - return (neg ? "-" : "") + result; + // Decorating a 'diff' + if ( neg ) { + result = "-" + result; + } + + return result; }; - if (typeof exports !== "undefined") module.exports = filesize; - else if (typeof define === "function") define(function () { return filesize; }); - else global.filesize = filesize; -})(this); \ No newline at end of file + // CommonJS, AMD, script tag + if ( typeof exports !== "undefined" ) { + module.exports = filesize; + } + else if ( typeof define === "function" ) { + define( function () { + return filesize; + }); + } + else { + global.filesize = filesize; + } +})( this ); diff --git a/lib/filesize.min.js b/lib/filesize.min.js index a2e24b6..736c87f 100644 --- a/lib/filesize.min.js +++ b/lib/filesize.min.js @@ -6,6 +6,6 @@ * @license BSD-3 * @link http://filesizejs.com * @module filesize - * @version 1.8.0 + * @version 1.9.0 */ -(function(e){"use strict";function filesize(e){var u,a,f,d,b,m,g,l="",c=n;if(void 0!==arguments[2]?(f=arguments[1],d=arguments[2]):"boolean"==typeof arguments[1]?d=arguments[1]:f=arguments[1],isNaN(e)||void 0!==f&&isNaN(f))throw Error("Invalid arguments");if(d=d===!0,f=d?1:void 0===f?2:parseInt(f,t),a=Number(e),u=0>a,u&&(a=-a),0===a)d&&(f=0),l=Number(0).toFixed(f)+"B";else for(;c--;)if(b=r[c][1],m=r[c][0],a>=b){l=(a/b).toFixed(f),d&&(o.test(m)&&(m=m.toLowerCase()),m=m.charAt(0),g=i.exec(l),null!==g&&void 0!==g[1]&&s.test(g[1])&&(l=parseInt(l,t))),l+=m;break}return(u?"-":"")+l}var t=10,r=[["B",1],["Kb",128],["KB",1024],["Mb",131072],["MB",1049e3],["Gb",1342e5],["GB",1074e6],["Tb",1374e8],["TB",11e11],["Pb",1407e11],["PB",1126e12]],n=r.length,i=/\.(.*)/,o=/b$/,s=/^0$/;"undefined"!=typeof exports?module.exports=filesize:"function"==typeof define?define(function(){return filesize}):e.filesize=filesize})(this); \ No newline at end of file +(function(e){"use strict";function filesize(e){var o,a,u,l,m,f,b,B,d,c="",g=!0;if(void 0!==arguments[3]?(l=arguments[1],m=arguments[2],g=arguments[3]):("boolean"==typeof arguments[1]?m=arguments[1]:l=arguments[1],"boolean"==typeof arguments[2]&&(g=arguments[2])),isNaN(e)||void 0!==l&&isNaN(l))throw Error("Invalid arguments");if(m=m===!0,g=g===!0,l=m?1:void 0===l?2:parseInt(l,t),u=Number(e),a=0>u,a&&(u=-u),0===u)m&&(l=0),c=Number(0).toFixed(l)+"B";else for(g?(b=i.all.increments,o=i.all.nth):(b=i.bitless.increments,o=i.bitless.nth);o--;)if(f=b[o][1],B=b[o][0],u>=f){c=(u/f).toFixed(l),m&&(g&&r.test(B)&&(B=B.toLowerCase()),B=B.charAt(0),d=n.exec(c),null!==d&&void 0!==d[1]&&s.test(d[1])&&(c=parseInt(c,t))),c+=B;break}return a&&(c="-"+c),c}var t=10,n=/\.(.*)/,r=/b$/,s=/^0$/,i={all:{increments:[["B",1],["Kb",128],["KB",1024],["Mb",131072],["MB",1049e3],["Gb",1342e5],["GB",1074e6],["Tb",1374e8],["TB",11e11],["Pb",1407e11],["PB",1126e12]],nth:11},bitless:{increments:[["B",1],["KB",1024],["MB",1049e3],["GB",1074e6],["TB",11e11],["PB",1126e12]],nth:6}};"undefined"!=typeof exports?module.exports=filesize:"function"==typeof define?define(function(){return filesize}):e.filesize=filesize})(this); \ No newline at end of file diff --git a/package.json b/package.json index 8dc8738..eedce8e 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "filesize", "description": "JavaScript library to generate a human readable String describing the file size", - "version": "1.8.0", + "version": "1.9.0", "homepage": "http://filesizejs.com", "author": { "name": "Jason Mulligan", @@ -22,7 +22,7 @@ ], "main": "lib/filesize", "engines": { - "node": ">= 0.8.0" + "node": ">= 0.4.0" }, "scripts": { "test": "grunt test" diff --git a/src/filesize.js b/src/filesize.js index 753a4e8..ac6d619 100644 --- a/src/filesize.js +++ b/src/filesize.js @@ -1,12 +1,20 @@ -(function (global) { +( function ( global ) { "use strict"; - var base = 10, - sizes = [["B", 1], ["Kb", 128], ["KB", 1024], ["Mb", 131072], ["MB", 1.049e+6], ["Gb", 1.342e+8], ["GB", 1.074e+9], ["Tb", 1.374e+11], ["TB", 1.1e+12], ["Pb", 1.407e+14], ["PB", 1.126e+15]], - nth = sizes.length, - regex = /\.(.*)/, - bit = /b$/, - zero = /^0$/; + var base = 10, + right = /\.(.*)/, + bit = /b$/, + zero = /^0$/, + options = { + all : { + increments : [["B", 1], ["Kb", 128], ["KB", 1024], ["Mb", 131072], ["MB", 1.049e+6], ["Gb", 1.342e+8], ["GB", 1.074e+9], ["Tb", 1.374e+11], ["TB", 1.1e+12], ["Pb", 1.407e+14], ["PB", 1.126e+15]], + nth : 11 + }, + bitless : { + increments : [["B", 1], ["KB", 1024], ["MB", 1.049e+6], ["GB", 1.074e+9], ["TB", 1.1e+12], ["PB", 1.126e+15]], + nth : 6 + } + }; /** * filesize @@ -14,56 +22,105 @@ * @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 + * @param {Boolean} bits [Optional] Determines if `bit` sizes are used for result calculation, default is true * @return {String} Readable file size String */ function filesize (arg) { var result = "", - i = nth, - neg, num, pos, short, size, suffix, z; + bits = true, + i, neg, num, pos, short, size, sizes, suffix, z; - if (arguments[2] !== undefined) { + // Determining arguments + if (arguments[3] !== undefined) { pos = arguments[1]; short = arguments[2]; + bits = arguments[3]; } - else typeof arguments[1] === "boolean" ? short = arguments[1] : pos = arguments[1]; + else { + typeof arguments[1] === "boolean" ? short = arguments[1] : pos = arguments[1]; - if (isNaN(arg) || (pos !== undefined && isNaN(pos))) throw Error("Invalid arguments"); + if ( typeof arguments[2] === "boolean" ) { + bits = arguments[2]; + } + } + + if ( isNaN( arg ) || ( pos !== undefined && isNaN( pos ) ) ) { + throw Error("Invalid arguments"); + } - short = (short === true); - pos = short ? 1 : (pos === undefined ? 2 : parseInt(pos, base)); - num = Number(arg); - neg = (num < 0); + short = ( short === true ); + bits = ( bits === true ); + pos = short ? 1 : ( pos === undefined ? 2 : parseInt( pos, base ) ); + num = Number( arg ); + neg = ( num < 0 ); // Flipping a negative number to determine the size - if (neg) num = -num; + if ( neg ) { + num = -num; + } // Zero is now a special case because bytes divide by 1 - if (num === 0) { - if (short) pos = 0; - result = Number(0).toFixed(pos) + "B"; + if ( num === 0 ) { + if ( short ) { + pos = 0; + } + + result = Number( 0 ).toFixed( pos ) + "B"; } else { - while (i--) { + if ( bits ) { + sizes = options.all.increments; + i = options.all.nth; + } + else { + sizes = options.bitless.increments; + i = options.bitless.nth; + } + + while ( i-- ) { size = sizes[i][1]; suffix = sizes[i][0]; - if (num >= size) { - result = (num / size).toFixed(pos); - if (short) { - if (bit.test(suffix)) suffix = suffix.toLowerCase(); - suffix = suffix.charAt(0); - z = regex.exec(result); - if (z !== null && z[1] !== undefined && zero.test(z[1])) result = parseInt(result, base); + + if ( num >= size ) { + result = ( num / size ).toFixed( pos ); + + if ( short ) { + if ( bits && bit.test( suffix ) ) { + suffix = suffix.toLowerCase(); + } + + suffix = suffix.charAt( 0 ); + z = right.exec( result ); + + if ( z !== null && z[1] !== undefined && zero.test( z[1] ) ) { + result = parseInt( result, base ); + } } + result += suffix; break; } } } - return (neg ? "-" : "") + result; + // Decorating a 'diff' + if ( neg ) { + result = "-" + result; + } + + return result; }; - if (typeof exports !== "undefined") module.exports = filesize; - else if (typeof define === "function") define(function () { return filesize; }); - else global.filesize = filesize; -})(this); \ No newline at end of file + // CommonJS, AMD, script tag + if ( typeof exports !== "undefined" ) { + module.exports = filesize; + } + else if ( typeof define === "function" ) { + define( function () { + return filesize; + }); + } + else { + global.filesize = filesize; + } +})( this ); diff --git a/test/filesize_test.js b/test/filesize_test.js index 32ceb7a..ad79653 100644 --- a/test/filesize_test.js +++ b/test/filesize_test.js @@ -12,24 +12,42 @@ exports["filesize"] = { done(); }, valid: function (test) { - test.expect(16); - test.equal(filesize(this.Kb), "3.91Kb", "Should match"); - test.equal(filesize(this.Kb,true), "3.9k", "Should match"); - test.equal(filesize(this.num), "1.00KB", "Should match"); - test.equal(filesize(this.str), "1.00KB", "Should match"); - test.equal(filesize(this.num, 1), "1.0KB", "Should match"); - test.equal(filesize(this.str, 1), "1.0KB", "Should match"); - test.equal(filesize(this.num, true), "1K", "Should match"); - test.equal(filesize(this.str, true), "1K", "Should match"); - test.equal(filesize(this.neg), "-1.00KB", "Should match"); - test.equal(filesize(this.neg, true), "-1K", "Should match"); - test.equal(filesize(this.byte), "1.00B", "Should match"); - test.equal(filesize(this.byte, 1), "1.0B", "Should match"); - test.equal(filesize(this.byte, true), "1B", "Should match"); - test.equal(filesize(this.zero), "0.00B", "Should match"); - test.equal(filesize(this.zero, 1), "0.0B", "Should match"); - test.equal(filesize(this.zero, true), "0B", "Should match"); - this.byte = 1; + test.expect(28); + + test.equal(filesize(this.Kb), "3.91Kb", "Should be '3.91Kb'"); + test.equal(filesize(this.Kb, 1), "3.9Kb", "Should be '3.9Kb'"); + test.equal(filesize(this.Kb, 1, false), "500.0B", "Should be '500.0B'"); + test.equal(filesize(this.Kb, true), "3.9k", "Should be '3.9k'"); + test.equal(filesize(this.Kb, true, false), "500B", "Should be '500B'"); + + test.equal(filesize(this.num), "1.00KB", "Should be '1.00KB'"); + test.equal(filesize(this.num, 1), "1.0KB", "Should be '1.0KB'"); + test.equal(filesize(this.num, 1, false), "1.0KB", "Should be '1.0KB'"); + test.equal(filesize(this.num, true), "1K", "Should be '1K'"); + test.equal(filesize(this.num, true, false), "1K", "Should be '1K'"); + + test.equal(filesize(this.str), "1.00KB", "Should be '1.00KB'"); + test.equal(filesize(this.str, 1), "1.0KB", "Should be '1.0KB'"); + test.equal(filesize(this.str, 1, false), "1.0KB", "Should be '1.0KB'"); + test.equal(filesize(this.str, true), "1K", "Should be '1K'"); + test.equal(filesize(this.str, true, false), "1K", "Should be '1K'"); + + test.equal(filesize(this.neg), "-1.00KB", "Should be '-1.00KB'"); + test.equal(filesize(this.neg, 1), "-1.0KB", "Should be '-1.0KB'"); + test.equal(filesize(this.neg, 1, false), "-1.0KB", "Should be '-1.0KB'"); + test.equal(filesize(this.neg, true), "-1K", "Should be '-1KB'"); + test.equal(filesize(this.neg, true, false), "-1K", "Should be '-1KB'"); + + test.equal(filesize(this.byte), "1.00B", "Should be '1.00B'"); + test.equal(filesize(this.byte, 1), "1.0B", "Should be '1.0B'"); + test.equal(filesize(this.byte, 1, false), "1.0B", "Should be '1.0B'"); + test.equal(filesize(this.byte, true), "1B", "Should be '1B'"); + test.equal(filesize(this.byte, true, false), "1B", "Should be '1B'"); + + test.equal(filesize(this.zero), "0.00B", "Should be '0.00B'"); + test.equal(filesize(this.zero, 1), "0.0B", "Should be '0.0B'"); + test.equal(filesize(this.zero, true), "0B", "Should be '0B'"); + test.done(); }, invalid: function (test) {