diff --git a/.jshintrc b/.jshintrc new file mode 100644 index 0000000..142a86c --- /dev/null +++ b/.jshintrc @@ -0,0 +1,26 @@ +{ + "smarttabs": true, + "latedef": true, + "undef": true, + "unused": true, + "proto": true, + "supernew": true, + "quotmark": "double", + "curly": true, + "newcap": true, + "noarg": true, + "noempty": false, + "nonew": true, + "quotmark": "double", + "regexp": false, + "trailing": true, + "boss": true, + "eqnull": true, + "expr": true, + "browser": true, + "globals": { + "require": true, + "module": true, + "define": true + } +} \ No newline at end of file diff --git a/.npmignore b/.npmignore index 9120db0..c4eb1ea 100644 --- a/.npmignore +++ b/.npmignore @@ -3,5 +3,6 @@ lib/filesize.min.js src test .jamignore +.jshintrc .travis.yml Gruntfile.js diff --git a/Gruntfile.js b/Gruntfile.js index f3bffdc..3dd098a 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -21,49 +21,53 @@ module.exports = function(grunt) { ], dest : "lib/filesize.js" } + },exec : { + closure : { + cmd : "cd lib\nclosure-compiler --js <%= pkg.name %>.js --js_output_file <%= pkg.name %>.min.js --create_source_map ./<%= pkg.name %>.map" + }, + sourcemap : { + cmd : "echo //@ sourceMappingURL=<%= pkg.name %>.map >> lib/<%= pkg.name %>.min.js" + } + }, + jshint : { + options : { + jshintrc : ".jshintrc" + }, + src : "lib/<%= pkg.name %>.js" }, - nodeunit: { + nodeunit : { all : ["test/*.js"] }, - uglify: { - options: { - banner : "/**\n" + - " * <%= pkg.name %>\n" + - " *\n" + - " * @author <%= pkg.author.name %> <<%= pkg.author.email %>>\n" + - " * @copyright <%= grunt.template.today('yyyy') %> <%= pkg.author.name %>\n" + - " * @license <%= pkg.licenses[0].type %> <<%= pkg.licenses[0].url %>>\n" + - " * @link <%= pkg.homepage %>\n" + - " * @module <%= pkg.name %>\n" + - " * @version <%= pkg.version %>\n" + - " */\n", - mangle: { - except: ["filesize"] - } + sed : { + "version" : { + pattern : "{{VERSION}}", + replacement : "<%= pkg.version %>", + path : ["<%= concat.dist.dest %>"] + } + }, + watch : { + js : { + files : "<%= concat.dist.src %>", + tasks : "build" }, - target: { - files: { - "lib/<%= pkg.name %>.min.js" : ["<%= concat.dist.dest %>"] - } + pkg: { + files : "package.json", + tasks : "build" } } }); + // tasks + grunt.loadNpmTasks("grunt-sed"); + grunt.loadNpmTasks("grunt-exec"); grunt.loadNpmTasks("grunt-contrib-concat"); grunt.loadNpmTasks("grunt-contrib-nodeunit"); - grunt.loadNpmTasks("grunt-contrib-uglify"); + grunt.loadNpmTasks("grunt-contrib-jshint"); + grunt.loadNpmTasks('grunt-contrib-watch'); + // aliases + grunt.registerTask("lint", ["jshint"]); grunt.registerTask("test", ["nodeunit"]); - - grunt.registerTask("version", function () { - var cfg = grunt.config("pkg"), - ver = cfg.version, - fn = "lib/" + cfg.name + ".js", - fp = grunt.file.read(fn); - - console.log("Setting version to: " + ver); - grunt.file.write(fn, fp.replace(/\{\{VERSION\}\}/g, ver)); - }); - - grunt.registerTask("default", ["concat", "version", "uglify", "test"]); + grunt.registerTask("build", ["concat", "sed", "exec"]); + grunt.registerTask("default", ["build", "test", "lint"]); }; \ No newline at end of file diff --git a/lib/filesize.js b/lib/filesize.js index 4708903..2319f18 100644 --- a/lib/filesize.js +++ b/lib/filesize.js @@ -6,7 +6,7 @@ * @license BSD-3 * @link http://filesizejs.com * @module filesize - * @version 1.9.3 + * @version 1.9.4 */ ( function ( global ) { "use strict"; @@ -14,24 +14,26 @@ var base = 10, right = /\.(.*)/, bit = /b$/, - byte = /^B$/, + bite = /^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 - } - }; + options; + + 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 - * + * * @param {Mixed} arg String, Int or Float to transform - * @param {Mixed} pos [Optional] Position to round to, defaults to 2 if short is ommitted, or `true` for shorthand output + * @param {Mixed} pos [Optional] Position to round to, defaults to 2 if shrt is ommitted, or `true` for shrthand output * @param {Boolean} bits [Optional] Determines if `bit` sizes are used for result calculation, default is true * @return {String} Readable file size String */ @@ -39,16 +41,16 @@ var result = "", bits = true, skip = false, - i, neg, num, pos, short, size, sizes, suffix, z; + i, neg, num, pos, shrt, size, sizes, suffix, z; // Determining arguments if (arguments[3] !== undefined) { - pos = arguments[1]; - short = arguments[2]; - bits = arguments[3]; + pos = arguments[1]; + shrt = arguments[2]; + bits = arguments[3]; } else { - typeof arguments[1] === "boolean" ? short = arguments[1] : pos = arguments[1]; + typeof arguments[1] === "boolean" ? shrt = arguments[1] : pos = arguments[1]; if ( typeof arguments[2] === "boolean" ) { bits = arguments[2]; @@ -56,14 +58,14 @@ } if ( isNaN( arg ) || ( pos !== undefined && isNaN( pos ) ) ) { - throw Error("Invalid arguments"); + throw new Error("Invalid arguments"); } - short = ( short === true ); - bits = ( bits === true ); - pos = short ? 1 : ( pos === undefined ? 2 : parseInt( pos, base ) ); - num = Number( arg ); - neg = ( num < 0 ); + shrt = ( shrt === true ); + bits = ( bits === true ); + pos = shrt ? 1 : ( pos === undefined ? 2 : parseInt( pos, base ) ); + num = Number( arg ); + neg = ( num < 0 ); // Flipping a negative number to determine the size if ( neg ) { @@ -72,7 +74,12 @@ // Zero is now a special case because bytes divide by 1 if ( num === 0 ) { - result = "0B"; + if ( shrt ) { + result = "0"; + } + else { + result = "0 B"; + } } else { if ( bits ) { @@ -90,14 +97,14 @@ if ( num >= size ) { // Treating bytes as cardinal - if ( byte.test( suffix ) ) { + if ( bite.test( suffix ) ) { skip = true; pos = 0; } result = ( num / size ).toFixed( pos ); - if ( !skip && short ) { + if ( !skip && shrt ) { if ( bits && bit.test( suffix ) ) { suffix = suffix.toLowerCase(); } @@ -108,9 +115,12 @@ if ( z !== null && z[1] !== undefined && zero.test( z[1] ) ) { result = parseInt( result, base ); } - } - result += suffix; + result += suffix; + } + else if ( !shrt ) { + result += " " + suffix; + } break; } } @@ -122,7 +132,7 @@ } return result; - }; + } // CommonJS, AMD, script tag if ( typeof exports !== "undefined" ) { diff --git a/lib/filesize.map b/lib/filesize.map new file mode 100644 index 0000000..6edf4ab --- /dev/null +++ b/lib/filesize.map @@ -0,0 +1,8 @@ +{ +"version":3, +"file":"filesize.min.js", +"lineCount":10, +"mappings":"A;;;;;;;AAUE,SAAS,CAAEA,CAAF,CAAW,CA6BrBC,QAASA,EAAS,CAACC,CAAD,OAAM,CAAA,IACnBC,EAAS,EADU,CAEnBC,EAAS,CAAA,CAFU,CAGnBC,EAAS,CAAA,CAHU,CAINC,CAJM,CAIDC,CAJC,CAIKC,CAJL,CAIWC,CAGbC,KAAAA,EAArB,GAAI,CAAJ,EACCJ,CAEA,CAFO,CAEP,CADAC,CACA,CADO,CACP,CAAAH,CAAA,CAAO,CAHR,GAMyB,SAAxB,GAAA,MAAO,EAAP,CAAoCG,CAApC,CAA2C,CAA3C,CAA0DD,CAA1D,CAAgE,CAEhE,CAA6B,SAA7B,GAAK,MAAO,EAAZ,GACCF,CADD,CACQ,CADR,CARD,CAaA,IAAKO,KAAA,CAAOT,CAAP,CAAL,EAA+BQ,IAAAA,EAA/B,GAAuBJ,CAAvB,EAA4CK,KAAA,CAAOL,CAAP,CAA5C,CACC,KAAUM,MAAJ,CAAU,mBAAV,CAA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QADD;AACkBlC,CADlB,CAG4B,UAAvB,GAAK,MAAOoC,OAAZ,CACJA,MAAA,CAAQ,QAAS,EAAG,CACnB,MAAOpC,EADY,CAApB,CADI,CAMJD,CAAAC,SANI,CAMcA,CAxIE,CAApB,CAAA,CA0IE,IA1IF;", +"sources":["filesize.js"], +"names":["global","filesize","arg","result","bits","skip","pos","shrt","size","sizes","undefined","isNaN","Error","parseInt","base","num","Number","neg","options","all","increments","i","nth","bitless","suffix","bite","test","toFixed","bit","toLowerCase","charAt","z","right","exec","zero","exports","module","define"] +} diff --git a/lib/filesize.min.js b/lib/filesize.min.js index a43e8ab..09a847a 100644 --- a/lib/filesize.min.js +++ b/lib/filesize.min.js @@ -1,11 +1,11 @@ -/** - * filesize - * - * @author Jason Mulligan - * @copyright 2013 Jason Mulligan - * @license BSD-3 - * @link http://filesizejs.com - * @module filesize - * @version 1.9.3 - */ -(function(e){"use strict";function filesize(e){var o,u,l,f,m,B,b,c,d,g="",p=!0,h=!1;if(void 0!==arguments[3]?(f=arguments[1],m=arguments[2],p=arguments[3]):("boolean"==typeof arguments[1]?m=arguments[1]:f=arguments[1],"boolean"==typeof arguments[2]&&(p=arguments[2])),isNaN(e)||void 0!==f&&isNaN(f))throw Error("Invalid arguments");if(m=m===!0,p=p===!0,f=m?1:void 0===f?2:parseInt(f,t),l=Number(e),u=0>l,u&&(l=-l),0===l)g="0B";else for(p?(b=a.all.increments,o=a.all.nth):(b=a.bitless.increments,o=a.bitless.nth);o--;)if(B=b[o][1],c=b[o][0],l>=B){r.test(c)&&(h=!0,f=0),g=(l/B).toFixed(f),!h&&m&&(p&&s.test(c)&&(c=c.toLowerCase()),c=c.charAt(0),d=n.exec(g),null!==d&&void 0!==d[1]&&i.test(d[1])&&(g=parseInt(g,t))),g+=c;break}return u&&(g="-"+g),g}var t=10,n=/\.(.*)/,s=/b$/,r=/^B$/,i=/^0$/,a={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 +/* + 2013 Jason Mulligan + @license BSD-3 + @link http://filesizejs.com + @module filesize + @version 1.9.4 +*/ +(function(q){function h(l,a,f,d){var b="",c=!0,h=!1,e,g,n,m;void 0!==d?(e=a,g=f,c=d):("boolean"===typeof a?g=a:e=a,"boolean"===typeof f&&(c=f));if(isNaN(l)||void 0!==e&&isNaN(e))throw Error("Invalid arguments");g=!0===g;c=!0===c;e=g?1:void 0===e?2:parseInt(e,p);d=Number(l);(l=0>d)&&(d=-d);if(0===d)b=g?"0":"0 B";else{c?(m=k.all.increments,f=k.all.nth):(m=k.bitless.increments,f=k.bitless.nth);for(;f--;)if(n=m[f][1],a=m[f][0],d>=n){r.test(a)&&(h=!0,e=0);b=(d/n).toFixed(e);!h&&g?(c&&s.test(a)&&(a=a.toLowerCase()), +a=a.charAt(0),c=t.exec(b),null!==c&&(void 0!==c[1]&&u.test(c[1]))&&(b=parseInt(b,p)),b+=a):g||(b+=" "+a);break}}l&&(b="-"+b);return b}var p=10,t=/\.(.*)/,s=/b$/,r=/^B$/,u=/^0$/,k;k={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= +h:"function"===typeof define?define(function(){return h}):q.filesize=h})(this); +//@ sourceMappingURL=filesize.map diff --git a/package.json b/package.json index 0ad0e8f..487effd 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.9.3", + "version": "1.9.4", "homepage": "http://filesizejs.com", "author": { "name": "Jason Mulligan", @@ -28,11 +28,13 @@ "test": "grunt test" }, "devDependencies": { - "grunt" : "~0.4.0", - "grunt-cli" : "~ 0.1.6", - "grunt-contrib-concat" : "~ 0.1.3", - "grunt-contrib-nodeunit" : "~ 0.1.2", - "grunt-contrib-uglify" : "~ 0.1.2" + "grunt": "~0.4.1", + "grunt-exec": "~0.4", + "grunt-sed": "~0.1", + "grunt-contrib-concat": "~0.1.3", + "grunt-contrib-jshint": "~0.1", + "grunt-contrib-nodeunit": "~0.1.2", + "grunt-contrib-watch": "~0.2" }, "keywords": ["file", "filesize", "size", "readable", "filesystem"] } \ No newline at end of file diff --git a/src/filesize.js b/src/filesize.js index b3c7cc1..bb6c5d1 100644 --- a/src/filesize.js +++ b/src/filesize.js @@ -4,24 +4,26 @@ var base = 10, right = /\.(.*)/, bit = /b$/, - byte = /^B$/, + bite = /^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 - } - }; + options; + + 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 - * + * * @param {Mixed} arg String, Int or Float to transform - * @param {Mixed} pos [Optional] Position to round to, defaults to 2 if short is ommitted, or `true` for shorthand output + * @param {Mixed} pos [Optional] Position to round to, defaults to 2 if shrt is ommitted, or `true` for shrthand output * @param {Boolean} bits [Optional] Determines if `bit` sizes are used for result calculation, default is true * @return {String} Readable file size String */ @@ -29,16 +31,16 @@ var result = "", bits = true, skip = false, - i, neg, num, pos, short, size, sizes, suffix, z; + i, neg, num, pos, shrt, size, sizes, suffix, z; // Determining arguments if (arguments[3] !== undefined) { - pos = arguments[1]; - short = arguments[2]; - bits = arguments[3]; + pos = arguments[1]; + shrt = arguments[2]; + bits = arguments[3]; } else { - typeof arguments[1] === "boolean" ? short = arguments[1] : pos = arguments[1]; + typeof arguments[1] === "boolean" ? shrt = arguments[1] : pos = arguments[1]; if ( typeof arguments[2] === "boolean" ) { bits = arguments[2]; @@ -46,14 +48,14 @@ } if ( isNaN( arg ) || ( pos !== undefined && isNaN( pos ) ) ) { - throw Error("Invalid arguments"); + throw new Error("Invalid arguments"); } - short = ( short === true ); - bits = ( bits === true ); - pos = short ? 1 : ( pos === undefined ? 2 : parseInt( pos, base ) ); - num = Number( arg ); - neg = ( num < 0 ); + shrt = ( shrt === true ); + bits = ( bits === true ); + pos = shrt ? 1 : ( pos === undefined ? 2 : parseInt( pos, base ) ); + num = Number( arg ); + neg = ( num < 0 ); // Flipping a negative number to determine the size if ( neg ) { @@ -62,7 +64,12 @@ // Zero is now a special case because bytes divide by 1 if ( num === 0 ) { - result = "0B"; + if ( shrt ) { + result = "0"; + } + else { + result = "0 B"; + } } else { if ( bits ) { @@ -80,14 +87,14 @@ if ( num >= size ) { // Treating bytes as cardinal - if ( byte.test( suffix ) ) { + if ( bite.test( suffix ) ) { skip = true; pos = 0; } result = ( num / size ).toFixed( pos ); - if ( !skip && short ) { + if ( !skip && shrt ) { if ( bits && bit.test( suffix ) ) { suffix = suffix.toLowerCase(); } @@ -98,9 +105,12 @@ if ( z !== null && z[1] !== undefined && zero.test( z[1] ) ) { result = parseInt( result, base ); } - } - result += suffix; + result += suffix; + } + else if ( !shrt ) { + result += " " + suffix; + } break; } } @@ -112,7 +122,7 @@ } return result; - }; + } // CommonJS, AMD, script tag if ( typeof exports !== "undefined" ) { diff --git a/test/filesize_test.js b/test/filesize_test.js index 2d600ef..315dad1 100644 --- a/test/filesize_test.js +++ b/test/filesize_test.js @@ -14,39 +14,39 @@ exports["filesize"] = { valid: function (test) { 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), "500B", "Should be '500B'"); - 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.Kb), "3.91 Kb", "Should be '3.91 Kb'"); + test.equal(filesize(this.Kb, 1), "3.9 Kb", "Should be '3.9 Kb'"); + test.equal(filesize(this.Kb, 1, false), "500 B", "Should be '500 B'"); + test.equal(filesize(this.Kb, true), "3.9k", "Should be '3.9k'"); + test.equal(filesize(this.Kb, true, false), "500", "Should be '500'"); + + test.equal(filesize(this.num), "1.00 KB", "Should be '1.00 KB'"); + test.equal(filesize(this.num, 1), "1.0 KB", "Should be '1.0 KB'"); + test.equal(filesize(this.num, 1, false), "1.0 KB", "Should be '1.0 KB'"); + 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.00 KB", "Should be '1.00 KB'"); + test.equal(filesize(this.str, 1), "1.0 KB", "Should be '1.0 KB'"); + test.equal(filesize(this.str, 1, false), "1.0 KB", "Should be '1.0 KB'"); 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), "-1.00 KB", "Should be '-1.00 KB'"); + test.equal(filesize(this.neg, 1), "-1.0 KB", "Should be '-1.0 KB'"); + test.equal(filesize(this.neg, 1, false), "-1.0 KB", "Should be '-1.0 KB'"); 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), "1B", "Should be '1B'"); - test.equal(filesize(this.byte, 1), "1B", "Should be '1B'"); - test.equal(filesize(this.byte, 1, false), "1B", "Should be '1B'"); - 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.byte), "1 B", "Should be '1 B'"); + test.equal(filesize(this.byte, 1), "1 B", "Should be '1 B'"); + test.equal(filesize(this.byte, 1, false), "1 B", "Should be '1 B'"); + test.equal(filesize(this.byte, true), "1", "Should be '1'"); + test.equal(filesize(this.byte, true, false), "1", "Should be '1'"); - test.equal(filesize(this.zero), "0B", "Should be '0B'"); - test.equal(filesize(this.zero, 1), "0B", "Should be '0B'"); - test.equal(filesize(this.zero, true), "0B", "Should be '0B'"); + test.equal(filesize(this.zero), "0 B", "Should be '0 B'"); + test.equal(filesize(this.zero, 1), "0 B", "Should be '0 B'"); + test.equal(filesize(this.zero, true), "0", "Should be '0B'"); test.done(); },