diff --git a/Gruntfile.js b/Gruntfile.js index aefc50f..ca7eb0b 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -22,7 +22,7 @@ module.exports = function(grunt) { "src/si.js", "src/outro.js" ], - dest : "lib/filesize.js" + dest : "lib/filesize.es6.js" } }, exec : { @@ -33,11 +33,15 @@ module.exports = function(grunt) { cmd : "echo //@ sourceMappingURL=<%= pkg.name %>.map >> lib/<%= pkg.name %>.min.js" } }, - jshint : { - options : { - jshintrc : ".jshintrc" + "6to5": { + options: { + sourceMap: false }, - src : "lib/<%= pkg.name %>.js" + dist: { + files: { + "lib/<%= pkg.name %>.js": "lib/<%= pkg.name %>.es6.js" + } + } }, nodeunit : { all : ["test/*.js"] @@ -80,12 +84,12 @@ module.exports = function(grunt) { grunt.loadNpmTasks("grunt-sed"); grunt.loadNpmTasks("grunt-contrib-concat"); grunt.loadNpmTasks("grunt-contrib-nodeunit"); - grunt.loadNpmTasks("grunt-contrib-jshint"); grunt.loadNpmTasks('grunt-contrib-watch'); grunt.loadNpmTasks("grunt-contrib-uglify"); + grunt.loadNpmTasks("grunt-6to5"); // aliases - grunt.registerTask("test", ["jshint", "nodeunit"]); - grunt.registerTask("build", ["concat", "sed"]); + grunt.registerTask("test", [ "nodeunit"]); + grunt.registerTask("build", ["concat", "sed", "6to5"]); grunt.registerTask("default", ["build", "test", "uglify"]); }; diff --git a/lib/filesize.es6.js b/lib/filesize.es6.js new file mode 100644 index 0000000..5cb8f3d --- /dev/null +++ b/lib/filesize.es6.js @@ -0,0 +1,145 @@ +/** + * filesize + * + * @author Jason Mulligan + * @copyright 2015 Jason Mulligan + * @license BSD-3 + * @link http://filesizejs.com + * @module filesize + * @version 3.1.0 + */ +( global ) => { +const bit = /b$/; + +/** + * filesize + * + * @method filesize + * @param {Mixed} arg String, Int or Float to transform + * @param {Object} descriptor [Optional] Flags + * @return {String} Readable file size String + */ +let filesize = ( arg, descriptor ) => { + let result = [], + skip = false, + val = 0, + e, base, bits, ceil, neg, num, output, round, unix, spacer, suffixes; + + if ( isNaN( arg ) ) { + throw new Error( "Invalid arguments" ); + } + + descriptor = descriptor || {}; + bits = ( descriptor.bits === true ); + unix = ( descriptor.unix === true ); + base = descriptor.base !== undefined ? descriptor.base : 2; + round = descriptor.round !== undefined ? descriptor.round : unix ? 1 : 2; + spacer = descriptor.spacer !== undefined ? descriptor.spacer : unix ? "" : " "; + suffixes = descriptor.suffixes !== undefined ? descriptor.suffixes : {}; + output = descriptor.output !== undefined ? descriptor.output : "string"; + e = descriptor.exponent !== undefined ? descriptor.exponent : -1; + num = Number( arg ); + neg = ( num < 0 ); + ceil = base > 2 ? 1000 : 1024; + + // Flipping a negative number to determine the size + if ( neg ) { + num = -num; + } + + // Zero is now a special case because bytes divide by 1 + if ( num === 0 ) { + result[ 0 ] = 0; + + if ( unix ) { + result[ 1 ] = ""; + } else { + result[ 1 ] = "B"; + } + } else { + // Determining the exponent + if ( e === -1 || isNaN( e ) ) { + e = Math.floor( Math.log( num ) / Math.log( ceil ) ); + } + + // Exceeding supported length, time to reduce & multiply + if ( e > 8 ) { + val = val * ( 1000 * ( e - 8 ) ); + e = 8; + } + + if ( base === 2 ) { + val = num / Math.pow( 2, ( e * 10 ) ); + } else { + val = num / Math.pow( 1000, e ); + } + + if ( bits ) { + val = ( val * 8 ); + + if ( val > ceil ) { + val = val / ceil; + e++; + } + } + + result[ 0 ] = Number( val.toFixed( e > 0 ? round : 0 ) ); + result[ 1 ] = si[ bits ? "bits" : "bytes" ][ e ]; + + if ( !skip && unix ) { + if ( bits && bit.test( result[ 1 ] ) ) { + result[ 1 ] = result[ 1 ].toLowerCase(); + } + + result[ 1 ] = result[ 1 ].charAt( 0 ); + + if ( result[ 1 ] === "B" ) { + result[ 0 ] = Math.floor( result[ 0 ] ); + result[ 1 ] = ""; + } else if ( !bits && result[ 1 ] === "k" ) { + result[ 1 ] = "K"; + } + } + } + + // Decorating a 'diff' + if ( neg ) { + result[ 0 ] = -result[ 0 ]; + } + + // Applying custom suffix + result[ 1 ] = suffixes[ result[ 1 ] ] || result[ 1 ]; + + // Returning Array, Object, or String (default) + if ( output === "array" ) { + return result; + } else if ( output === "exponent" ) { + return e; + } else if ( output === "object" ) { + return { value: result[ 0 ], suffix: result[ 1 ] }; + } else { + return result.join( spacer ); + } +} + +/** + * SI suffixes + * + * @type {Object} + */ +const si = { + bits: [ "B", "kb", "Mb", "Gb", "Tb", "Pb", "Eb", "Zb", "Yb" ], + bytes: [ "B", "kB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB" ] +}; + +// CommonJS, AMD, script tag +if ( typeof exports !== "undefined" ) { + module.exports = filesize; +} else if ( typeof define === "function" ) { + define( () => { + return filesize; + } ); +} else { + global.filesize = filesize; +} +}( this ); diff --git a/lib/filesize.js b/lib/filesize.js index 7986a05..e430f64 100644 --- a/lib/filesize.js +++ b/lib/filesize.js @@ -1,3 +1,5 @@ +"use strict"; + /** * filesize * @@ -6,151 +8,150 @@ * @license BSD-3 * @link http://filesizejs.com * @module filesize - * @version 3.0.2 - */ -( function ( global ) { -"use strict"; - -var bit = /b$/; - -/** - * filesize - * - * @method filesize - * @param {Mixed} arg String, Int or Float to transform - * @param {Object} descriptor [Optional] Flags - * @return {String} Readable file size String - */ -function filesize ( arg, descriptor ) { - var result = [], - skip = false, - val = 0, - e, base, bits, ceil, neg, num, output, round, unix, spacer, suffixes; - - if ( isNaN( arg ) ) { - throw new Error( "Invalid arguments" ); - } - - descriptor = descriptor || {}; - bits = ( descriptor.bits === true ); - unix = ( descriptor.unix === true ); - base = descriptor.base !== undefined ? descriptor.base : 2; - round = descriptor.round !== undefined ? descriptor.round : unix ? 1 : 2; - spacer = descriptor.spacer !== undefined ? descriptor.spacer : unix ? "" : " "; - suffixes = descriptor.suffixes !== undefined ? descriptor.suffixes : {}; - output = descriptor.output !== undefined ? descriptor.output : "string"; - e = descriptor.exponent !== undefined ? descriptor.exponent : -1; - num = Number( arg ); - neg = ( num < 0 ); - ceil = base > 2 ? 1000 : 1024; - - // Flipping a negative number to determine the size - if ( neg ) { - num = -num; - } - - // Zero is now a special case because bytes divide by 1 - if ( num === 0 ) { - result[ 0 ] = 0; - - if ( unix ) { - result[ 1 ] = ""; - } - else { - result[ 1 ] = "B"; - } - } - else { - // Determining the exponent - if ( e === -1 || isNaN( e ) ) { - e = Math.floor( Math.log( num ) / Math.log( ceil ) ); - } - - // Exceeding supported length, time to reduce & multiply - if ( e > 8 ) { - val = val * ( 1000 * ( e - 8 ) ); - e = 8; - } - - if ( base === 2 ) { - val = num / Math.pow( 2, ( e * 10 ) ); - } - else { - val = num / Math.pow( 1000, e ); - } - - if ( bits ) { - val = ( val * 8 ); - - if ( val > ceil ) { - val = val / ceil; - e++; - } - } - - result[ 0 ] = Number( val.toFixed( e > 0 ? round : 0 ) ); - result[ 1 ] = si[ bits ? "bits" : "bytes" ][ e ]; - - if ( !skip && unix ) { - if ( bits && bit.test( result[ 1 ] ) ) { - result[ 1 ] = result[ 1 ].toLowerCase(); - } - - result[ 1 ] = result[ 1 ].charAt( 0 ); - - if ( result[ 1 ] === "B" ) { - result[ 0 ] = Math.floor( result[ 0 ] ); - result[ 1 ] = ""; - } - else if ( !bits && result[ 1 ] === "k" ) { - result[ 1 ] = "K"; - } - } - } - - // Decorating a 'diff' - if ( neg ) { - result[ 0 ] = -result[ 0 ]; - } - - // Applying custom suffix - result[ 1 ] = suffixes[ result[ 1 ] ] || result[ 1 ]; - - // Returning Array, Object, or String (default) - if ( output === "array" ) { - return result; - } - else if ( output === "exponent" ) { - return e; - } - else if ( output === "object" ) { - return { value: result[ 0 ], suffix: result[ 1 ] }; - } - else { - return result.join( spacer ); - } -} - -/** - * SI suffixes - * - * @type {Object} + * @version 3.1.0 */ -var si = { - bits: [ "B", "kb", "Mb", "Gb", "Tb", "Pb", "Eb", "Zb", "Yb" ], - bytes: [ "B", "kB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB" ] -}; - -// 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 ); +(function (global) { + var bit = /b$/; + + /** + * filesize + * + * @method filesize + * @param {Mixed} arg String, Int or Float to transform + * @param {Object} descriptor [Optional] Flags + * @return {String} Readable file size String + */ + var filesize = function (arg, descriptor) { + var result = [], + skip = false, + val = 0, + e = undefined, + base = undefined, + bits = undefined, + ceil = undefined, + neg = undefined, + num = undefined, + output = undefined, + round = undefined, + unix = undefined, + spacer = undefined, + suffixes = undefined; + + if (isNaN(arg)) { + throw new Error("Invalid arguments"); + } + + descriptor = descriptor || {}; + bits = descriptor.bits === true; + unix = descriptor.unix === true; + base = descriptor.base !== undefined ? descriptor.base : 2; + round = descriptor.round !== undefined ? descriptor.round : unix ? 1 : 2; + spacer = descriptor.spacer !== undefined ? descriptor.spacer : unix ? "" : " "; + suffixes = descriptor.suffixes !== undefined ? descriptor.suffixes : {}; + output = descriptor.output !== undefined ? descriptor.output : "string"; + e = descriptor.exponent !== undefined ? descriptor.exponent : -1; + num = Number(arg); + neg = num < 0; + ceil = base > 2 ? 1000 : 1024; + + // Flipping a negative number to determine the size + if (neg) { + num = -num; + } + + // Zero is now a special case because bytes divide by 1 + if (num === 0) { + result[0] = 0; + + if (unix) { + result[1] = ""; + } else { + result[1] = "B"; + } + } else { + // Determining the exponent + if (e === -1 || isNaN(e)) { + e = Math.floor(Math.log(num) / Math.log(ceil)); + } + + // Exceeding supported length, time to reduce & multiply + if (e > 8) { + val = val * (1000 * (e - 8)); + e = 8; + } + + if (base === 2) { + val = num / Math.pow(2, e * 10); + } else { + val = num / Math.pow(1000, e); + } + + if (bits) { + val = val * 8; + + if (val > ceil) { + val = val / ceil; + e++; + } + } + + result[0] = Number(val.toFixed(e > 0 ? round : 0)); + result[1] = si[bits ? "bits" : "bytes"][e]; + + if (!skip && unix) { + if (bits && bit.test(result[1])) { + result[1] = result[1].toLowerCase(); + } + + result[1] = result[1].charAt(0); + + if (result[1] === "B") { + result[0] = Math.floor(result[0]); + result[1] = ""; + } else if (!bits && result[1] === "k") { + result[1] = "K"; + } + } + } + + // Decorating a 'diff' + if (neg) { + result[0] = -result[0]; + } + + // Applying custom suffix + result[1] = suffixes[result[1]] || result[1]; + + // Returning Array, Object, or String (default) + if (output === "array") { + return result; + } else if (output === "exponent") { + return e; + } else if (output === "object") { + return { value: result[0], suffix: result[1] }; + } else { + return result.join(spacer); + } + }; + + /** + * SI suffixes + * + * @type {Object} + */ + var si = { + bits: ["B", "kb", "Mb", "Gb", "Tb", "Pb", "Eb", "Zb", "Yb"], + bytes: ["B", "kB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"] + }; + + // 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); \ No newline at end of file diff --git a/lib/filesize.min.js b/lib/filesize.min.js index a0a1481..8156188 100644 --- a/lib/filesize.min.js +++ b/lib/filesize.min.js @@ -1,6 +1,6 @@ /* 2015 Jason Mulligan - @version 3.0.2 + @version 3.1.0 */ -!function(a){"use strict";function b(a,b){var e,f,g,h,i,j,k,l,m,n,o,p=[],q=!1,r=0;if(isNaN(a))throw new Error("Invalid arguments");return b=b||{},g=b.bits===!0,m=b.unix===!0,f=void 0!==b.base?b.base:2,l=void 0!==b.round?b.round:m?1:2,n=void 0!==b.spacer?b.spacer:m?"":" ",o=void 0!==b.suffixes?b.suffixes:{},k=void 0!==b.output?b.output:"string",e=void 0!==b.exponent?b.exponent:-1,j=Number(a),i=0>j,h=f>2?1e3:1024,i&&(j=-j),0===j?(p[0]=0,p[1]=m?"":"B"):((-1===e||isNaN(e))&&(e=Math.floor(Math.log(j)/Math.log(h))),e>8&&(r=1e3*r*(e-8),e=8),r=2===f?j/Math.pow(2,10*e):j/Math.pow(1e3,e),g&&(r=8*r,r>h&&(r/=h,e++)),p[0]=Number(r.toFixed(e>0?l:0)),p[1]=d[g?"bits":"bytes"][e],!q&&m&&(g&&c.test(p[1])&&(p[1]=p[1].toLowerCase()),p[1]=p[1].charAt(0),"B"===p[1]?(p[0]=Math.floor(p[0]),p[1]=""):g||"k"!==p[1]||(p[1]="K"))),i&&(p[0]=-p[0]),p[1]=o[p[1]]||p[1],"array"===k?p:"exponent"===k?e:"object"===k?{value:p[0],suffix:p[1]}:p.join(n)}var c=/b$/,d={bits:["B","kb","Mb","Gb","Tb","Pb","Eb","Zb","Yb"],bytes:["B","kB","MB","GB","TB","PB","EB","ZB","YB"]};"undefined"!=typeof exports?module.exports=b:"function"==typeof define?define(function(){return b}):a.filesize=b}(this); +"use strict";!function(a){var b=/b$/,c=function(a,c){var e=[],f=!1,g=0,h=void 0,i=void 0,j=void 0,k=void 0,l=void 0,m=void 0,n=void 0,o=void 0,p=void 0,q=void 0,r=void 0;if(isNaN(a))throw new Error("Invalid arguments");return c=c||{},j=c.bits===!0,p=c.unix===!0,i=void 0!==c.base?c.base:2,o=void 0!==c.round?c.round:p?1:2,q=void 0!==c.spacer?c.spacer:p?"":" ",r=void 0!==c.suffixes?c.suffixes:{},n=void 0!==c.output?c.output:"string",h=void 0!==c.exponent?c.exponent:-1,m=Number(a),l=0>m,k=i>2?1e3:1024,l&&(m=-m),0===m?(e[0]=0,e[1]=p?"":"B"):((-1===h||isNaN(h))&&(h=Math.floor(Math.log(m)/Math.log(k))),h>8&&(g=1e3*g*(h-8),h=8),g=2===i?m/Math.pow(2,10*h):m/Math.pow(1e3,h),j&&(g=8*g,g>k&&(g/=k,h++)),e[0]=Number(g.toFixed(h>0?o:0)),e[1]=d[j?"bits":"bytes"][h],!f&&p&&(j&&b.test(e[1])&&(e[1]=e[1].toLowerCase()),e[1]=e[1].charAt(0),"B"===e[1]?(e[0]=Math.floor(e[0]),e[1]=""):j||"k"!==e[1]||(e[1]="K"))),l&&(e[0]=-e[0]),e[1]=r[e[1]]||e[1],"array"===n?e:"exponent"===n?h:"object"===n?{value:e[0],suffix:e[1]}:e.join(q)},d={bits:["B","kb","Mb","Gb","Tb","Pb","Eb","Zb","Yb"],bytes:["B","kB","MB","GB","TB","PB","EB","ZB","YB"]};"undefined"!=typeof exports?module.exports=c:"function"==typeof define?define(function(){return c}):a.filesize=c}(this); //# sourceMappingURL=filesize.min.js.map \ No newline at end of file diff --git a/lib/filesize.min.js.map b/lib/filesize.min.js.map index 74be1e8..b11f4b0 100644 --- a/lib/filesize.min.js.map +++ b/lib/filesize.min.js.map @@ -1 +1 @@ -{"version":3,"file":"filesize.min.js","sources":["filesize.js"],"names":["global","filesize","arg","descriptor","e","base","bits","ceil","neg","num","output","round","unix","spacer","suffixes","result","skip","val","isNaN","Error","undefined","exponent","Number","Math","floor","log","pow","toFixed","si","bit","test","toLowerCase","charAt","value","suffix","join","bytes","exports","module","define","this"],"mappings":";;;;CAUA,SAAaA,GACb,YAYA,SAASC,GAAWC,EAAKC,GACxB,GAGCC,GAAGC,EAAMC,EAAMC,EAAMC,EAAKC,EAAKC,EAAQC,EAAOC,EAAMC,EAAQC,EAHzDC,KACHC,GAAO,EACPC,EAAM,CAGP,IAAKC,MAAOhB,GACX,KAAM,IAAIiB,OAAO,oBAyFlB,OAtFAhB,GAAaA,MACbG,EAASH,EAAWG,QAAS,EAC7BM,EAAST,EAAWS,QAAS,EAC7BP,EAA2Be,SAApBjB,EAAWE,KAAqBF,EAAWE,KAAO,EACzDM,EAA6BS,SAArBjB,EAAWQ,MAAsBR,EAAWQ,MAAQC,EAAO,EAAI,EACvEC,EAA+BO,SAAtBjB,EAAWU,OAAuBV,EAAWU,OAASD,EAAO,GAAK,IAC3EE,EAAmCM,SAAxBjB,EAAWW,SAAyBX,EAAWW,YAC1DJ,EAA+BU,SAAtBjB,EAAWO,OAAuBP,EAAWO,OAAS,SAC/DN,EAA4BgB,SAAxBjB,EAAWkB,SAAyBlB,EAAWkB,SAAW,GAC9DZ,EAAMa,OAAQpB,GACdM,EAAc,EAANC,EACRF,EAAOF,EAAO,EAAI,I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OtC,KAIRD,EAAOC,SAAWA,GAEduC","sourcesContent":["/**\n * filesize\n *\n * @author Jason Mulligan \n * @copyright 2015 Jason Mulligan\n * @license BSD-3 \n * @link http://filesizejs.com\n * @module filesize\n * @version 3.0.2\n */\n( function ( global ) {\n\"use strict\";\n\nvar bit = /b$/;\n\n/**\n * filesize\n *\n * @method filesize\n * @param {Mixed} arg String, Int or Float to transform\n * @param {Object} descriptor [Optional] Flags\n * @return {String} Readable file size String\n */\nfunction filesize ( arg, descriptor ) {\n\tvar result = [],\n\t\tskip = false,\n\t\tval = 0,\n\t\te, base, bits, ceil, neg, num, output, round, unix, spacer, suffixes;\n\n\tif ( isNaN( arg ) ) {\n\t\tthrow new Error( \"Invalid arguments\" );\n\t}\n\n\tdescriptor = descriptor || {};\n\tbits = ( descriptor.bits === true );\n\tunix = ( descriptor.unix === true );\n\tbase = descriptor.base !== undefined ? descriptor.base : 2;\n\tround = descriptor.round !== undefined ? descriptor.round : unix ? 1 : 2;\n\tspacer = descriptor.spacer !== undefined ? descriptor.spacer : unix ? \"\" : \" \";\n\tsuffixes = descriptor.suffixes !== undefined ? descriptor.suffixes : {};\n\toutput = descriptor.output !== undefined ? descriptor.output : \"string\";\n\te = descriptor.exponent !== undefined ? descriptor.exponent : -1;\n\tnum = Number( arg );\n\tneg = ( num < 0 );\n\tceil = base > 2 ? 1000 : 1024;\n\n\t// Flipping a negative number to determine the size\n\tif ( neg ) {\n\t\tnum = -num;\n\t}\n\n\t// Zero is now a special case because bytes divide by 1\n\tif ( num === 0 ) {\n\t\tresult[ 0 ] = 0;\n\n\t\tif ( unix ) {\n\t\t\tresult[ 1 ] = \"\";\n\t\t}\n\t\telse {\n\t\t\tresult[ 1 ] = \"B\";\n\t\t}\n\t}\n\telse {\n\t\t// Determining the exponent\n\t\tif ( e === -1 || isNaN( e ) ) {\n\t\t\te = Math.floor( Math.log( num ) / Math.log( ceil ) );\n\t\t}\n\n\t\t// Exceeding supported length, time to reduce & multiply\n\t\tif ( e > 8 ) {\n\t\t\tval = val * ( 1000 * ( e - 8 ) );\n\t\t\te = 8;\n\t\t}\n\n\t\tif ( base === 2 ) {\n\t\t\tval = num / Math.pow( 2, ( e * 10 ) );\n\t\t}\n\t\telse {\n\t\t\tval = num / Math.pow( 1000, e );\n\t\t}\n\n\t\tif ( bits ) {\n\t\t\tval = ( val * 8 );\n\n\t\t\tif ( val > ceil ) {\n\t\t\t\tval = val / ceil;\n\t\t\t\te++;\n\t\t\t}\n\t\t}\n\n\t\tresult[ 0 ] = Number( val.toFixed( e > 0 ? round : 0 ) );\n\t\tresult[ 1 ] = si[ bits ? \"bits\" : \"bytes\" ][ e ];\n\n\t\tif ( !skip && unix ) {\n\t\t\tif ( bits && bit.test( result[ 1 ] ) ) {\n\t\t\t\tresult[ 1 ] = result[ 1 ].toLowerCase();\n\t\t\t}\n\n\t\t\tresult[ 1 ] = result[ 1 ].charAt( 0 );\n\n\t\t\tif ( result[ 1 ] === \"B\" ) {\n\t\t\t\tresult[ 0 ] = Math.floor( result[ 0 ] );\n\t\t\t\tresult[ 1 ] = \"\";\n\t\t\t}\n\t\t\telse if ( !bits && result[ 1 ] === \"k\" ) {\n\t\t\t\tresult[ 1 ] = \"K\";\n\t\t\t}\n\t\t}\n\t}\n\n\t// Decorating a 'diff'\n\tif ( neg ) {\n\t\tresult[ 0 ] = -result[ 0 ];\n\t}\n\n\t// Applying custom suffix\n\tresult[ 1 ] = suffixes[ result[ 1 ] ] || result[ 1 ];\n\n\t// Returning Array, Object, or String (default)\n\tif ( output === \"array\" ) {\n\t\treturn result;\n\t}\n\telse if ( output === \"exponent\" ) {\n\t\treturn e;\n\t}\n\telse if ( output === \"object\" ) {\n\t\treturn { value: result[ 0 ], suffix: result[ 1 ] };\n\t}\n\telse {\n\t\treturn result.join( spacer );\n\t}\n}\n\n/**\n * SI suffixes\n *\n * @type {Object}\n */\nvar si = {\n\tbits: [ \"B\", \"kb\", \"Mb\", \"Gb\", \"Tb\", \"Pb\", \"Eb\", \"Zb\", \"Yb\" ],\n\tbytes: [ \"B\", \"kB\", \"MB\", \"GB\", \"TB\", \"PB\", \"EB\", \"ZB\", \"YB\" ]\n};\n\n// CommonJS, AMD, script tag\nif ( typeof exports !== \"undefined\" ) {\n\tmodule.exports = filesize;\n}\nelse if ( typeof define === \"function\" ) {\n\tdefine( function () {\n\t\treturn filesize;\n\t} );\n}\nelse {\n\tglobal.filesize = filesize;\n}\n} )( this );\n"]} \ No newline at end of file +{"version":3,"file":"filesize.min.js","sources":["filesize.js"],"names":["global","bit","filesize","arg","descriptor","result","skip","val","e","undefined","base","bits","ceil","neg","num","output","round","unix","spacer","suffixes","isNaN","Error","exponent","Number","Math","floor","log","pow","toFixed","si","test","toLowerCase","charAt","value","suffix","join","bytes","exports","module","define","this"],"mappings":";;;;AAAA,cAYA,SAAWA,GACT,GAAIC,GAAM,KAUNC,EAAW,SAAUC,EAAKC,GAC5B,GAAIC,MACAC,GAAO,EACPC,EAAM,EACNC,EAAIC,OACJC,EAAOD,OACPE,EAAOF,OACPG,EAAOH,OACPI,EAAMJ,OACNK,EAAML,OACNM,EAASN,OACTO,EAAQP,OACRQ,EAAOR,OACPS,EAAST,OACTU,EAAWV,MAEf,IAAIW,MAAMjB,GACR,KAAM,IAAIkB,OAAM,oBAqFlB,OAlFAjB,GAAaA,MACbO,EAAOP,EAAWO,QAAS,EAC3BM,EAAOb,EAAWa,QAAS,EAC3BP,EAA2BD,SAApBL,EAAWM,KAAqBN,EAAWM,KAAO,EACzDM,EAA6BP,SAArBL,EAAWY,MAAsBZ,EAAWY,MAAQC,EAAO,EAAI,EACvEC,EAA+BT,SAAtBL,EAAWc,OAAuBd,EAAWc,OAASD,EAAO,GAAK,IAC3EE,EAAmCV,SAAxBL,EAAWe,SAAyBf,EAAWe,YAC1DJ,EAA+BN,SAAtBL,EAAWW,OAAuBX,EAAWW,OAAS,SAC/DP,EAA4BC,SAAxBL,EAAWkB,SAAyBlB,EAAWkB,SAAW,GAC9DR,EAAM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,MAAOrC,KAGTF,EAAOE,SAAWA,GAEnBsC","sourcesContent":["\"use strict\";\n\n/**\n * filesize\n *\n * @author Jason Mulligan \n * @copyright 2015 Jason Mulligan\n * @license BSD-3 \n * @link http://filesizejs.com\n * @module filesize\n * @version 3.1.0\n */\n(function (global) {\n var bit = /b$/;\n\n /**\n * filesize\n *\n * @method filesize\n * @param {Mixed} arg String, Int or Float to transform\n * @param {Object} descriptor [Optional] Flags\n * @return {String} Readable file size String\n */\n var filesize = function (arg, descriptor) {\n var result = [],\n skip = false,\n val = 0,\n e = undefined,\n base = undefined,\n bits = undefined,\n ceil = undefined,\n neg = undefined,\n num = undefined,\n output = undefined,\n round = undefined,\n unix = undefined,\n spacer = undefined,\n suffixes = undefined;\n\n if (isNaN(arg)) {\n throw new Error(\"Invalid arguments\");\n }\n\n descriptor = descriptor || {};\n bits = descriptor.bits === true;\n unix = descriptor.unix === true;\n base = descriptor.base !== undefined ? descriptor.base : 2;\n round = descriptor.round !== undefined ? descriptor.round : unix ? 1 : 2;\n spacer = descriptor.spacer !== undefined ? descriptor.spacer : unix ? \"\" : \" \";\n suffixes = descriptor.suffixes !== undefined ? descriptor.suffixes : {};\n output = descriptor.output !== undefined ? descriptor.output : \"string\";\n e = descriptor.exponent !== undefined ? descriptor.exponent : -1;\n num = Number(arg);\n neg = num < 0;\n ceil = base > 2 ? 1000 : 1024;\n\n // Flipping a negative number to determine the size\n if (neg) {\n num = -num;\n }\n\n // Zero is now a special case because bytes divide by 1\n if (num === 0) {\n result[0] = 0;\n\n if (unix) {\n result[1] = \"\";\n } else {\n result[1] = \"B\";\n }\n } else {\n // Determining the exponent\n if (e === -1 || isNaN(e)) {\n e = Math.floor(Math.log(num) / Math.log(ceil));\n }\n\n // Exceeding supported length, time to reduce & multiply\n if (e > 8) {\n val = val * (1000 * (e - 8));\n e = 8;\n }\n\n if (base === 2) {\n val = num / Math.pow(2, e * 10);\n } else {\n val = num / Math.pow(1000, e);\n }\n\n if (bits) {\n val = val * 8;\n\n if (val > ceil) {\n val = val / ceil;\n e++;\n }\n }\n\n result[0] = Number(val.toFixed(e > 0 ? round : 0));\n result[1] = si[bits ? \"bits\" : \"bytes\"][e];\n\n if (!skip && unix) {\n if (bits && bit.test(result[1])) {\n result[1] = result[1].toLowerCase();\n }\n\n result[1] = result[1].charAt(0);\n\n if (result[1] === \"B\") {\n result[0] = Math.floor(result[0]);\n result[1] = \"\";\n } else if (!bits && result[1] === \"k\") {\n result[1] = \"K\";\n }\n }\n }\n\n // Decorating a 'diff'\n if (neg) {\n result[0] = -result[0];\n }\n\n // Applying custom suffix\n result[1] = suffixes[result[1]] || result[1];\n\n // Returning Array, Object, or String (default)\n if (output === \"array\") {\n return result;\n } else if (output === \"exponent\") {\n return e;\n } else if (output === \"object\") {\n return { value: result[0], suffix: result[1] };\n } else {\n return result.join(spacer);\n }\n };\n\n /**\n * SI suffixes\n *\n * @type {Object}\n */\n var si = {\n bits: [\"B\", \"kb\", \"Mb\", \"Gb\", \"Tb\", \"Pb\", \"Eb\", \"Zb\", \"Yb\"],\n bytes: [\"B\", \"kB\", \"MB\", \"GB\", \"TB\", \"PB\", \"EB\", \"ZB\", \"YB\"]\n };\n\n // CommonJS, AMD, script tag\n if (typeof exports !== \"undefined\") {\n module.exports = filesize;\n } else if (typeof define === \"function\") {\n define(function () {\n return filesize;\n });\n } else {\n global.filesize = filesize;\n }\n})(this);"]} \ No newline at end of file diff --git a/package.json b/package.json index 4ff6116..27741a7 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": "3.0.2", + "version": "3.1.0", "homepage": "http://filesizejs.com", "author": { "name": "Jason Mulligan", @@ -31,8 +31,8 @@ "grunt": "~0.4.1", "grunt-cli": "~0.1.6", "grunt-sed": "~0.1", + "grunt-6to5": "^2.0.0", "grunt-contrib-concat": "~0.1.3", - "grunt-contrib-jshint": "~0.1", "grunt-contrib-nodeunit": "~0.1.2", "grunt-contrib-watch": "~0.2", "grunt-contrib-uglify": "~0.6.0" diff --git a/src/filesize.js b/src/filesize.js index fb38691..eb6ad19 100644 --- a/src/filesize.js +++ b/src/filesize.js @@ -6,8 +6,8 @@ * @param {Object} descriptor [Optional] Flags * @return {String} Readable file size String */ -function filesize ( arg, descriptor ) { - var result = [], +let filesize = ( arg, descriptor ) => { + let result = [], skip = false, val = 0, e, base, bits, ceil, neg, num, output, round, unix, spacer, suffixes; @@ -40,12 +40,10 @@ function filesize ( arg, descriptor ) { if ( unix ) { result[ 1 ] = ""; - } - else { + } else { result[ 1 ] = "B"; } - } - else { + } else { // Determining the exponent if ( e === -1 || isNaN( e ) ) { e = Math.floor( Math.log( num ) / Math.log( ceil ) ); @@ -59,8 +57,7 @@ function filesize ( arg, descriptor ) { if ( base === 2 ) { val = num / Math.pow( 2, ( e * 10 ) ); - } - else { + } else { val = num / Math.pow( 1000, e ); } @@ -86,8 +83,7 @@ function filesize ( arg, descriptor ) { if ( result[ 1 ] === "B" ) { result[ 0 ] = Math.floor( result[ 0 ] ); result[ 1 ] = ""; - } - else if ( !bits && result[ 1 ] === "k" ) { + } else if ( !bits && result[ 1 ] === "k" ) { result[ 1 ] = "K"; } } @@ -104,14 +100,11 @@ function filesize ( arg, descriptor ) { // Returning Array, Object, or String (default) if ( output === "array" ) { return result; - } - else if ( output === "exponent" ) { + } else if ( output === "exponent" ) { return e; - } - else if ( output === "object" ) { + } else if ( output === "object" ) { return { value: result[ 0 ], suffix: result[ 1 ] }; - } - else { + } else { return result.join( spacer ); } } diff --git a/src/intro.js b/src/intro.js index 3e77aa9..52f9e93 100644 --- a/src/intro.js +++ b/src/intro.js @@ -1,4 +1,2 @@ -( function ( global ) { -"use strict"; - -var bit = /b$/; +( global ) => { +const bit = /b$/; diff --git a/src/outro.js b/src/outro.js index a742b50..4ab3741 100644 --- a/src/outro.js +++ b/src/outro.js @@ -1,13 +1,11 @@ // CommonJS, AMD, script tag if ( typeof exports !== "undefined" ) { module.exports = filesize; -} -else if ( typeof define === "function" ) { - define( function () { +} else if ( typeof define === "function" ) { + define( () => { return filesize; } ); -} -else { +} else { global.filesize = filesize; } -} )( this ); +}( this ); diff --git a/src/si.js b/src/si.js index b701d31..f4fa728 100644 --- a/src/si.js +++ b/src/si.js @@ -3,7 +3,7 @@ * * @type {Object} */ -var si = { +const si = { bits: [ "B", "kb", "Mb", "Gb", "Tb", "Pb", "Eb", "Zb", "Yb" ], bytes: [ "B", "kB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB" ] };