From 31d9a21f22330db33cf256b84ea76301f015a064 Mon Sep 17 00:00:00 2001 From: Jason Mulligan Date: Tue, 29 Jun 2021 20:19:26 -0400 Subject: [PATCH] Implementing `precision` option with tests, fixes #117, fixes #132 Adding test for invalid `precision` value --- lib/filesize.es6.js | 12 +++++++++++- lib/filesize.es6.min.js | 2 +- lib/filesize.es6.min.js.map | 2 +- lib/filesize.js | 15 +++++++++++++-- lib/filesize.min.js | 2 +- lib/filesize.min.js.map | 2 +- src/filesize.js | 12 +++++++++++- test/filesize_test.js | 9 +++++++++ 8 files changed, 48 insertions(+), 8 deletions(-) diff --git a/lib/filesize.es6.js b/lib/filesize.es6.js index 20ad704..60aa292 100644 --- a/lib/filesize.es6.js +++ b/lib/filesize.es6.js @@ -42,7 +42,7 @@ function filesize (arg, descriptor = {}) { let result = [], val = 0, - e, base, bits, ceil, full, fullforms, locale, localeOptions, neg, num, output, pad, round, u, unix, separator, spacer, standard, symbols, roundingFunc; + e, base, bits, ceil, full, fullforms, locale, localeOptions, neg, num, output, pad, round, u, unix, separator, spacer, standard, symbols, roundingFunc, precision; if (isNaN(arg)) { throw new TypeError("Invalid number"); @@ -67,6 +67,7 @@ num = Number(arg); neg = num < 0; ceil = base > 2 ? 1000 : 1024; + precision = isNaN(descriptor.precision) === false ? parseInt(descriptor.precision, 10) : 0; // Flipping a negative number to determine the size if (neg) { @@ -84,6 +85,10 @@ // Exceeding supported length, time to reduce & multiply if (e > 8) { + if (precision > 0) { + precision += 8 - e; + } + e = 8; } @@ -132,6 +137,11 @@ result[0] = -result[0]; } + // Setting optional precision + if (precision > 0) { + result[0] = result[0].toPrecision(precision); + } + // Applying custom symbol result[1] = symbols[result[1]] || result[1]; diff --git a/lib/filesize.es6.min.js b/lib/filesize.es6.min.js index 5d3ce6f..d7c9c0b 100644 --- a/lib/filesize.es6.min.js +++ b/lib/filesize.es6.min.js @@ -2,5 +2,5 @@ 2020 Jason Mulligan @version 6.3.0 */ -!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(e="undefined"!=typeof globalThis?globalThis:e||self).filesize=t()}(this,(function(){"use strict";const e=/^(b|B)$/,t={iec:{bits:["b","Kib","Mib","Gib","Tib","Pib","Eib","Zib","Yib"],bytes:["B","KiB","MiB","GiB","TiB","PiB","EiB","ZiB","YiB"]},jedec:{bits:["b","Kb","Mb","Gb","Tb","Pb","Eb","Zb","Yb"],bytes:["B","KB","MB","GB","TB","PB","EB","ZB","YB"]}},i={iec:["","kibi","mebi","gibi","tebi","pebi","exbi","zebi","yobi"],jedec:["","kilo","mega","giga","tera","peta","exa","zetta","yotta"]},o={floor:Math.floor,ceil:Math.ceil};function n(n,a={}){let b,r,l,s,d,f,c,p,u,B,h,g,y,M,m,x,v,j,T,E,N=[],w=0;if(isNaN(n))throw new TypeError("Invalid number");if(l=!0===a.bits,m=!0===a.unix,g=!0===a.pad,r=a.base||2,y=void 0!==a.round?a.round:m?1:2,c=void 0!==a.locale?a.locale:"",p=a.localeOptions||{},x=void 0!==a.separator?a.separator:"",v=void 0!==a.spacer?a.spacer:m?"":" ",T=a.symbols||{},j=2===r&&a.standard||"jedec",h=a.output||"string",d=!0===a.fullform,f=a.fullforms instanceof Array?a.fullforms:[],b=void 0!==a.exponent?a.exponent:-1,E=o[a.roundingMethod]||Math.round,B=Number(n),u=B<0,s=r>2?1e3:1024,u&&(B=-B),(-1===b||isNaN(b))&&(b=Math.floor(Math.log(B)/Math.log(s)),b<0&&(b=0)),b>8&&(b=8),"exponent"===h)return b;if(0===B)N[0]=0,M=N[1]=m?"":t[j][l?"bits":"bytes"][b];else{w=B/(2===r?Math.pow(2,10*b):Math.pow(1e3,b)),l&&(w*=8,w>=s&&b<8&&(w/=s,b++));const i=Math.pow(10,b>0?y:0);N[0]=E(w*i)/i,N[0]===s&&b<8&&void 0===a.exponent&&(N[0]=1,b++),M=N[1]=10===r&&1===b?l?"kb":"kB":t[j][l?"bits":"bytes"][b],m&&(N[1]="jedec"===j?N[1].charAt(0):b>0?N[1].replace(/B$/,""):N[1],e.test(N[1])&&(N[0]=Math.floor(N[0]),N[1]=""))}if(u&&(N[0]=-N[0]),N[1]=T[N[1]]||N[1],!0===c?N[0]=N[0].toLocaleString():c.length>0?N[0]=N[0].toLocaleString(c,p):x.length>0&&(N[0]=N[0].toString().replace(".",x)),g&&!1===Number.isInteger(N[0])&&y>0){const e=x||".",t=N[0].toString().split(e),i=t[1]||"",o=i.length,n=y-o;N[0]=`${t[0]}${e}${i.padEnd(o+n,"0")}`}return d&&(N[1]=f[b]?f[b]:i[j][b]+(l?"bit":"byte")+(1===N[0]?"":"s")),"array"===h?N:"object"===h?{value:N[0],symbol:N[1],exponent:b,unit:M}:N.join(v)}return n.partial=e=>t=>n(t,e),n})); +!function(e,i){"object"==typeof exports&&"undefined"!=typeof module?module.exports=i():"function"==typeof define&&define.amd?define(i):(e="undefined"!=typeof globalThis?globalThis:e||self).filesize=i()}(this,(function(){"use strict";const e=/^(b|B)$/,i={iec:{bits:["b","Kib","Mib","Gib","Tib","Pib","Eib","Zib","Yib"],bytes:["B","KiB","MiB","GiB","TiB","PiB","EiB","ZiB","YiB"]},jedec:{bits:["b","Kb","Mb","Gb","Tb","Pb","Eb","Zb","Yb"],bytes:["B","KB","MB","GB","TB","PB","EB","ZB","YB"]}},t={iec:["","kibi","mebi","gibi","tebi","pebi","exbi","zebi","yobi"],jedec:["","kilo","mega","giga","tera","peta","exa","zetta","yotta"]},o={floor:Math.floor,ceil:Math.ceil};function n(n,r={}){let a,b,s,l,c,p,d,f,u,B,h,g,y,M,m,x,v,N,j,T,E,w=[],P=0;if(isNaN(n))throw new TypeError("Invalid number");if(s=!0===r.bits,m=!0===r.unix,g=!0===r.pad,b=r.base||2,y=void 0!==r.round?r.round:m?1:2,d=void 0!==r.locale?r.locale:"",f=r.localeOptions||{},x=void 0!==r.separator?r.separator:"",v=void 0!==r.spacer?r.spacer:m?"":" ",j=r.symbols||{},N=2===b&&r.standard||"jedec",h=r.output||"string",c=!0===r.fullform,p=r.fullforms instanceof Array?r.fullforms:[],a=void 0!==r.exponent?r.exponent:-1,T=o[r.roundingMethod]||Math.round,B=Number(n),u=B<0,l=b>2?1e3:1024,E=!1===isNaN(r.precision)?parseInt(r.precision,10):0,u&&(B=-B),(-1===a||isNaN(a))&&(a=Math.floor(Math.log(B)/Math.log(l)),a<0&&(a=0)),a>8&&(E>0&&(E+=8-a),a=8),"exponent"===h)return a;if(0===B)w[0]=0,M=w[1]=m?"":i[N][s?"bits":"bytes"][a];else{P=B/(2===b?Math.pow(2,10*a):Math.pow(1e3,a)),s&&(P*=8,P>=l&&a<8&&(P/=l,a++));const t=Math.pow(10,a>0?y:0);w[0]=T(P*t)/t,w[0]===l&&a<8&&void 0===r.exponent&&(w[0]=1,a++),M=w[1]=10===b&&1===a?s?"kb":"kB":i[N][s?"bits":"bytes"][a],m&&(w[1]="jedec"===N?w[1].charAt(0):a>0?w[1].replace(/B$/,""):w[1],e.test(w[1])&&(w[0]=Math.floor(w[0]),w[1]=""))}if(u&&(w[0]=-w[0]),E>0&&(w[0]=w[0].toPrecision(E)),w[1]=j[w[1]]||w[1],!0===d?w[0]=w[0].toLocaleString():d.length>0?w[0]=w[0].toLocaleString(d,f):x.length>0&&(w[0]=w[0].toString().replace(".",x)),g&&!1===Number.isInteger(w[0])&&y>0){const e=x||".",i=w[0].toString().split(e),t=i[1]||"",o=t.length,n=y-o;w[0]=`${i[0]}${e}${t.padEnd(o+n,"0")}`}return c&&(w[1]=p[a]?p[a]:t[N][a]+(s?"bit":"byte")+(1===w[0]?"":"s")),"array"===h?w:"object"===h?{value:w[0],symbol:w[1],exponent:a,unit:M}:w.join(v)}return n.partial=e=>i=>n(i,e),n})); //# sourceMappingURL=filesize.es6.min.js.map diff --git a/lib/filesize.es6.min.js.map b/lib/filesize.es6.min.js.map index 78d173c..3f3a6bd 100644 --- a/lib/filesize.es6.min.js.map +++ b/lib/filesize.es6.min.js.map @@ -1 +1 @@ -{"version":3,"file":"filesize.es6.min.js","sources":["../src/filesize.js"],"sourcesContent":["const b = /^(b|B)$/,\n\tsymbol = {\n\t\tiec: {\n\t\t\tbits: [\"b\", \"Kib\", \"Mib\", \"Gib\", \"Tib\", \"Pib\", \"Eib\", \"Zib\", \"Yib\"],\n\t\t\tbytes: [\"B\", \"KiB\", \"MiB\", \"GiB\", \"TiB\", \"PiB\", \"EiB\", \"ZiB\", \"YiB\"]\n\t\t},\n\t\tjedec: {\n\t\t\tbits: [\"b\", \"Kb\", \"Mb\", \"Gb\", \"Tb\", \"Pb\", \"Eb\", \"Zb\", \"Yb\"],\n\t\t\tbytes: [\"B\", \"KB\", \"MB\", \"GB\", \"TB\", \"PB\", \"EB\", \"ZB\", \"YB\"]\n\t\t}\n\t},\n\tfullform = {\n\t\tiec: [\"\", \"kibi\", \"mebi\", \"gibi\", \"tebi\", \"pebi\", \"exbi\", \"zebi\", \"yobi\"],\n\t\tjedec: [\"\", \"kilo\", \"mega\", \"giga\", \"tera\", \"peta\", \"exa\", \"zetta\", \"yotta\"]\n\t},\n\troundingFuncs = {\n\t\tfloor: Math.floor,\n\t\tceil: Math.ceil\n\t};\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\tlet result = [],\n\t\tval = 0,\n\t\te, base, bits, ceil, full, fullforms, locale, localeOptions, neg, num, output, pad, round, u, unix, separator, spacer, standard, symbols, roundingFunc;\n\n\tif (isNaN(arg)) {\n\t\tthrow new TypeError(\"Invalid number\");\n\t}\n\n\tbits = descriptor.bits === true;\n\tunix = descriptor.unix === true;\n\tpad = descriptor.pad === true;\n\tbase = descriptor.base || 2;\n\tround = descriptor.round !== void 0 ? descriptor.round : unix ? 1 : 2;\n\tlocale = descriptor.locale !== void 0 ? descriptor.locale : \"\";\n\tlocaleOptions = descriptor.localeOptions || {};\n\tseparator = descriptor.separator !== void 0 ? descriptor.separator : \"\";\n\tspacer = descriptor.spacer !== void 0 ? descriptor.spacer : unix ? \"\" : \" \";\n\tsymbols = descriptor.symbols || {};\n\tstandard = base === 2 ? descriptor.standard || \"jedec\" : \"jedec\";\n\toutput = descriptor.output || \"string\";\n\tfull = descriptor.fullform === true;\n\tfullforms = descriptor.fullforms instanceof Array ? descriptor.fullforms : [];\n\te = descriptor.exponent !== void 0 ? descriptor.exponent : -1;\n\troundingFunc = roundingFuncs[descriptor.roundingMethod] || Math.round;\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// Determining the exponent\n\tif (e === -1 || isNaN(e)) {\n\t\te = Math.floor(Math.log(num) / Math.log(ceil));\n\n\t\tif (e < 0) {\n\t\t\te = 0;\n\t\t}\n\t}\n\n\t// Exceeding supported length, time to reduce & multiply\n\tif (e > 8) {\n\t\te = 8;\n\t}\n\n\tif (output === \"exponent\") {\n\t\treturn e;\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\t\tu = result[1] = unix ? \"\" : symbol[standard][bits ? \"bits\" : \"bytes\"][e];\n\t} else {\n\t\tval = num / (base === 2 ? Math.pow(2, e * 10) : Math.pow(1000, e));\n\n\t\tif (bits) {\n\t\t\tval = val * 8;\n\n\t\t\tif (val >= ceil && e < 8) {\n\t\t\t\tval = val / ceil;\n\t\t\t\te++;\n\t\t\t}\n\t\t}\n\n\t\tconst p = Math.pow(10, e > 0 ? round : 0);\n\t\tresult[0] = roundingFunc(val * p) / p;\n\n\t\tif (result[0] === ceil && e < 8 && descriptor.exponent === void 0) {\n\t\t\tresult[0] = 1;\n\t\t\te++;\n\t\t}\n\n\t\tu = result[1] = base === 10 && e === 1 ? bits ? \"kb\" : \"kB\" : symbol[standard][bits ? \"bits\" : \"bytes\"][e];\n\n\t\tif (unix) {\n\t\t\tresult[1] = standard === \"jedec\" ? result[1].charAt(0) : e > 0 ? result[1].replace(/B$/, \"\") : result[1];\n\n\t\t\tif (b.test(result[1])) {\n\t\t\t\tresult[0] = Math.floor(result[0]);\n\t\t\t\tresult[1] = \"\";\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 symbol\n\tresult[1] = symbols[result[1]] || result[1];\n\n\tif (locale === true) {\n\t\tresult[0] = result[0].toLocaleString();\n\t} else if (locale.length > 0) {\n\t\tresult[0] = result[0].toLocaleString(locale, localeOptions);\n\t} else if (separator.length > 0) {\n\t\tresult[0] = result[0].toString().replace(\".\", separator);\n\t}\n\n\tif (pad && Number.isInteger(result[0]) === false && round > 0) {\n\t\tconst x = separator || \".\",\n\t\t\ttmp = result[0].toString().split(x),\n\t\t\ts = tmp[1] || \"\",\n\t\t\tl = s.length,\n\t\t\tn = round - l;\n\n\t\tresult[0] = `${tmp[0]}${x}${s.padEnd(l + n, \"0\")}`;\n\t}\n\n\tif (full) {\n\t\tresult[1] = fullforms[e] ? fullforms[e] : fullform[standard][e] + (bits ? \"bit\" : \"byte\") + (result[0] === 1 ? \"\" : \"s\");\n\t}\n\n\t// Returning Array, Object, or String (default)\n\treturn output === \"array\" ? result : output === \"object\" ? {value: result[0], symbol: result[1], exponent: e, unit: u} : result.join(spacer);\n}\n\n// Partial application for functional programming\nfilesize.partial = opt => arg => filesize(arg, opt);\n\nexport default filesize;\n"],"names":["b","symbol","iec","bits","bytes","jedec","fullform","roundingFuncs","floor","Math","ceil","filesize","arg","descriptor","e","base","full","fullforms","locale","localeOptions","neg","num","output","pad","round","u","unix","separator","spacer","standard","symbols","roundingFunc","result","val","isNaN","TypeError","Array","exponent","roundingMethod","Number","log","pow","p","charAt","replace","test","toLocaleString","length","toString","isInteger","x","tmp","split","s","l","n","padEnd","value","unit","join","partial","opt"],"mappings":";;;;yOAAA,MAAMA,EAA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GAElGhC,EAAE6C,KAAKb,EAAO,MACjBA,EAAO,GAAKvB,KAAKD,MAAMwB,EAAO,IAC9BA,EAAO,GAAK,KAqBf,GAfIZ,IACHY,EAAO,IAAMA,EAAO,IAIrBA,EAAO,GAAKF,EAAQE,EAAO,KAAOA,EAAO,IAE1B,IAAXd,EACHc,EAAO,GAAKA,EAAO,GAAGc,iBACZ5B,EAAO6B,OAAS,EAC1Bf,EAAO,GAAKA,EAAO,GAAGc,eAAe5B,EAAQC,GACnCQ,EAAUoB,OAAS,IAC7Bf,EAAO,GAAKA,EAAO,GAAGgB,WAAWJ,QAAQ,IAAKjB,IAG3CJ,IAAuC,IAAhCgB,OAAOU,UAAUjB,EAAO,KAAiBR,EAAQ,EAAG,CAC9D,MAAM0B,EAAIvB,GAAa,IACtBwB,EAAMnB,EAAO,GAAGgB,WAAWI,MAAMF,GACjCG,EAAIF,EAAI,IAAM,GACdG,EAAID,EAAEN,OACNQ,EAAI/B,EAAQ8B,EAEbtB,EAAO,GAAK,GAAGmB,EAAI,KAAKD,IAAIG,EAAEG,OAAOF,EAAIC,EAAG,OAQ7C,OALIvC,IACHgB,EAAO,GAAKf,EAAUH,GAAKG,EAAUH,GAAKR,EAASuB,GAAUf,IAAMX,EAAO,MAAQ,SAAyB,IAAd6B,EAAO,GAAW,GAAK,MAInG,UAAXV,EAAqBU,EAAoB,WAAXV,EAAsB,CAACmC,MAAOzB,EAAO,GAAI/B,OAAQ+B,EAAO,GAAIK,SAAUvB,EAAG4C,KAAMjC,GAAKO,EAAO2B,KAAK/B,UAItIjB,EAASiD,QAAUC,GAAOjD,GAAOD,EAASC,EAAKiD"} \ No newline at end of file +{"version":3,"file":"filesize.es6.min.js","sources":["../src/filesize.js"],"sourcesContent":["const b = /^(b|B)$/,\n\tsymbol = {\n\t\tiec: {\n\t\t\tbits: [\"b\", \"Kib\", \"Mib\", \"Gib\", \"Tib\", \"Pib\", \"Eib\", \"Zib\", \"Yib\"],\n\t\t\tbytes: [\"B\", \"KiB\", \"MiB\", \"GiB\", \"TiB\", \"PiB\", \"EiB\", \"ZiB\", \"YiB\"]\n\t\t},\n\t\tjedec: {\n\t\t\tbits: [\"b\", \"Kb\", \"Mb\", \"Gb\", \"Tb\", \"Pb\", \"Eb\", \"Zb\", \"Yb\"],\n\t\t\tbytes: [\"B\", \"KB\", \"MB\", \"GB\", \"TB\", \"PB\", \"EB\", \"ZB\", \"YB\"]\n\t\t}\n\t},\n\tfullform = {\n\t\tiec: [\"\", \"kibi\", \"mebi\", \"gibi\", \"tebi\", \"pebi\", \"exbi\", \"zebi\", \"yobi\"],\n\t\tjedec: [\"\", \"kilo\", \"mega\", \"giga\", \"tera\", \"peta\", \"exa\", \"zetta\", \"yotta\"]\n\t},\n\troundingFuncs = {\n\t\tfloor: Math.floor,\n\t\tceil: Math.ceil\n\t};\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\tlet result = [],\n\t\tval = 0,\n\t\te, base, bits, ceil, full, fullforms, locale, localeOptions, neg, num, output, pad, round, u, unix, separator, spacer, standard, symbols, roundingFunc, precision;\n\n\tif (isNaN(arg)) {\n\t\tthrow new TypeError(\"Invalid number\");\n\t}\n\n\tbits = descriptor.bits === true;\n\tunix = descriptor.unix === true;\n\tpad = descriptor.pad === true;\n\tbase = descriptor.base || 2;\n\tround = descriptor.round !== void 0 ? descriptor.round : unix ? 1 : 2;\n\tlocale = descriptor.locale !== void 0 ? descriptor.locale : \"\";\n\tlocaleOptions = descriptor.localeOptions || {};\n\tseparator = descriptor.separator !== void 0 ? descriptor.separator : \"\";\n\tspacer = descriptor.spacer !== void 0 ? descriptor.spacer : unix ? \"\" : \" \";\n\tsymbols = descriptor.symbols || {};\n\tstandard = base === 2 ? descriptor.standard || \"jedec\" : \"jedec\";\n\toutput = descriptor.output || \"string\";\n\tfull = descriptor.fullform === true;\n\tfullforms = descriptor.fullforms instanceof Array ? descriptor.fullforms : [];\n\te = descriptor.exponent !== void 0 ? descriptor.exponent : -1;\n\troundingFunc = roundingFuncs[descriptor.roundingMethod] || Math.round;\n\tnum = Number(arg);\n\tneg = num < 0;\n\tceil = base > 2 ? 1000 : 1024;\n\tprecision = isNaN(descriptor.precision) === false ? parseInt(descriptor.precision, 10) : 0;\n\n\t// Flipping a negative number to determine the size\n\tif (neg) {\n\t\tnum = -num;\n\t}\n\n\t// Determining the exponent\n\tif (e === -1 || isNaN(e)) {\n\t\te = Math.floor(Math.log(num) / Math.log(ceil));\n\n\t\tif (e < 0) {\n\t\t\te = 0;\n\t\t}\n\t}\n\n\t// Exceeding supported length, time to reduce & multiply\n\tif (e > 8) {\n\t\tif (precision > 0) {\n\t\t\tprecision += 8 - e;\n\t\t}\n\n\t\te = 8;\n\t}\n\n\tif (output === \"exponent\") {\n\t\treturn e;\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\t\tu = result[1] = unix ? \"\" : symbol[standard][bits ? \"bits\" : \"bytes\"][e];\n\t} else {\n\t\tval = num / (base === 2 ? Math.pow(2, e * 10) : Math.pow(1000, e));\n\n\t\tif (bits) {\n\t\t\tval = val * 8;\n\n\t\t\tif (val >= ceil && e < 8) {\n\t\t\t\tval = val / ceil;\n\t\t\t\te++;\n\t\t\t}\n\t\t}\n\n\t\tconst p = Math.pow(10, e > 0 ? round : 0);\n\t\tresult[0] = roundingFunc(val * p) / p;\n\n\t\tif (result[0] === ceil && e < 8 && descriptor.exponent === void 0) {\n\t\t\tresult[0] = 1;\n\t\t\te++;\n\t\t}\n\n\t\tu = result[1] = base === 10 && e === 1 ? bits ? \"kb\" : \"kB\" : symbol[standard][bits ? \"bits\" : \"bytes\"][e];\n\n\t\tif (unix) {\n\t\t\tresult[1] = standard === \"jedec\" ? result[1].charAt(0) : e > 0 ? result[1].replace(/B$/, \"\") : result[1];\n\n\t\t\tif (b.test(result[1])) {\n\t\t\t\tresult[0] = Math.floor(result[0]);\n\t\t\t\tresult[1] = \"\";\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// Setting optional precision\n\tif (precision > 0) {\n\t\tresult[0] = result[0].toPrecision(precision);\n\t}\n\n\t// Applying custom symbol\n\tresult[1] = symbols[result[1]] || result[1];\n\n\tif (locale === true) {\n\t\tresult[0] = result[0].toLocaleString();\n\t} else if (locale.length > 0) {\n\t\tresult[0] = result[0].toLocaleString(locale, localeOptions);\n\t} else if (separator.length > 0) {\n\t\tresult[0] = result[0].toString().replace(\".\", separator);\n\t}\n\n\tif (pad && Number.isInteger(result[0]) === false && round > 0) {\n\t\tconst x = separator || \".\",\n\t\t\ttmp = result[0].toString().split(x),\n\t\t\ts = tmp[1] || \"\",\n\t\t\tl = s.length,\n\t\t\tn = round - l;\n\n\t\tresult[0] = `${tmp[0]}${x}${s.padEnd(l + n, \"0\")}`;\n\t}\n\n\tif (full) {\n\t\tresult[1] = fullforms[e] ? fullforms[e] : fullform[standard][e] + (bits ? \"bit\" : \"byte\") + (result[0] === 1 ? \"\" : \"s\");\n\t}\n\n\t// Returning Array, Object, or String (default)\n\treturn output === \"array\" ? result : output === \"object\" ? {value: result[0], symbol: result[1], exponent: e, unit: u} : result.join(spacer);\n}\n\n// Partial application for functional programming\nfilesize.partial = opt => arg => filesize(arg, opt);\n\nexport default filesize;\n"],"names":["b","symbol","iec","bits","bytes","jedec","fullform","roundingFuncs","floor","Math","ceil","filesize","arg","descriptor","e","base","full","fullforms","locale","localeOptions","neg","num","output","pad","round","u","unix","separator","spacer","standard","symbols","roundingFunc","precision","result","val","isNaN","TypeError","Array","exponent","roundingMethod","Number","parseInt","log","pow","p","charAt","replace","test","toPrecision","toLocaleString","length","toString","isInteger","x","tmp","split","s","l","n","padEnd","value","unit","join","partial","opt"],"mappings":";;;;yOAAA,MAAMA,EAAI,UACTC,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..70912a1 100644 --- a/lib/filesize.js +++ b/lib/filesize.js @@ -62,7 +62,8 @@ spacer, standard, symbols, - roundingFunc; + roundingFunc, + precision; if (isNaN(arg)) { throw new TypeError("Invalid number"); @@ -86,7 +87,8 @@ roundingFunc = roundingFuncs[descriptor.roundingMethod] || Math.round; num = Number(arg); neg = num < 0; - ceil = base > 2 ? 1000 : 1024; // Flipping a negative number to determine the size + ceil = base > 2 ? 1000 : 1024; + precision = isNaN(descriptor.precision) === false ? parseInt(descriptor.precision, 10) : 0; // Flipping a negative number to determine the size if (neg) { num = -num; @@ -103,6 +105,10 @@ if (e > 8) { + if (precision > 0) { + precision += 8 - e; + } + e = 8; } @@ -149,6 +155,11 @@ if (neg) { result[0] = -result[0]; + } // Setting optional precision + + + if (precision > 0) { + result[0] = result[0].toPrecision(precision); } // Applying custom symbol diff --git a/lib/filesize.min.js b/lib/filesize.min.js index ac90cca..b3ea873 100644 --- a/lib/filesize.min.js +++ b/lib/filesize.min.js @@ -2,5 +2,5 @@ 2020 Jason Mulligan @version 6.3.0 */ -!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(e="undefined"!=typeof globalThis?globalThis:e||self).filesize=t()}(this,(function(){"use strict";var e=/^(b|B)$/,t={iec:{bits:["b","Kib","Mib","Gib","Tib","Pib","Eib","Zib","Yib"],bytes:["B","KiB","MiB","GiB","TiB","PiB","EiB","ZiB","YiB"]},jedec:{bits:["b","Kb","Mb","Gb","Tb","Pb","Eb","Zb","Yb"],bytes:["B","KB","MB","GB","TB","PB","EB","ZB","YB"]}},i={iec:["","kibi","mebi","gibi","tebi","pebi","exbi","zebi","yobi"],jedec:["","kilo","mega","giga","tera","peta","exa","zetta","yotta"]},o={floor:Math.floor,ceil:Math.ceil};function n(n){var r,a,b,l,s,c,d,f,u,p,B,h,g,y,M,m,v,x,j,T,E=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},N=[],w=0;if(isNaN(n))throw new TypeError("Invalid number");if(b=!0===E.bits,M=!0===E.unix,h=!0===E.pad,a=E.base||2,g=void 0!==E.round?E.round:M?1:2,d=void 0!==E.locale?E.locale:"",f=E.localeOptions||{},m=void 0!==E.separator?E.separator:"",v=void 0!==E.spacer?E.spacer:M?"":" ",j=E.symbols||{},x=2===a&&E.standard||"jedec",B=E.output||"string",s=!0===E.fullform,c=E.fullforms instanceof Array?E.fullforms:[],r=void 0!==E.exponent?E.exponent:-1,T=o[E.roundingMethod]||Math.round,l=a>2?1e3:1024,(u=(p=Number(n))<0)&&(p=-p),(-1===r||isNaN(r))&&(r=Math.floor(Math.log(p)/Math.log(l)))<0&&(r=0),r>8&&(r=8),"exponent"===B)return r;if(0===p)N[0]=0,y=N[1]=M?"":t[x][b?"bits":"bytes"][r];else{w=p/(2===a?Math.pow(2,10*r):Math.pow(1e3,r)),b&&(w*=8)>=l&&r<8&&(w/=l,r++);var k=Math.pow(10,r>0?g:0);N[0]=T(w*k)/k,N[0]===l&&r<8&&void 0===E.exponent&&(N[0]=1,r++),y=N[1]=10===a&&1===r?b?"kb":"kB":t[x][b?"bits":"bytes"][r],M&&(N[1]="jedec"===x?N[1].charAt(0):r>0?N[1].replace(/B$/,""):N[1],e.test(N[1])&&(N[0]=Math.floor(N[0]),N[1]=""))}if(u&&(N[0]=-N[0]),N[1]=j[N[1]]||N[1],!0===d?N[0]=N[0].toLocaleString():d.length>0?N[0]=N[0].toLocaleString(d,f):m.length>0&&(N[0]=N[0].toString().replace(".",m)),h&&!1===Number.isInteger(N[0])&&g>0){var G=m||".",K=N[0].toString().split(G),P=K[1]||"",S=P.length,Y=g-S;N[0]="".concat(K[0]).concat(G).concat(P.padEnd(S+Y,"0"))}return s&&(N[1]=c[r]?c[r]:i[x][r]+(b?"bit":"byte")+(1===N[0]?"":"s")),"array"===B?N:"object"===B?{value:N[0],symbol:N[1],exponent:r,unit:y}:N.join(v)}return n.partial=function(e){return function(t){return n(t,e)}},n})); +!function(e,i){"object"==typeof exports&&"undefined"!=typeof module?module.exports=i():"function"==typeof define&&define.amd?define(i):(e="undefined"!=typeof globalThis?globalThis:e||self).filesize=i()}(this,(function(){"use strict";var e=/^(b|B)$/,i={iec:{bits:["b","Kib","Mib","Gib","Tib","Pib","Eib","Zib","Yib"],bytes:["B","KiB","MiB","GiB","TiB","PiB","EiB","ZiB","YiB"]},jedec:{bits:["b","Kb","Mb","Gb","Tb","Pb","Eb","Zb","Yb"],bytes:["B","KB","MB","GB","TB","PB","EB","ZB","YB"]}},t={iec:["","kibi","mebi","gibi","tebi","pebi","exbi","zebi","yobi"],jedec:["","kilo","mega","giga","tera","peta","exa","zetta","yotta"]},o={floor:Math.floor,ceil:Math.ceil};function n(n){var r,a,b,l,s,c,d,f,p,u,B,h,g,y,M,m,v,x,N,j,T,E=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},w=[],P=0;if(isNaN(n))throw new TypeError("Invalid number");if(b=!0===E.bits,M=!0===E.unix,h=!0===E.pad,a=E.base||2,g=void 0!==E.round?E.round:M?1:2,d=void 0!==E.locale?E.locale:"",f=E.localeOptions||{},m=void 0!==E.separator?E.separator:"",v=void 0!==E.spacer?E.spacer:M?"":" ",N=E.symbols||{},x=2===a&&E.standard||"jedec",B=E.output||"string",s=!0===E.fullform,c=E.fullforms instanceof Array?E.fullforms:[],r=void 0!==E.exponent?E.exponent:-1,j=o[E.roundingMethod]||Math.round,p=(u=Number(n))<0,l=a>2?1e3:1024,T=!1===isNaN(E.precision)?parseInt(E.precision,10):0,p&&(u=-u),(-1===r||isNaN(r))&&(r=Math.floor(Math.log(u)/Math.log(l)))<0&&(r=0),r>8&&(T>0&&(T+=8-r),r=8),"exponent"===B)return r;if(0===u)w[0]=0,y=w[1]=M?"":i[x][b?"bits":"bytes"][r];else{P=u/(2===a?Math.pow(2,10*r):Math.pow(1e3,r)),b&&(P*=8)>=l&&r<8&&(P/=l,r++);var k=Math.pow(10,r>0?g:0);w[0]=j(P*k)/k,w[0]===l&&r<8&&void 0===E.exponent&&(w[0]=1,r++),y=w[1]=10===a&&1===r?b?"kb":"kB":i[x][b?"bits":"bytes"][r],M&&(w[1]="jedec"===x?w[1].charAt(0):r>0?w[1].replace(/B$/,""):w[1],e.test(w[1])&&(w[0]=Math.floor(w[0]),w[1]=""))}if(p&&(w[0]=-w[0]),T>0&&(w[0]=w[0].toPrecision(T)),w[1]=N[w[1]]||w[1],!0===d?w[0]=w[0].toLocaleString():d.length>0?w[0]=w[0].toLocaleString(d,f):m.length>0&&(w[0]=w[0].toString().replace(".",m)),h&&!1===Number.isInteger(w[0])&&g>0){var G=m||".",K=w[0].toString().split(G),S=K[1]||"",Y=S.length,Z=g-Y;w[0]="".concat(K[0]).concat(G).concat(S.padEnd(Y+Z,"0"))}return s&&(w[1]=c[r]?c[r]:t[x][r]+(b?"bit":"byte")+(1===w[0]?"":"s")),"array"===B?w:"object"===B?{value:w[0],symbol:w[1],exponent:r,unit:y}:w.join(v)}return n.partial=function(e){return function(i){return n(i,e)}},n})); //# sourceMappingURL=filesize.min.js.map diff --git a/lib/filesize.min.js.map b/lib/filesize.min.js.map index deec4ec..c77bfca 100644 --- a/lib/filesize.min.js.map +++ b/lib/filesize.min.js.map @@ -1 +1 @@ -{"version":3,"file":"filesize.min.js","sources":["../src/filesize.js"],"sourcesContent":["const b = /^(b|B)$/,\n\tsymbol = {\n\t\tiec: {\n\t\t\tbits: [\"b\", \"Kib\", \"Mib\", \"Gib\", \"Tib\", \"Pib\", \"Eib\", \"Zib\", \"Yib\"],\n\t\t\tbytes: [\"B\", \"KiB\", \"MiB\", \"GiB\", \"TiB\", \"PiB\", \"EiB\", \"ZiB\", \"YiB\"]\n\t\t},\n\t\tjedec: {\n\t\t\tbits: [\"b\", \"Kb\", \"Mb\", \"Gb\", \"Tb\", \"Pb\", \"Eb\", \"Zb\", \"Yb\"],\n\t\t\tbytes: [\"B\", \"KB\", \"MB\", \"GB\", \"TB\", \"PB\", \"EB\", \"ZB\", \"YB\"]\n\t\t}\n\t},\n\tfullform = {\n\t\tiec: [\"\", \"kibi\", \"mebi\", \"gibi\", \"tebi\", \"pebi\", \"exbi\", \"zebi\", \"yobi\"],\n\t\tjedec: [\"\", \"kilo\", \"mega\", \"giga\", \"tera\", \"peta\", \"exa\", \"zetta\", \"yotta\"]\n\t},\n\troundingFuncs = {\n\t\tfloor: Math.floor,\n\t\tceil: Math.ceil\n\t};\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\tlet result = [],\n\t\tval = 0,\n\t\te, base, bits, ceil, full, fullforms, locale, localeOptions, neg, num, output, pad, round, u, unix, separator, spacer, standard, symbols, roundingFunc;\n\n\tif (isNaN(arg)) {\n\t\tthrow new TypeError(\"Invalid number\");\n\t}\n\n\tbits = descriptor.bits === true;\n\tunix = descriptor.unix === true;\n\tpad = descriptor.pad === true;\n\tbase = descriptor.base || 2;\n\tround = descriptor.round !== void 0 ? descriptor.round : unix ? 1 : 2;\n\tlocale = descriptor.locale !== void 0 ? descriptor.locale : \"\";\n\tlocaleOptions = descriptor.localeOptions || {};\n\tseparator = descriptor.separator !== void 0 ? descriptor.separator : \"\";\n\tspacer = descriptor.spacer !== void 0 ? descriptor.spacer : unix ? \"\" : \" \";\n\tsymbols = descriptor.symbols || {};\n\tstandard = base === 2 ? descriptor.standard || \"jedec\" : \"jedec\";\n\toutput = descriptor.output || \"string\";\n\tfull = descriptor.fullform === true;\n\tfullforms = descriptor.fullforms instanceof Array ? descriptor.fullforms : [];\n\te = descriptor.exponent !== void 0 ? descriptor.exponent : -1;\n\troundingFunc = roundingFuncs[descriptor.roundingMethod] || Math.round;\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// Determining the exponent\n\tif (e === -1 || isNaN(e)) {\n\t\te = Math.floor(Math.log(num) / Math.log(ceil));\n\n\t\tif (e < 0) {\n\t\t\te = 0;\n\t\t}\n\t}\n\n\t// Exceeding supported length, time to reduce & multiply\n\tif (e > 8) {\n\t\te = 8;\n\t}\n\n\tif (output === \"exponent\") {\n\t\treturn e;\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\t\tu = result[1] = unix ? \"\" : symbol[standard][bits ? \"bits\" : \"bytes\"][e];\n\t} else {\n\t\tval = num / (base === 2 ? Math.pow(2, e * 10) : Math.pow(1000, e));\n\n\t\tif (bits) {\n\t\t\tval = val * 8;\n\n\t\t\tif (val >= ceil && e < 8) {\n\t\t\t\tval = val / ceil;\n\t\t\t\te++;\n\t\t\t}\n\t\t}\n\n\t\tconst p = Math.pow(10, e > 0 ? round : 0);\n\t\tresult[0] = roundingFunc(val * p) / p;\n\n\t\tif (result[0] === ceil && e < 8 && descriptor.exponent === void 0) {\n\t\t\tresult[0] = 1;\n\t\t\te++;\n\t\t}\n\n\t\tu = result[1] = base === 10 && e === 1 ? bits ? \"kb\" : \"kB\" : symbol[standard][bits ? \"bits\" : \"bytes\"][e];\n\n\t\tif (unix) {\n\t\t\tresult[1] = standard === \"jedec\" ? result[1].charAt(0) : e > 0 ? result[1].replace(/B$/, \"\") : result[1];\n\n\t\t\tif (b.test(result[1])) {\n\t\t\t\tresult[0] = Math.floor(result[0]);\n\t\t\t\tresult[1] = \"\";\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 symbol\n\tresult[1] = symbols[result[1]] || result[1];\n\n\tif (locale === true) {\n\t\tresult[0] = result[0].toLocaleString();\n\t} else if (locale.length > 0) {\n\t\tresult[0] = result[0].toLocaleString(locale, localeOptions);\n\t} else if (separator.length > 0) {\n\t\tresult[0] = result[0].toString().replace(\".\", separator);\n\t}\n\n\tif (pad && Number.isInteger(result[0]) === false && round > 0) {\n\t\tconst x = separator || \".\",\n\t\t\ttmp = result[0].toString().split(x),\n\t\t\ts = tmp[1] || \"\",\n\t\t\tl = s.length,\n\t\t\tn = round - l;\n\n\t\tresult[0] = `${tmp[0]}${x}${s.padEnd(l + n, \"0\")}`;\n\t}\n\n\tif (full) {\n\t\tresult[1] = fullforms[e] ? fullforms[e] : fullform[standard][e] + (bits ? \"bit\" : \"byte\") + (result[0] === 1 ? \"\" : \"s\");\n\t}\n\n\t// Returning Array, Object, or String (default)\n\treturn output === \"array\" ? result : output === \"object\" ? {value: result[0], symbol: result[1], exponent: e, unit: u} : result.join(spacer);\n}\n\n// Partial application for functional programming\nfilesize.partial = opt => arg => filesize(arg, opt);\n\nexport default filesize;\n"],"names":["b","symbol","iec","bits","bytes","jedec","fullform","roundingFuncs","floor","Math","ceil","filesize","arg","e","base","full","fullforms","locale","localeOptions","neg","num","output","pad","round","u","unix","separator","spacer","standard","symbols","roundingFunc","descriptor","result","val","isNaN","TypeError","Array","exponent","roundingMethod","Number","log","pow","p","charAt","replace","test","toLocaleString","length","toString","isInteger","x","tmp","split","s","l","n","padEnd","value","unit","join","partial","opt"],"mappings":";;;;yOAAA,IAAMA,EAAI,UACTC,EAAS,CACRC,IAAK,CACJC,KAAM,CAAC,IAAK,MAAO,MAAO,MAAO,MAAO,MAAO,MAAO,MAAO,OAC7DC,MAAO,CAAC,IAAK,MAAO,MAAO,MAAO,MAAO,MAAO,MAAO,MAAO,QAE/DC,MAAO,CACNF,KAAM,CAAC,IAAK,KAAM,KAAM,KAAM,KAAM,KAAM,KAAM,KAAM,MACtDC,MAAO,CAAC,IAAK,KAAM,KAAM,KAAM,KAAM,KAAM,KAAM,KAAM,QAGzDE,EAAW,CACVJ,IAAK,CAAC,GAAI,OAAQ,OAAQ,OAAQ,OAAQ,OAAQ,OAAQ,OAAQ,QAClEG,MAAO,CAAC,GAAI,OAAQ,OAAQ,OAAQ,OAAQ,OAAQ,MAAO,QAAS,UAErEE,E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GACdG,EAAID,EAAEN,OACNQ,EAAIhC,EAAQ+B,EAEbtB,EAAO,aAAQmB,EAAI,WAAKD,UAAIG,EAAEG,OAAOF,EAAIC,EAAG,aAGzCxC,IACHiB,EAAO,GAAKhB,EAAUH,GAAKG,EAAUH,GAAKP,EAASsB,GAAUf,IAAMV,EAAO,MAAQ,SAAyB,IAAd6B,EAAO,GAAW,GAAK,MAInG,UAAXX,EAAqBW,EAAoB,WAAXX,EAAsB,CAACoC,MAAOzB,EAAO,GAAI/B,OAAQ+B,EAAO,GAAIK,SAAUxB,EAAG6C,KAAMlC,GAAKQ,EAAO2B,KAAKhC,UAItIhB,EAASiD,QAAU,SAAAC,UAAO,SAAAjD,UAAOD,EAASC,EAAKiD"} \ No newline at end of file +{"version":3,"file":"filesize.min.js","sources":["../src/filesize.js"],"sourcesContent":["const b = /^(b|B)$/,\n\tsymbol = {\n\t\tiec: {\n\t\t\tbits: [\"b\", \"Kib\", \"Mib\", \"Gib\", \"Tib\", \"Pib\", \"Eib\", \"Zib\", \"Yib\"],\n\t\t\tbytes: [\"B\", \"KiB\", \"MiB\", \"GiB\", \"TiB\", \"PiB\", \"EiB\", \"ZiB\", \"YiB\"]\n\t\t},\n\t\tjedec: {\n\t\t\tbits: [\"b\", \"Kb\", \"Mb\", \"Gb\", \"Tb\", \"Pb\", \"Eb\", \"Zb\", \"Yb\"],\n\t\t\tbytes: [\"B\", \"KB\", \"MB\", \"GB\", \"TB\", \"PB\", \"EB\", \"ZB\", \"YB\"]\n\t\t}\n\t},\n\tfullform = {\n\t\tiec: [\"\", \"kibi\", \"mebi\", \"gibi\", \"tebi\", \"pebi\", \"exbi\", \"zebi\", \"yobi\"],\n\t\tjedec: [\"\", \"kilo\", \"mega\", \"giga\", \"tera\", \"peta\", \"exa\", \"zetta\", \"yotta\"]\n\t},\n\troundingFuncs = {\n\t\tfloor: Math.floor,\n\t\tceil: Math.ceil\n\t};\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\tlet result = [],\n\t\tval = 0,\n\t\te, base, bits, ceil, full, fullforms, locale, localeOptions, neg, num, output, pad, round, u, unix, separator, spacer, standard, symbols, roundingFunc, precision;\n\n\tif (isNaN(arg)) {\n\t\tthrow new TypeError(\"Invalid number\");\n\t}\n\n\tbits = descriptor.bits === true;\n\tunix = descriptor.unix === true;\n\tpad = descriptor.pad === true;\n\tbase = descriptor.base || 2;\n\tround = descriptor.round !== void 0 ? descriptor.round : unix ? 1 : 2;\n\tlocale = descriptor.locale !== void 0 ? descriptor.locale : \"\";\n\tlocaleOptions = descriptor.localeOptions || {};\n\tseparator = descriptor.separator !== void 0 ? descriptor.separator : \"\";\n\tspacer = descriptor.spacer !== void 0 ? descriptor.spacer : unix ? \"\" : \" \";\n\tsymbols = descriptor.symbols || {};\n\tstandard = base === 2 ? descriptor.standard || \"jedec\" : \"jedec\";\n\toutput = descriptor.output || \"string\";\n\tfull = descriptor.fullform === true;\n\tfullforms = descriptor.fullforms instanceof Array ? descriptor.fullforms : [];\n\te = descriptor.exponent !== void 0 ? descriptor.exponent : -1;\n\troundingFunc = roundingFuncs[descriptor.roundingMethod] || Math.round;\n\tnum = Number(arg);\n\tneg = num < 0;\n\tceil = base > 2 ? 1000 : 1024;\n\tprecision = isNaN(descriptor.precision) === false ? parseInt(descriptor.precision, 10) : 0;\n\n\t// Flipping a negative number to determine the size\n\tif (neg) {\n\t\tnum = -num;\n\t}\n\n\t// Determining the exponent\n\tif (e === -1 || isNaN(e)) {\n\t\te = Math.floor(Math.log(num) / Math.log(ceil));\n\n\t\tif (e < 0) {\n\t\t\te = 0;\n\t\t}\n\t}\n\n\t// Exceeding supported length, time to reduce & multiply\n\tif (e > 8) {\n\t\tif (precision > 0) {\n\t\t\tprecision += 8 - e;\n\t\t}\n\n\t\te = 8;\n\t}\n\n\tif (output === \"exponent\") {\n\t\treturn e;\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\t\tu = result[1] = unix ? \"\" : symbol[standard][bits ? \"bits\" : \"bytes\"][e];\n\t} else {\n\t\tval = num / (base === 2 ? Math.pow(2, e * 10) : Math.pow(1000, e));\n\n\t\tif (bits) {\n\t\t\tval = val * 8;\n\n\t\t\tif (val >= ceil && e < 8) {\n\t\t\t\tval = val / ceil;\n\t\t\t\te++;\n\t\t\t}\n\t\t}\n\n\t\tconst p = Math.pow(10, e > 0 ? round : 0);\n\t\tresult[0] = roundingFunc(val * p) / p;\n\n\t\tif (result[0] === ceil && e < 8 && descriptor.exponent === void 0) {\n\t\t\tresult[0] = 1;\n\t\t\te++;\n\t\t}\n\n\t\tu = result[1] = base === 10 && e === 1 ? bits ? \"kb\" : \"kB\" : symbol[standard][bits ? \"bits\" : \"bytes\"][e];\n\n\t\tif (unix) {\n\t\t\tresult[1] = standard === \"jedec\" ? result[1].charAt(0) : e > 0 ? result[1].replace(/B$/, \"\") : result[1];\n\n\t\t\tif (b.test(result[1])) {\n\t\t\t\tresult[0] = Math.floor(result[0]);\n\t\t\t\tresult[1] = \"\";\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// Setting optional precision\n\tif (precision > 0) {\n\t\tresult[0] = result[0].toPrecision(precision);\n\t}\n\n\t// Applying custom symbol\n\tresult[1] = symbols[result[1]] || result[1];\n\n\tif (locale === true) {\n\t\tresult[0] = result[0].toLocaleString();\n\t} else if (locale.length > 0) {\n\t\tresult[0] = result[0].toLocaleString(locale, localeOptions);\n\t} else if (separator.length > 0) {\n\t\tresult[0] = result[0].toString().replace(\".\", separator);\n\t}\n\n\tif (pad && Number.isInteger(result[0]) === false && round > 0) {\n\t\tconst x = separator || \".\",\n\t\t\ttmp = result[0].toString().split(x),\n\t\t\ts = tmp[1] || \"\",\n\t\t\tl = s.length,\n\t\t\tn = round - l;\n\n\t\tresult[0] = `${tmp[0]}${x}${s.padEnd(l + n, \"0\")}`;\n\t}\n\n\tif (full) {\n\t\tresult[1] = fullforms[e] ? fullforms[e] : fullform[standard][e] + (bits ? \"bit\" : \"byte\") + (result[0] === 1 ? \"\" : \"s\");\n\t}\n\n\t// Returning Array, Object, or String (default)\n\treturn output === \"array\" ? result : output === \"object\" ? {value: result[0], symbol: result[1], exponent: e, unit: u} : result.join(spacer);\n}\n\n// Partial application for functional programming\nfilesize.partial = opt => arg => filesize(arg, opt);\n\nexport default filesize;\n"],"names":["b","symbol","iec","bits","bytes","jedec","fullform","roundingFuncs","floor","Math","ceil","filesize","arg","e","base","full","fullforms","locale","localeOptions","neg","num","output","pad","round","u","unix","separator","spacer","standard","symbols","roundingFunc","precision","descriptor","result","val","isNaN","TypeError","Array","exponent","roundingMethod","Number","parseInt","log","pow","p","charAt","replace","test","toPrecision","toLocaleString","length","toString","isInteger","x","tmp","split","s","l","n","padEnd","value","unit","join","partial","opt"],"mappings":";;;;yOAAA,IAAMA,EAAI,UACTC,EAAS,CACRC,IAAK,CACJC,KAAM,CAAC,IAAK,MAAO,MAAO,MAAO,MAAO,MAAO,MAAO,MAAO,OAC7DC,MAAO,CAAC,IAAK,MAAO,MAAO,MAAO,MAAO,MAAO,MAAO,MAAO,QAE/DC,MAAO,CACNF,KAAM,CAAC,IAAK,KAAM,KAAM,KAAM,KAAM,KAAM,KAAM,KAAM,MACtDC,MAAO,CAAC,IAAK,KAAM,KAAM,KAAM,KAAM,KAAM,KAAM,KAAM,QAGzDE,EAAW,CACVJ,IAAK,CAAC,GAAI,OAAQ,OAAQ,OAAQ,OAAQ,OAAQ,OAAQ,OAAQ,QAClEG,MAAO,CAAC,GAAI,OAAQ,OAAQ,OAAQ,OAAQ,OAAQ,MAAO,QAAS,UAErEE,EAAgB,CACfC,MAAOC,KAAKD,MACZE,KAAMD,KAAKC,MAWb,SAASC,EAAUC,OAGjBC,EAAGC,EAAMX,E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AI3B,GAAa,IACtB4B,EAAMrB,EAAO,GAAGkB,WAAWI,MAAMF,GACjCG,EAAIF,EAAI,IAAM,GACdG,EAAID,EAAEN,OACNQ,EAAInC,EAAQkC,EAEbxB,EAAO,aAAQqB,EAAI,WAAKD,UAAIG,EAAEG,OAAOF,EAAIC,EAAG,aAGzC3C,IACHkB,EAAO,GAAKjB,EAAUH,GAAKG,EAAUH,GAAKP,EAASsB,GAAUf,IAAMV,EAAO,MAAQ,SAAyB,IAAd8B,EAAO,GAAW,GAAK,MAInG,UAAXZ,EAAqBY,EAAoB,WAAXZ,EAAsB,CAACuC,MAAO3B,EAAO,GAAIhC,OAAQgC,EAAO,GAAIK,SAAUzB,EAAGgD,KAAMrC,GAAKS,EAAO6B,KAAKnC,UAItIhB,EAASoD,QAAU,SAAAC,UAAO,SAAApD,UAAOD,EAASC,EAAKoD"} \ No newline at end of file diff --git a/src/filesize.js b/src/filesize.js index efbe24e..d237b84 100644 --- a/src/filesize.js +++ b/src/filesize.js @@ -29,7 +29,7 @@ const b = /^(b|B)$/, function filesize (arg, descriptor = {}) { let result = [], val = 0, - e, base, bits, ceil, full, fullforms, locale, localeOptions, neg, num, output, pad, round, u, unix, separator, spacer, standard, symbols, roundingFunc; + e, base, bits, ceil, full, fullforms, locale, localeOptions, neg, num, output, pad, round, u, unix, separator, spacer, standard, symbols, roundingFunc, precision; if (isNaN(arg)) { throw new TypeError("Invalid number"); @@ -54,6 +54,7 @@ function filesize (arg, descriptor = {}) { num = Number(arg); neg = num < 0; ceil = base > 2 ? 1000 : 1024; + precision = isNaN(descriptor.precision) === false ? parseInt(descriptor.precision, 10) : 0; // Flipping a negative number to determine the size if (neg) { @@ -71,6 +72,10 @@ function filesize (arg, descriptor = {}) { // Exceeding supported length, time to reduce & multiply if (e > 8) { + if (precision > 0) { + precision += 8 - e; + } + e = 8; } @@ -119,6 +124,11 @@ function filesize (arg, descriptor = {}) { result[0] = -result[0]; } + // Setting optional precision + if (precision > 0) { + result[0] = result[0].toPrecision(precision); + } + // Applying custom symbol result[1] = symbols[result[1]] || result[1]; diff --git a/test/filesize_test.js b/test/filesize_test.js index c2f9677..a124312 100644 --- a/test/filesize_test.js +++ b/test/filesize_test.js @@ -195,5 +195,14 @@ exports.filesize = { test.equal(filesize(1024 * 1.666, {round: 1, roundingMethod: "floor"}), "1.6 KB", "Should be '1.6 KB'"); test.equal(filesize(1024 * 1.666, {round: 1, roundingMethod: "ceil"}), "1.7 KB", "Should be '1.7 KB'"); test.done(); + }, + precision: function (test) { + test.expect(5); + test.equal(filesize(1024 * 1, {precision: 3}), "1.00 KB", "Should be '1.00 KB'"); + test.equal(filesize(1024 * 1024 * 10.25, {precision: 3}), "10.3 MB", "Should be '10.3 MB'"); + test.equal(filesize(1024 * 1024 * 10.25, {precision: "x"}), "10.25 MB", "Should be '10.25 MB'"); + test.equal(filesize(1024 * 1024 * 1024, {precision: 3}), "1.00 GB", "Should be '1.00 GB'"); + test.equal(filesize(Math.pow(1024, 10), {precision: 3}), "1e+6 YB", "Should be '1e+6 YB'"); + test.done(); } };