diff --git a/README.md b/README.md index dd2f553..a5110eb 100644 --- a/README.md +++ b/README.md @@ -56,9 +56,6 @@ _*(string)*_ Standard unit of measure, can be `iec` or `jedec`, default is `iec` ### symbols _*(object)*_ Dictionary of IEC/JEDEC symbols to replace for localization, defaults to english if no match is found -### unix -_*(boolean)*_ Enables unix style human readable output, e.g `ls -lh`, default is `false` - ## Examples ```javascript diff --git a/lib/filesize.es6.js b/lib/filesize.es6.js index 641918e..f538177 100644 --- a/lib/filesize.es6.js +++ b/lib/filesize.es6.js @@ -11,8 +11,7 @@ (global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.filesize = factory()); })(this, (function () { 'use strict'; - const b = /^(b|B)$/, - symbol = { + const symbol = { iec: { bits: ["bit", "Kibit", "Mibit", "Gibit", "Tibit", "Pibit", "Eibit", "Zibit", "Yibit"], bytes: ["B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB"] @@ -42,23 +41,22 @@ 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, precision; + e, base, bits, ceil, full, fullforms, locale, localeOptions, neg, num, output, pad, round, u, separator, spacer, standard, symbols, roundingFunc, precision; if (isNaN(arg)) { throw new TypeError("Invalid number"); } bits = descriptor.bits === true; - unix = descriptor.unix === true; pad = descriptor.pad === true; base = descriptor.base || 10; - round = descriptor.round !== void 0 ? descriptor.round : unix ? 1 : 2; + round = descriptor.round !== void 0 ? descriptor.round : 2; locale = descriptor.locale !== void 0 ? descriptor.locale : ""; localeOptions = descriptor.localeOptions || {}; separator = descriptor.separator !== void 0 ? descriptor.separator : ""; - spacer = descriptor.spacer !== void 0 ? descriptor.spacer : unix ? "" : " "; + spacer = descriptor.spacer !== void 0 ? descriptor.spacer : " "; symbols = descriptor.symbols || {}; - standard = base === 2 ? descriptor.standard || "iec" : "jedec"; + standard = descriptor.standard in symbol ? descriptor.standard : "iec"; output = descriptor.output || "string"; full = descriptor.fullform === true; fullforms = descriptor.fullforms instanceof Array ? descriptor.fullforms : []; @@ -99,7 +97,7 @@ // Zero is now a special case because bytes divide by 1 if (num === 0) { result[0] = 0; - u = result[1] = unix ? "" : symbol[standard][bits ? "bits" : "bytes"][e]; + u = result[1] = symbol[standard][bits ? "bits" : "bytes"][e]; } else { val = num / (base === 2 ? Math.pow(2, e * 10) : Math.pow(1000, e)); @@ -120,16 +118,7 @@ e++; } - u = result[1] = base === 10 && e === 1 ? bits ? "kbit" : "kB" : symbol[standard][bits ? "bits" : "bytes"][e]; - - if (unix) { - result[1] = result[1].charAt(0); - - if (b.test(result[1])) { - result[0] = Math.floor(result[0]); - result[1] = ""; - } - } + u = result[1] = symbol[standard][bits ? "bits" : "bytes"][e]; } // Decorating a 'diff' diff --git a/lib/filesize.es6.min.js b/lib/filesize.es6.min.js index 959690e..86c8830 100644 --- a/lib/filesize.es6.min.js +++ b/lib/filesize.es6.min.js @@ -2,5 +2,5 @@ 2022 Jason Mulligan @version 8.0.7 */ -!function(i,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(i="undefined"!=typeof globalThis?globalThis:i||self).filesize=t()}(this,(function(){"use strict";const i=/^(b|B)$/,t={iec:{bits:["bit","Kibit","Mibit","Gibit","Tibit","Pibit","Eibit","Zibit","Yibit"],bytes:["B","KiB","MiB","GiB","TiB","PiB","EiB","ZiB","YiB"]},jedec:{bits:["bit","Kbit","Mbit","Gbit","Tbit","Pbit","Ebit","Zbit","Ybit"],bytes:["B","KB","MB","GB","TB","PB","EB","ZB","YB"]}},e={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,T,j,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||10,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?"":" ",T=r.symbols||{},N=2===b?r.standard||"iec":"jedec",h=r.output||"string",c=!0===r.fullform,p=r.fullforms instanceof Array?r.fullforms:[],a=void 0!==r.exponent?r.exponent:-1,j=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?"":t[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 e=Math.pow(10,a>0?y:0);w[0]=j(P*e)/e,w[0]===l&&a<8&&void 0===r.exponent&&(w[0]=1,a++),M=w[1]=10===b&&1===a?s?"kbit":"kB":t[N][s?"bits":"bytes"][a],m&&(w[1]=w[1].charAt(0),i.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]=T[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 i=x||".",t=w[0].toString().split(i),e=t[1]||"",o=e.length,n=y-o;w[0]=`${t[0]}${i}${e.padEnd(o+n,"0")}`}return c&&(w[1]=p[a]?p[a]:e[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=i=>t=>n(t,i),n})); +!function(i,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(i="undefined"!=typeof globalThis?globalThis:i||self).filesize=t()}(this,(function(){"use strict";const i={iec:{bits:["bit","Kibit","Mibit","Gibit","Tibit","Pibit","Eibit","Zibit","Yibit"],bytes:["B","KiB","MiB","GiB","TiB","PiB","EiB","ZiB","YiB"]},jedec:{bits:["bit","Kbit","Mbit","Gbit","Tbit","Pbit","Ebit","Zbit","Ybit"],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"]},e={floor:Math.floor,ceil:Math.ceil};function o(o,n={}){let r,a,b,s,l,d,p,c,f,u,g,B,h,y,M,m,x,v,N,T,E=[],j=0;if(isNaN(o))throw new TypeError("Invalid number");if(b=!0===n.bits,B=!0===n.pad,a=n.base||10,h=void 0!==n.round?n.round:2,p=void 0!==n.locale?n.locale:"",c=n.localeOptions||{},M=void 0!==n.separator?n.separator:"",m=void 0!==n.spacer?n.spacer:" ",v=n.symbols||{},x=n.standard in i?n.standard:"iec",g=n.output||"string",l=!0===n.fullform,d=n.fullforms instanceof Array?n.fullforms:[],r=void 0!==n.exponent?n.exponent:-1,N=e[n.roundingMethod]||Math.round,u=Number(o),f=u<0,s=a>2?1e3:1024,T=!1===isNaN(n.precision)?parseInt(n.precision,10):0,f&&(u=-u),(-1===r||isNaN(r))&&(r=Math.floor(Math.log(u)/Math.log(s)),r<0&&(r=0)),r>8&&(T>0&&(T+=8-r),r=8),"exponent"===g)return r;if(0===u)E[0]=0,y=E[1]=i[x][b?"bits":"bytes"][r];else{j=u/(2===a?Math.pow(2,10*r):Math.pow(1e3,r)),b&&(j*=8,j>=s&&r<8&&(j/=s,r++));const t=Math.pow(10,r>0?h:0);E[0]=N(j*t)/t,E[0]===s&&r<8&&void 0===n.exponent&&(E[0]=1,r++),y=E[1]=i[x][b?"bits":"bytes"][r]}if(f&&(E[0]=-E[0]),T>0&&(E[0]=E[0].toPrecision(T)),E[1]=v[E[1]]||E[1],!0===p?E[0]=E[0].toLocaleString():p.length>0?E[0]=E[0].toLocaleString(p,c):M.length>0&&(E[0]=E[0].toString().replace(".",M)),B&&!1===Number.isInteger(E[0])&&h>0){const i=M||".",t=E[0].toString().split(i),e=t[1]||"",o=e.length,n=h-o;E[0]=`${t[0]}${i}${e.padEnd(o+n,"0")}`}return l&&(E[1]=d[r]?d[r]:t[x][r]+(b?"bit":"byte")+(1===E[0]?"":"s")),"array"===g?E:"object"===g?{value:E[0],symbol:E[1],exponent:r,unit:y}:E.join(m)}return o.partial=i=>t=>o(t,i),o})); //# sourceMappingURL=filesize.es6.min.js.map diff --git a/lib/filesize.es6.min.js.map b/lib/filesize.es6.min.js.map index 357f271..ec34546 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)$/,\r\n\tsymbol = {\r\n\t\tiec: {\r\n\t\t\tbits: [\"bit\", \"Kibit\", \"Mibit\", \"Gibit\", \"Tibit\", \"Pibit\", \"Eibit\", \"Zibit\", \"Yibit\"],\r\n\t\t\tbytes: [\"B\", \"KiB\", \"MiB\", \"GiB\", \"TiB\", \"PiB\", \"EiB\", \"ZiB\", \"YiB\"]\r\n\t\t},\r\n\t\tjedec: {\r\n\t\t\tbits: [\"bit\", \"Kbit\", \"Mbit\", \"Gbit\", \"Tbit\", \"Pbit\", \"Ebit\", \"Zbit\", \"Ybit\"],\r\n\t\t\tbytes: [\"B\", \"KB\", \"MB\", \"GB\", \"TB\", \"PB\", \"EB\", \"ZB\", \"YB\"]\r\n\t\t}\r\n\t},\r\n\tfullform = {\r\n\t\tiec: [\"\", \"kibi\", \"mebi\", \"gibi\", \"tebi\", \"pebi\", \"exbi\", \"zebi\", \"yobi\"],\r\n\t\tjedec: [\"\", \"kilo\", \"mega\", \"giga\", \"tera\", \"peta\", \"exa\", \"zetta\", \"yotta\"]\r\n\t},\r\n\troundingFuncs = {\r\n\t\tfloor: Math.floor,\r\n\t\tceil: Math.ceil\r\n\t};\r\n\r\n/**\r\n * filesize\r\n *\r\n * @method filesize\r\n * @param {Mixed} arg String, Int or Float to transform\r\n * @param {Object} descriptor [Optional] Flags\r\n * @return {String} Readable file size String\r\n */\r\nfunction filesize (arg, descriptor = {}) {\r\n\tlet result = [],\r\n\t\tval = 0,\r\n\t\te, base, bits, ceil, full, fullforms, locale, localeOptions, neg, num, output, pad, round, u, unix, separator, spacer, standard, symbols, roundingFunc, precision;\r\n\r\n\tif (isNaN(arg)) {\r\n\t\tthrow new TypeError(\"Invalid number\");\r\n\t}\r\n\r\n\tbits = descriptor.bits === true;\r\n\tunix = descriptor.unix === true;\r\n\tpad = descriptor.pad === true;\r\n\tbase = descriptor.base || 10;\r\n\tround = descriptor.round !== void 0 ? descriptor.round : unix ? 1 : 2;\r\n\tlocale = descriptor.locale !== void 0 ? descriptor.locale : \"\";\r\n\tlocaleOptions = descriptor.localeOptions || {};\r\n\tseparator = descriptor.separator !== void 0 ? descriptor.separator : \"\";\r\n\tspacer = descriptor.spacer !== void 0 ? descriptor.spacer : unix ? \"\" : \" \";\r\n\tsymbols = descriptor.symbols || {};\r\n\tstandard = base === 2 ? descriptor.standard || \"iec\" : \"jedec\";\r\n\toutput = descriptor.output || \"string\";\r\n\tfull = descriptor.fullform === true;\r\n\tfullforms = descriptor.fullforms instanceof Array ? descriptor.fullforms : [];\r\n\te = descriptor.exponent !== void 0 ? descriptor.exponent : -1;\r\n\troundingFunc = roundingFuncs[descriptor.roundingMethod] || Math.round;\r\n\tnum = Number(arg);\r\n\tneg = num < 0;\r\n\tceil = base > 2 ? 1000 : 1024;\r\n\tprecision = isNaN(descriptor.precision) === false ? parseInt(descriptor.precision, 10) : 0;\r\n\r\n\t// Flipping a negative number to determine the size\r\n\tif (neg) {\r\n\t\tnum = -num;\r\n\t}\r\n\r\n\t// Determining the exponent\r\n\tif (e === -1 || isNaN(e)) {\r\n\t\te = Math.floor(Math.log(num) / Math.log(ceil));\r\n\r\n\t\tif (e < 0) {\r\n\t\t\te = 0;\r\n\t\t}\r\n\t}\r\n\r\n\t// Exceeding supported length, time to reduce & multiply\r\n\tif (e > 8) {\r\n\t\tif (precision > 0) {\r\n\t\t\tprecision += 8 - e;\r\n\t\t}\r\n\r\n\t\te = 8;\r\n\t}\r\n\r\n\tif (output === \"exponent\") {\r\n\t\treturn e;\r\n\t}\r\n\r\n\t// Zero is now a special case because bytes divide by 1\r\n\tif (num === 0) {\r\n\t\tresult[0] = 0;\r\n\t\tu = result[1] = unix ? \"\" : symbol[standard][bits ? \"bits\" : \"bytes\"][e];\r\n\t} else {\r\n\t\tval = num / (base === 2 ? Math.pow(2, e * 10) : Math.pow(1000, e));\r\n\r\n\t\tif (bits) {\r\n\t\t\tval = val * 8;\r\n\r\n\t\t\tif (val >= ceil && e < 8) {\r\n\t\t\t\tval = val / ceil;\r\n\t\t\t\te++;\r\n\t\t\t}\r\n\t\t}\r\n\r\n\t\tconst p = Math.pow(10, e > 0 ? round : 0);\r\n\t\tresult[0] = roundingFunc(val * p) / p;\r\n\r\n\t\tif (result[0] === ceil && e < 8 && descriptor.exponent === void 0) {\r\n\t\t\tresult[0] = 1;\r\n\t\t\te++;\r\n\t\t}\r\n\r\n\t\tu = result[1] = base === 10 && e === 1 ? bits ? \"kbit\" : \"kB\" : symbol[standard][bits ? \"bits\" : \"bytes\"][e];\r\n\r\n\t\tif (unix) {\r\n\t\t\tresult[1] = result[1].charAt(0);\r\n\r\n\t\t\tif (b.test(result[1])) {\r\n\t\t\t\tresult[0] = Math.floor(result[0]);\r\n\t\t\t\tresult[1] = \"\";\r\n\t\t\t}\r\n\t\t}\r\n\t}\r\n\r\n\t// Decorating a 'diff'\r\n\tif (neg) {\r\n\t\tresult[0] = -result[0];\r\n\t}\r\n\r\n\t// Setting optional precision\r\n\tif (precision > 0) {\r\n\t\tresult[0] = result[0].toPrecision(precision);\r\n\t}\r\n\r\n\t// Applying custom symbol\r\n\tresult[1] = symbols[result[1]] || result[1];\r\n\r\n\tif (locale === true) {\r\n\t\tresult[0] = result[0].toLocaleString();\r\n\t} else if (locale.length > 0) {\r\n\t\tresult[0] = result[0].toLocaleString(locale, localeOptions);\r\n\t} else if (separator.length > 0) {\r\n\t\tresult[0] = result[0].toString().replace(\".\", separator);\r\n\t}\r\n\r\n\tif (pad && Number.isInteger(result[0]) === false && round > 0) {\r\n\t\tconst x = separator || \".\",\r\n\t\t\ttmp = result[0].toString().split(x),\r\n\t\t\ts = tmp[1] || \"\",\r\n\t\t\tl = s.length,\r\n\t\t\tn = round - l;\r\n\r\n\t\tresult[0] = `${tmp[0]}${x}${s.padEnd(l + n, \"0\")}`;\r\n\t}\r\n\r\n\tif (full) {\r\n\t\tresult[1] = fullforms[e] ? fullforms[e] : fullform[standard][e] + (bits ? \"bit\" : \"byte\") + (result[0] === 1 ? \"\" : \"s\");\r\n\t}\r\n\r\n\t// Returning Array, Object, or String (default)\r\n\treturn output === \"array\" ? result : output === \"object\" ? {value: result[0], symbol: result[1], exponent: e, unit: u} : result.join(spacer);\r\n}\r\n\r\n// Partial application for functional programming\r\nfilesize.partial = opt => arg => filesize(arg, opt);\r\n\r\nexport default filesize;\r\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","test","toPrecision","toLocaleString","length","toString","replace","isInteger","x","tmp","split","s","l","n","padEnd","value","unit","join","partial","opt"],"mappings":";;;;yOAAA,MAAMA,EAAI,UACTC,EAAS,CACRC,IAAK,CACJC,KAAM,CAAC,MAAO,QAAS,QAAS,Q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7C,EAAE8C,KAAKb,EAAO,MACjBA,EAAO,GAAKxB,KAAKD,MAAMyB,EAAO,IAC9BA,EAAO,GAAK,KA0Bf,GApBIb,IACHa,EAAO,IAAMA,EAAO,IAIjBD,EAAY,IACfC,EAAO,GAAKA,EAAO,GAAGc,YAAYf,IAInCC,EAAO,GAAKH,EAAQG,EAAO,KAAOA,EAAO,IAE1B,IAAXf,EACHe,EAAO,GAAKA,EAAO,GAAGe,iBACZ9B,EAAO+B,OAAS,EAC1BhB,EAAO,GAAKA,EAAO,GAAGe,eAAe9B,EAAQC,GACnCQ,EAAUsB,OAAS,IAC7BhB,EAAO,GAAKA,EAAO,GAAGiB,WAAWC,QAAQ,IAAKxB,IAG3CJ,IAAuC,IAAhCiB,OAAOY,UAAUnB,EAAO,KAAiBT,EAAQ,EAAG,CAC9D,MAAM6B,EAAI1B,GAAa,IACtB2B,EAAMrB,EAAO,GAAGiB,WAAWK,MAAMF,GACjCG,EAAIF,EAAI,IAAM,GACdG,EAAID,EAAEP,OACNS,EAAIlC,EAAQiC,EAEbxB,EAAO,GAAK,GAAGqB,EAAI,KAAKD,IAAIG,EAAEG,OAAOF,EAAIC,EAAG,OAQ7C,OALI1C,IACHiB,EAAO,GAAKhB,EAAUH,GAAKG,EAAUH,GAAKR,EAASuB,GAAUf,IAAMX,EAAO,MAAQ,SAAyB,IAAd8B,EAAO,GAAW,GAAK,MAInG,UAAXX,EAAqBW,EAAoB,WAAXX,EAAsB,CAACsC,MAAO3B,EAAO,GAAIhC,OAAQgC,EAAO,GAAIK,SAAUxB,EAAG+C,KAAMpC,GAAKQ,EAAO6B,KAAKlC,UAItIjB,EAASoD,QAAUC,GAAOpD,GAAOD,EAASC,EAAKoD"} \ No newline at end of file +{"version":3,"file":"filesize.es6.min.js","sources":["../src/filesize.js"],"sourcesContent":["const symbol = {\n\t\tiec: {\n\t\t\tbits: [\"bit\", \"Kibit\", \"Mibit\", \"Gibit\", \"Tibit\", \"Pibit\", \"Eibit\", \"Zibit\", \"Yibit\"],\n\t\t\tbytes: [\"B\", \"KiB\", \"MiB\", \"GiB\", \"TiB\", \"PiB\", \"EiB\", \"ZiB\", \"YiB\"]\n\t\t},\n\t\tjedec: {\n\t\t\tbits: [\"bit\", \"Kbit\", \"Mbit\", \"Gbit\", \"Tbit\", \"Pbit\", \"Ebit\", \"Zbit\", \"Ybit\"],\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, 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\tpad = descriptor.pad === true;\n\tbase = descriptor.base || 10;\n\tround = descriptor.round !== void 0 ? descriptor.round : 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 : \" \";\n\tsymbols = descriptor.symbols || {};\n\tstandard = descriptor.standard in symbol ? descriptor.standard : \"iec\";\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] = 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] = symbol[standard][bits ? \"bits\" : \"bytes\"][e];\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":["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","separator","spacer","standard","symbols","roundingFunc","precision","result","val","isNaN","TypeError","Array","exponent","roundingMethod","Number","parseInt","log","pow","p","toPrecision","toLocaleString","length","toString","replace","isInteger","x","tmp","split","s","l","n","padEnd","value","unit","join","partial","opt"],"mappings":";;;;yOAAA,MAAMA,EAAS,CACbC,IAAK,CACJC,KAAM,CAAC,MAAO,QAAS,QAAS,QAAS,QAAS,QAAS,QAAS,QAAS,SAC7EC,MAAO,CAAC,IAAK,MAAO,MAAO,MAAO,MAAO,MAAO,MAAO,MAAO,QAE/DC,MAAO,CACNF,KAAM,CAAC,MAAO,OAAQ,OAAQ,OAAQ,OAAQ,OAAQ,OAAQ,OAAQ,QACtEC,MAAO,CAAC,IAAK,KAAM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EAAO,KAAiBR,EAAQ,EAAG,CAC9D,MAAM0B,EAAIxB,GAAa,IACtByB,EAAMnB,EAAO,GAAGe,WAAWK,MAAMF,GACjCG,EAAIF,EAAI,IAAM,GACdG,EAAID,EAAEP,OACNS,EAAI/B,EAAQ8B,EAEbtB,EAAO,GAAK,GAAGmB,EAAI,KAAKD,IAAIG,EAAEG,OAAOF,EAAIC,EAAG,OAQ7C,OALIvC,IACHgB,EAAO,GAAKf,EAAUH,GAAKG,EAAUH,GAAKR,EAASsB,GAAUd,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,KAAKhC,UAItIhB,EAASiD,QAAUC,GAAOjD,GAAOD,EAASC,EAAKiD"} \ No newline at end of file diff --git a/lib/filesize.esm.js b/lib/filesize.esm.js index d9b7683..7976b4e 100644 --- a/lib/filesize.esm.js +++ b/lib/filesize.esm.js @@ -5,8 +5,7 @@ * @license BSD-3-Clause * @version 8.0.7 */ -const b = /^(b|B)$/, - symbol = { +const symbol = { iec: { bits: ["bit", "Kibit", "Mibit", "Gibit", "Tibit", "Pibit", "Eibit", "Zibit", "Yibit"], bytes: ["B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB"] @@ -36,23 +35,22 @@ 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, precision; + e, base, bits, ceil, full, fullforms, locale, localeOptions, neg, num, output, pad, round, u, separator, spacer, standard, symbols, roundingFunc, precision; if (isNaN(arg)) { throw new TypeError("Invalid number"); } bits = descriptor.bits === true; - unix = descriptor.unix === true; pad = descriptor.pad === true; base = descriptor.base || 10; - round = descriptor.round !== void 0 ? descriptor.round : unix ? 1 : 2; + round = descriptor.round !== void 0 ? descriptor.round : 2; locale = descriptor.locale !== void 0 ? descriptor.locale : ""; localeOptions = descriptor.localeOptions || {}; separator = descriptor.separator !== void 0 ? descriptor.separator : ""; - spacer = descriptor.spacer !== void 0 ? descriptor.spacer : unix ? "" : " "; + spacer = descriptor.spacer !== void 0 ? descriptor.spacer : " "; symbols = descriptor.symbols || {}; - standard = base === 2 ? descriptor.standard || "iec" : "jedec"; + standard = descriptor.standard in symbol ? descriptor.standard : "iec"; output = descriptor.output || "string"; full = descriptor.fullform === true; fullforms = descriptor.fullforms instanceof Array ? descriptor.fullforms : []; @@ -93,7 +91,7 @@ function filesize (arg, descriptor = {}) { // Zero is now a special case because bytes divide by 1 if (num === 0) { result[0] = 0; - u = result[1] = unix ? "" : symbol[standard][bits ? "bits" : "bytes"][e]; + u = result[1] = symbol[standard][bits ? "bits" : "bytes"][e]; } else { val = num / (base === 2 ? Math.pow(2, e * 10) : Math.pow(1000, e)); @@ -114,16 +112,7 @@ function filesize (arg, descriptor = {}) { e++; } - u = result[1] = base === 10 && e === 1 ? bits ? "kbit" : "kB" : symbol[standard][bits ? "bits" : "bytes"][e]; - - if (unix) { - result[1] = result[1].charAt(0); - - if (b.test(result[1])) { - result[0] = Math.floor(result[0]); - result[1] = ""; - } - } + u = result[1] = symbol[standard][bits ? "bits" : "bytes"][e]; } // Decorating a 'diff' diff --git a/lib/filesize.esm.min.js b/lib/filesize.esm.min.js index caff6e9..2a2d88a 100644 --- a/lib/filesize.esm.min.js +++ b/lib/filesize.esm.min.js @@ -2,5 +2,5 @@ 2022 Jason Mulligan @version 8.0.7 */ -const i=/^(b|B)$/,t={iec:{bits:["bit","Kibit","Mibit","Gibit","Tibit","Pibit","Eibit","Zibit","Yibit"],bytes:["B","KiB","MiB","GiB","TiB","PiB","EiB","ZiB","YiB"]},jedec:{bits:["bit","Kbit","Mbit","Gbit","Tbit","Pbit","Ebit","Zbit","Ybit"],bytes:["B","KB","MB","GB","TB","PB","EB","ZB","YB"]}},e={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 a(a,r={}){let b,n,s,l,c,p,d,B,u,f,g,h,M,y,m,x,v,N,E,j,w,P=[],T=0;if(isNaN(a))throw new TypeError("Invalid number");if(s=!0===r.bits,m=!0===r.unix,h=!0===r.pad,n=r.base||10,M=void 0!==r.round?r.round:m?1:2,d=void 0!==r.locale?r.locale:"",B=r.localeOptions||{},x=void 0!==r.separator?r.separator:"",v=void 0!==r.spacer?r.spacer:m?"":" ",E=r.symbols||{},N=2===n?r.standard||"iec":"jedec",g=r.output||"string",c=!0===r.fullform,p=r.fullforms instanceof Array?r.fullforms:[],b=void 0!==r.exponent?r.exponent:-1,j=o[r.roundingMethod]||Math.round,f=Number(a),u=f<0,l=n>2?1e3:1024,w=!1===isNaN(r.precision)?parseInt(r.precision,10):0,u&&(f=-f),(-1===b||isNaN(b))&&(b=Math.floor(Math.log(f)/Math.log(l)),b<0&&(b=0)),b>8&&(w>0&&(w+=8-b),b=8),"exponent"===g)return b;if(0===f)P[0]=0,y=P[1]=m?"":t[N][s?"bits":"bytes"][b];else{T=f/(2===n?Math.pow(2,10*b):Math.pow(1e3,b)),s&&(T*=8,T>=l&&b<8&&(T/=l,b++));const e=Math.pow(10,b>0?M:0);P[0]=j(T*e)/e,P[0]===l&&b<8&&void 0===r.exponent&&(P[0]=1,b++),y=P[1]=10===n&&1===b?s?"kbit":"kB":t[N][s?"bits":"bytes"][b],m&&(P[1]=P[1].charAt(0),i.test(P[1])&&(P[0]=Math.floor(P[0]),P[1]=""))}if(u&&(P[0]=-P[0]),w>0&&(P[0]=P[0].toPrecision(w)),P[1]=E[P[1]]||P[1],!0===d?P[0]=P[0].toLocaleString():d.length>0?P[0]=P[0].toLocaleString(d,B):x.length>0&&(P[0]=P[0].toString().replace(".",x)),h&&!1===Number.isInteger(P[0])&&M>0){const i=x||".",t=P[0].toString().split(i),e=t[1]||"",o=e.length,a=M-o;P[0]=`${t[0]}${i}${e.padEnd(o+a,"0")}`}return c&&(P[1]=p[b]?p[b]:e[N][b]+(s?"bit":"byte")+(1===P[0]?"":"s")),"array"===g?P:"object"===g?{value:P[0],symbol:P[1],exponent:b,unit:y}:P.join(v)}a.partial=i=>t=>a(t,i);export{a as default}; +const i={iec:{bits:["bit","Kibit","Mibit","Gibit","Tibit","Pibit","Eibit","Zibit","Yibit"],bytes:["B","KiB","MiB","GiB","TiB","PiB","EiB","ZiB","YiB"]},jedec:{bits:["bit","Kbit","Mbit","Gbit","Tbit","Pbit","Ebit","Zbit","Ybit"],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"]},e={floor:Math.floor,ceil:Math.ceil};function o(o,a={}){let n,r,b,s,l,p,c,d,u,B,f,g,h,M,y,m,v,x,N,E,w=[],P=0;if(isNaN(o))throw new TypeError("Invalid number");if(b=!0===a.bits,g=!0===a.pad,r=a.base||10,h=void 0!==a.round?a.round:2,c=void 0!==a.locale?a.locale:"",d=a.localeOptions||{},y=void 0!==a.separator?a.separator:"",m=void 0!==a.spacer?a.spacer:" ",x=a.symbols||{},v=a.standard in i?a.standard:"iec",f=a.output||"string",l=!0===a.fullform,p=a.fullforms instanceof Array?a.fullforms:[],n=void 0!==a.exponent?a.exponent:-1,N=e[a.roundingMethod]||Math.round,B=Number(o),u=B<0,s=r>2?1e3:1024,E=!1===isNaN(a.precision)?parseInt(a.precision,10):0,u&&(B=-B),(-1===n||isNaN(n))&&(n=Math.floor(Math.log(B)/Math.log(s)),n<0&&(n=0)),n>8&&(E>0&&(E+=8-n),n=8),"exponent"===f)return n;if(0===B)w[0]=0,M=w[1]=i[v][b?"bits":"bytes"][n];else{P=B/(2===r?Math.pow(2,10*n):Math.pow(1e3,n)),b&&(P*=8,P>=s&&n<8&&(P/=s,n++));const t=Math.pow(10,n>0?h:0);w[0]=N(P*t)/t,w[0]===s&&n<8&&void 0===a.exponent&&(w[0]=1,n++),M=w[1]=i[v][b?"bits":"bytes"][n]}if(u&&(w[0]=-w[0]),E>0&&(w[0]=w[0].toPrecision(E)),w[1]=x[w[1]]||w[1],!0===c?w[0]=w[0].toLocaleString():c.length>0?w[0]=w[0].toLocaleString(c,d):y.length>0&&(w[0]=w[0].toString().replace(".",y)),g&&!1===Number.isInteger(w[0])&&h>0){const i=y||".",t=w[0].toString().split(i),e=t[1]||"",o=e.length,a=h-o;w[0]=`${t[0]}${i}${e.padEnd(o+a,"0")}`}return l&&(w[1]=p[n]?p[n]:t[v][n]+(b?"bit":"byte")+(1===w[0]?"":"s")),"array"===f?w:"object"===f?{value:w[0],symbol:w[1],exponent:n,unit:M}:w.join(m)}o.partial=i=>t=>o(t,i);export{o as default}; //# sourceMappingURL=filesize.esm.min.js.map diff --git a/lib/filesize.esm.min.js.map b/lib/filesize.esm.min.js.map index 7adcef7..bc52316 100644 --- a/lib/filesize.esm.min.js.map +++ b/lib/filesize.esm.min.js.map @@ -1 +1 @@ -{"version":3,"file":"filesize.esm.min.js","sources":["../src/filesize.js"],"sourcesContent":["const b = /^(b|B)$/,\r\n\tsymbol = {\r\n\t\tiec: {\r\n\t\t\tbits: [\"bit\", \"Kibit\", \"Mibit\", \"Gibit\", \"Tibit\", \"Pibit\", \"Eibit\", \"Zibit\", \"Yibit\"],\r\n\t\t\tbytes: [\"B\", \"KiB\", \"MiB\", \"GiB\", \"TiB\", \"PiB\", \"EiB\", \"ZiB\", \"YiB\"]\r\n\t\t},\r\n\t\tjedec: {\r\n\t\t\tbits: [\"bit\", \"Kbit\", \"Mbit\", \"Gbit\", \"Tbit\", \"Pbit\", \"Ebit\", \"Zbit\", \"Ybit\"],\r\n\t\t\tbytes: [\"B\", \"KB\", \"MB\", \"GB\", \"TB\", \"PB\", \"EB\", \"ZB\", \"YB\"]\r\n\t\t}\r\n\t},\r\n\tfullform = {\r\n\t\tiec: [\"\", \"kibi\", \"mebi\", \"gibi\", \"tebi\", \"pebi\", \"exbi\", \"zebi\", \"yobi\"],\r\n\t\tjedec: [\"\", \"kilo\", \"mega\", \"giga\", \"tera\", \"peta\", \"exa\", \"zetta\", \"yotta\"]\r\n\t},\r\n\troundingFuncs = {\r\n\t\tfloor: Math.floor,\r\n\t\tceil: Math.ceil\r\n\t};\r\n\r\n/**\r\n * filesize\r\n *\r\n * @method filesize\r\n * @param {Mixed} arg String, Int or Float to transform\r\n * @param {Object} descriptor [Optional] Flags\r\n * @return {String} Readable file size String\r\n */\r\nfunction filesize (arg, descriptor = {}) {\r\n\tlet result = [],\r\n\t\tval = 0,\r\n\t\te, base, bits, ceil, full, fullforms, locale, localeOptions, neg, num, output, pad, round, u, unix, separator, spacer, standard, symbols, roundingFunc, precision;\r\n\r\n\tif (isNaN(arg)) {\r\n\t\tthrow new TypeError(\"Invalid number\");\r\n\t}\r\n\r\n\tbits = descriptor.bits === true;\r\n\tunix = descriptor.unix === true;\r\n\tpad = descriptor.pad === true;\r\n\tbase = descriptor.base || 10;\r\n\tround = descriptor.round !== void 0 ? descriptor.round : unix ? 1 : 2;\r\n\tlocale = descriptor.locale !== void 0 ? descriptor.locale : \"\";\r\n\tlocaleOptions = descriptor.localeOptions || {};\r\n\tseparator = descriptor.separator !== void 0 ? descriptor.separator : \"\";\r\n\tspacer = descriptor.spacer !== void 0 ? descriptor.spacer : unix ? \"\" : \" \";\r\n\tsymbols = descriptor.symbols || {};\r\n\tstandard = base === 2 ? descriptor.standard || \"iec\" : \"jedec\";\r\n\toutput = descriptor.output || \"string\";\r\n\tfull = descriptor.fullform === true;\r\n\tfullforms = descriptor.fullforms instanceof Array ? descriptor.fullforms : [];\r\n\te = descriptor.exponent !== void 0 ? descriptor.exponent : -1;\r\n\troundingFunc = roundingFuncs[descriptor.roundingMethod] || Math.round;\r\n\tnum = Number(arg);\r\n\tneg = num < 0;\r\n\tceil = base > 2 ? 1000 : 1024;\r\n\tprecision = isNaN(descriptor.precision) === false ? parseInt(descriptor.precision, 10) : 0;\r\n\r\n\t// Flipping a negative number to determine the size\r\n\tif (neg) {\r\n\t\tnum = -num;\r\n\t}\r\n\r\n\t// Determining the exponent\r\n\tif (e === -1 || isNaN(e)) {\r\n\t\te = Math.floor(Math.log(num) / Math.log(ceil));\r\n\r\n\t\tif (e < 0) {\r\n\t\t\te = 0;\r\n\t\t}\r\n\t}\r\n\r\n\t// Exceeding supported length, time to reduce & multiply\r\n\tif (e > 8) {\r\n\t\tif (precision > 0) {\r\n\t\t\tprecision += 8 - e;\r\n\t\t}\r\n\r\n\t\te = 8;\r\n\t}\r\n\r\n\tif (output === \"exponent\") {\r\n\t\treturn e;\r\n\t}\r\n\r\n\t// Zero is now a special case because bytes divide by 1\r\n\tif (num === 0) {\r\n\t\tresult[0] = 0;\r\n\t\tu = result[1] = unix ? \"\" : symbol[standard][bits ? \"bits\" : \"bytes\"][e];\r\n\t} else {\r\n\t\tval = num / (base === 2 ? Math.pow(2, e * 10) : Math.pow(1000, e));\r\n\r\n\t\tif (bits) {\r\n\t\t\tval = val * 8;\r\n\r\n\t\t\tif (val >= ceil && e < 8) {\r\n\t\t\t\tval = val / ceil;\r\n\t\t\t\te++;\r\n\t\t\t}\r\n\t\t}\r\n\r\n\t\tconst p = Math.pow(10, e > 0 ? round : 0);\r\n\t\tresult[0] = roundingFunc(val * p) / p;\r\n\r\n\t\tif (result[0] === ceil && e < 8 && descriptor.exponent === void 0) {\r\n\t\t\tresult[0] = 1;\r\n\t\t\te++;\r\n\t\t}\r\n\r\n\t\tu = result[1] = base === 10 && e === 1 ? bits ? \"kbit\" : \"kB\" : symbol[standard][bits ? \"bits\" : \"bytes\"][e];\r\n\r\n\t\tif (unix) {\r\n\t\t\tresult[1] = result[1].charAt(0);\r\n\r\n\t\t\tif (b.test(result[1])) {\r\n\t\t\t\tresult[0] = Math.floor(result[0]);\r\n\t\t\t\tresult[1] = \"\";\r\n\t\t\t}\r\n\t\t}\r\n\t}\r\n\r\n\t// Decorating a 'diff'\r\n\tif (neg) {\r\n\t\tresult[0] = -result[0];\r\n\t}\r\n\r\n\t// Setting optional precision\r\n\tif (precision > 0) {\r\n\t\tresult[0] = result[0].toPrecision(precision);\r\n\t}\r\n\r\n\t// Applying custom symbol\r\n\tresult[1] = symbols[result[1]] || result[1];\r\n\r\n\tif (locale === true) {\r\n\t\tresult[0] = result[0].toLocaleString();\r\n\t} else if (locale.length > 0) {\r\n\t\tresult[0] = result[0].toLocaleString(locale, localeOptions);\r\n\t} else if (separator.length > 0) {\r\n\t\tresult[0] = result[0].toString().replace(\".\", separator);\r\n\t}\r\n\r\n\tif (pad && Number.isInteger(result[0]) === false && round > 0) {\r\n\t\tconst x = separator || \".\",\r\n\t\t\ttmp = result[0].toString().split(x),\r\n\t\t\ts = tmp[1] || \"\",\r\n\t\t\tl = s.length,\r\n\t\t\tn = round - l;\r\n\r\n\t\tresult[0] = `${tmp[0]}${x}${s.padEnd(l + n, \"0\")}`;\r\n\t}\r\n\r\n\tif (full) {\r\n\t\tresult[1] = fullforms[e] ? fullforms[e] : fullform[standard][e] + (bits ? \"bit\" : \"byte\") + (result[0] === 1 ? \"\" : \"s\");\r\n\t}\r\n\r\n\t// Returning Array, Object, or String (default)\r\n\treturn output === \"array\" ? result : output === \"object\" ? {value: result[0], symbol: result[1], exponent: e, unit: u} : result.join(spacer);\r\n}\r\n\r\n// Partial application for functional programming\r\nfilesize.partial = opt => arg => filesize(arg, opt);\r\n\r\nexport default filesize;\r\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","test","toPrecision","toLocaleString","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"filesize.esm.min.js","sources":["../src/filesize.js"],"sourcesContent":["const symbol = {\n\t\tiec: {\n\t\t\tbits: [\"bit\", \"Kibit\", \"Mibit\", \"Gibit\", \"Tibit\", \"Pibit\", \"Eibit\", \"Zibit\", \"Yibit\"],\n\t\t\tbytes: [\"B\", \"KiB\", \"MiB\", \"GiB\", \"TiB\", \"PiB\", \"EiB\", \"ZiB\", \"YiB\"]\n\t\t},\n\t\tjedec: {\n\t\t\tbits: [\"bit\", \"Kbit\", \"Mbit\", \"Gbit\", \"Tbit\", \"Pbit\", \"Ebit\", \"Zbit\", \"Ybit\"],\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, 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\tpad = descriptor.pad === true;\n\tbase = descriptor.base || 10;\n\tround = descriptor.round !== void 0 ? descriptor.round : 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 : \" \";\n\tsymbols = descriptor.symbols || {};\n\tstandard = descriptor.standard in symbol ? descriptor.standard : \"iec\";\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] = 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] = symbol[standard][bits ? \"bits\" : \"bytes\"][e];\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":["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","separator","spacer","standard","symbols","roundingFunc","precision","result","val","isNaN","TypeError","Array","exponent","roundingMethod","Number","parseInt","log","pow","p","toPrecision","toLocaleString","length","toString","replace","isInteger","x","tmp","split","s","l","n","padEnd","value","unit","join","partial","opt"],"mappings":";;;;AAAA,MAAMA,EAAS,CACbC,IAAK,CACJC,KAAM,CAAC,MAAO,QAAS,QAAS,QAAS,QAAS,QAAS,QAAS,QAAS,SAC7EC,MAAO,CAAC,IAAK,MAAO,MAAO,MAA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,GAAGa,eAAe3B,EAAQC,GACnCO,EAAUoB,OAAS,IAC7Bd,EAAO,GAAKA,EAAO,GAAGe,WAAWC,QAAQ,IAAKtB,IAG3CH,IAAuC,IAAhCgB,OAAOU,UAAUjB,EAAO,KAAiBR,EAAQ,EAAG,CAC9D,MAAM0B,EAAIxB,GAAa,IACtByB,EAAMnB,EAAO,GAAGe,WAAWK,MAAMF,GACjCG,EAAIF,EAAI,IAAM,GACdG,EAAID,EAAEP,OACNS,EAAI/B,EAAQ8B,EAEbtB,EAAO,GAAK,GAAGmB,EAAI,KAAKD,IAAIG,EAAEG,OAAOF,EAAIC,EAAG,OAQ7C,OALIvC,IACHgB,EAAO,GAAKf,EAAUH,GAAKG,EAAUH,GAAKR,EAASsB,GAAUd,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,KAAKhC,GAItIhB,EAASiD,QAAUC,GAAOjD,GAAOD,EAASC,EAAKiD"} \ No newline at end of file diff --git a/lib/filesize.js b/lib/filesize.js index 13b63ab..72b8a8d 100644 --- a/lib/filesize.js +++ b/lib/filesize.js @@ -11,8 +11,7 @@ (global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.filesize = factory()); })(this, (function () { 'use strict'; - var b = /^(b|B)$/, - symbol = { + var symbol = { iec: { bits: ["bit", "Kibit", "Mibit", "Gibit", "Tibit", "Pibit", "Eibit", "Zibit", "Yibit"], bytes: ["B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB"] @@ -57,7 +56,6 @@ pad, round, u, - unix, separator, spacer, standard, @@ -70,16 +68,15 @@ } bits = descriptor.bits === true; - unix = descriptor.unix === true; pad = descriptor.pad === true; base = descriptor.base || 10; - round = descriptor.round !== void 0 ? descriptor.round : unix ? 1 : 2; + round = descriptor.round !== void 0 ? descriptor.round : 2; locale = descriptor.locale !== void 0 ? descriptor.locale : ""; localeOptions = descriptor.localeOptions || {}; separator = descriptor.separator !== void 0 ? descriptor.separator : ""; - spacer = descriptor.spacer !== void 0 ? descriptor.spacer : unix ? "" : " "; + spacer = descriptor.spacer !== void 0 ? descriptor.spacer : " "; symbols = descriptor.symbols || {}; - standard = base === 2 ? descriptor.standard || "iec" : "jedec"; + standard = descriptor.standard in symbol ? descriptor.standard : "iec"; output = descriptor.output || "string"; full = descriptor.fullform === true; fullforms = descriptor.fullforms instanceof Array ? descriptor.fullforms : []; @@ -119,7 +116,7 @@ if (num === 0) { result[0] = 0; - u = result[1] = unix ? "" : symbol[standard][bits ? "bits" : "bytes"][e]; + u = result[1] = symbol[standard][bits ? "bits" : "bytes"][e]; } else { val = num / (base === 2 ? Math.pow(2, e * 10) : Math.pow(1000, e)); @@ -140,16 +137,7 @@ e++; } - u = result[1] = base === 10 && e === 1 ? bits ? "kbit" : "kB" : symbol[standard][bits ? "bits" : "bytes"][e]; - - if (unix) { - result[1] = result[1].charAt(0); - - if (b.test(result[1])) { - result[0] = Math.floor(result[0]); - result[1] = ""; - } - } + u = result[1] = symbol[standard][bits ? "bits" : "bytes"][e]; } // Decorating a 'diff' diff --git a/lib/filesize.min.js b/lib/filesize.min.js index c4ecda1..a4c5feb 100644 --- a/lib/filesize.min.js +++ b/lib/filesize.min.js @@ -2,5 +2,5 @@ 2022 Jason Mulligan @version 8.0.7 */ -!function(i,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(i="undefined"!=typeof globalThis?globalThis:i||self).filesize=t()}(this,(function(){"use strict";var i=/^(b|B)$/,t={iec:{bits:["bit","Kibit","Mibit","Gibit","Tibit","Pibit","Eibit","Zibit","Yibit"],bytes:["B","KiB","MiB","GiB","TiB","PiB","EiB","ZiB","YiB"]},jedec:{bits:["bit","Kbit","Mbit","Gbit","Tbit","Pbit","Ebit","Zbit","Ybit"],bytes:["B","KB","MB","GB","TB","PB","EB","ZB","YB"]}},e={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,s,l,c,f,d,p,u,h,B,g,y,M,m,v,x,N,T,j,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,B=!0===E.pad,a=E.base||10,g=void 0!==E.round?E.round:M?1:2,f=void 0!==E.locale?E.locale:"",d=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||"iec":"jedec",h=E.output||"string",l=!0===E.fullform,c=E.fullforms instanceof Array?E.fullforms:[],r=void 0!==E.exponent?E.exponent:-1,T=o[E.roundingMethod]||Math.round,p=(u=Number(n))<0,s=a>2?1e3:1024,j=!1===isNaN(E.precision)?parseInt(E.precision,10):0,p&&(u=-u),(-1===r||isNaN(r))&&(r=Math.floor(Math.log(u)/Math.log(s)))<0&&(r=0),r>8&&(j>0&&(j+=8-r),r=8),"exponent"===h)return r;if(0===u)w[0]=0,y=w[1]=M?"":t[x][b?"bits":"bytes"][r];else{P=u/(2===a?Math.pow(2,10*r):Math.pow(1e3,r)),b&&(P*=8)>=s&&r<8&&(P/=s,r++);var k=Math.pow(10,r>0?g:0);w[0]=T(P*k)/k,w[0]===s&&r<8&&void 0===E.exponent&&(w[0]=1,r++),y=w[1]=10===a&&1===r?b?"kbit":"kB":t[x][b?"bits":"bytes"][r],M&&(w[1]=w[1].charAt(0),i.test(w[1])&&(w[0]=Math.floor(w[0]),w[1]=""))}if(p&&(w[0]=-w[0]),j>0&&(w[0]=w[0].toPrecision(j)),w[1]=N[w[1]]||w[1],!0===f?w[0]=w[0].toLocaleString():f.length>0?w[0]=w[0].toLocaleString(f,d):m.length>0&&(w[0]=w[0].toString().replace(".",m)),B&&!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 l&&(w[1]=c[r]?c[r]:e[x][r]+(b?"bit":"byte")+(1===w[0]?"":"s")),"array"===h?w:"object"===h?{value:w[0],symbol:w[1],exponent:r,unit:y}:w.join(v)}return n.partial=function(i){return function(t){return n(t,i)}},n})); +!function(i,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(i="undefined"!=typeof globalThis?globalThis:i||self).filesize=t()}(this,(function(){"use strict";var i={iec:{bits:["bit","Kibit","Mibit","Gibit","Tibit","Pibit","Eibit","Zibit","Yibit"],bytes:["B","KiB","MiB","GiB","TiB","PiB","EiB","ZiB","YiB"]},jedec:{bits:["bit","Kbit","Mbit","Gbit","Tbit","Pbit","Ebit","Zbit","Ybit"],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"]},e={floor:Math.floor,ceil:Math.ceil};function o(o){var n,r,a,b,s,l,c,d,f,p,u,g,h,B,y,M,m,v,x,N,T=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},E=[],j=0;if(isNaN(o))throw new TypeError("Invalid number");if(a=!0===T.bits,g=!0===T.pad,r=T.base||10,h=void 0!==T.round?T.round:2,c=void 0!==T.locale?T.locale:"",d=T.localeOptions||{},y=void 0!==T.separator?T.separator:"",M=void 0!==T.spacer?T.spacer:" ",v=T.symbols||{},m=T.standard in i?T.standard:"iec",u=T.output||"string",s=!0===T.fullform,l=T.fullforms instanceof Array?T.fullforms:[],n=void 0!==T.exponent?T.exponent:-1,x=e[T.roundingMethod]||Math.round,f=(p=Number(o))<0,b=r>2?1e3:1024,N=!1===isNaN(T.precision)?parseInt(T.precision,10):0,f&&(p=-p),(-1===n||isNaN(n))&&(n=Math.floor(Math.log(p)/Math.log(b)))<0&&(n=0),n>8&&(N>0&&(N+=8-n),n=8),"exponent"===u)return n;if(0===p)E[0]=0,B=E[1]=i[m][a?"bits":"bytes"][n];else{j=p/(2===r?Math.pow(2,10*n):Math.pow(1e3,n)),a&&(j*=8)>=b&&n<8&&(j/=b,n++);var w=Math.pow(10,n>0?h:0);E[0]=x(j*w)/w,E[0]===b&&n<8&&void 0===T.exponent&&(E[0]=1,n++),B=E[1]=i[m][a?"bits":"bytes"][n]}if(f&&(E[0]=-E[0]),N>0&&(E[0]=E[0].toPrecision(N)),E[1]=v[E[1]]||E[1],!0===c?E[0]=E[0].toLocaleString():c.length>0?E[0]=E[0].toLocaleString(c,d):y.length>0&&(E[0]=E[0].toString().replace(".",y)),g&&!1===Number.isInteger(E[0])&&h>0){var P=y||".",G=E[0].toString().split(P),K=G[1]||"",S=K.length,Y=h-S;E[0]="".concat(G[0]).concat(P).concat(K.padEnd(S+Y,"0"))}return s&&(E[1]=l[n]?l[n]:t[m][n]+(a?"bit":"byte")+(1===E[0]?"":"s")),"array"===u?E:"object"===u?{value:E[0],symbol:E[1],exponent:n,unit:B}:E.join(M)}return o.partial=function(i){return function(t){return o(t,i)}},o})); //# sourceMappingURL=filesize.min.js.map diff --git a/lib/filesize.min.js.map b/lib/filesize.min.js.map index 2eaf3d8..78c2e59 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)$/,\r\n\tsymbol = {\r\n\t\tiec: {\r\n\t\t\tbits: [\"bit\", \"Kibit\", \"Mibit\", \"Gibit\", \"Tibit\", \"Pibit\", \"Eibit\", \"Zibit\", \"Yibit\"],\r\n\t\t\tbytes: [\"B\", \"KiB\", \"MiB\", \"GiB\", \"TiB\", \"PiB\", \"EiB\", \"ZiB\", \"YiB\"]\r\n\t\t},\r\n\t\tjedec: {\r\n\t\t\tbits: [\"bit\", \"Kbit\", \"Mbit\", \"Gbit\", \"Tbit\", \"Pbit\", \"Ebit\", \"Zbit\", \"Ybit\"],\r\n\t\t\tbytes: [\"B\", \"KB\", \"MB\", \"GB\", \"TB\", \"PB\", \"EB\", \"ZB\", \"YB\"]\r\n\t\t}\r\n\t},\r\n\tfullform = {\r\n\t\tiec: [\"\", \"kibi\", \"mebi\", \"gibi\", \"tebi\", \"pebi\", \"exbi\", \"zebi\", \"yobi\"],\r\n\t\tjedec: [\"\", \"kilo\", \"mega\", \"giga\", \"tera\", \"peta\", \"exa\", \"zetta\", \"yotta\"]\r\n\t},\r\n\troundingFuncs = {\r\n\t\tfloor: Math.floor,\r\n\t\tceil: Math.ceil\r\n\t};\r\n\r\n/**\r\n * filesize\r\n *\r\n * @method filesize\r\n * @param {Mixed} arg String, Int or Float to transform\r\n * @param {Object} descriptor [Optional] Flags\r\n * @return {String} Readable file size String\r\n */\r\nfunction filesize (arg, descriptor = {}) {\r\n\tlet result = [],\r\n\t\tval = 0,\r\n\t\te, base, bits, ceil, full, fullforms, locale, localeOptions, neg, num, output, pad, round, u, unix, separator, spacer, standard, symbols, roundingFunc, precision;\r\n\r\n\tif (isNaN(arg)) {\r\n\t\tthrow new TypeError(\"Invalid number\");\r\n\t}\r\n\r\n\tbits = descriptor.bits === true;\r\n\tunix = descriptor.unix === true;\r\n\tpad = descriptor.pad === true;\r\n\tbase = descriptor.base || 10;\r\n\tround = descriptor.round !== void 0 ? descriptor.round : unix ? 1 : 2;\r\n\tlocale = descriptor.locale !== void 0 ? descriptor.locale : \"\";\r\n\tlocaleOptions = descriptor.localeOptions || {};\r\n\tseparator = descriptor.separator !== void 0 ? descriptor.separator : \"\";\r\n\tspacer = descriptor.spacer !== void 0 ? descriptor.spacer : unix ? \"\" : \" \";\r\n\tsymbols = descriptor.symbols || {};\r\n\tstandard = base === 2 ? descriptor.standard || \"iec\" : \"jedec\";\r\n\toutput = descriptor.output || \"string\";\r\n\tfull = descriptor.fullform === true;\r\n\tfullforms = descriptor.fullforms instanceof Array ? descriptor.fullforms : [];\r\n\te = descriptor.exponent !== void 0 ? descriptor.exponent : -1;\r\n\troundingFunc = roundingFuncs[descriptor.roundingMethod] || Math.round;\r\n\tnum = Number(arg);\r\n\tneg = num < 0;\r\n\tceil = base > 2 ? 1000 : 1024;\r\n\tprecision = isNaN(descriptor.precision) === false ? parseInt(descriptor.precision, 10) : 0;\r\n\r\n\t// Flipping a negative number to determine the size\r\n\tif (neg) {\r\n\t\tnum = -num;\r\n\t}\r\n\r\n\t// Determining the exponent\r\n\tif (e === -1 || isNaN(e)) {\r\n\t\te = Math.floor(Math.log(num) / Math.log(ceil));\r\n\r\n\t\tif (e < 0) {\r\n\t\t\te = 0;\r\n\t\t}\r\n\t}\r\n\r\n\t// Exceeding supported length, time to reduce & multiply\r\n\tif (e > 8) {\r\n\t\tif (precision > 0) {\r\n\t\t\tprecision += 8 - e;\r\n\t\t}\r\n\r\n\t\te = 8;\r\n\t}\r\n\r\n\tif (output === \"exponent\") {\r\n\t\treturn e;\r\n\t}\r\n\r\n\t// Zero is now a special case because bytes divide by 1\r\n\tif (num === 0) {\r\n\t\tresult[0] = 0;\r\n\t\tu = result[1] = unix ? \"\" : symbol[standard][bits ? \"bits\" : \"bytes\"][e];\r\n\t} else {\r\n\t\tval = num / (base === 2 ? Math.pow(2, e * 10) : Math.pow(1000, e));\r\n\r\n\t\tif (bits) {\r\n\t\t\tval = val * 8;\r\n\r\n\t\t\tif (val >= ceil && e < 8) {\r\n\t\t\t\tval = val / ceil;\r\n\t\t\t\te++;\r\n\t\t\t}\r\n\t\t}\r\n\r\n\t\tconst p = Math.pow(10, e > 0 ? round : 0);\r\n\t\tresult[0] = roundingFunc(val * p) / p;\r\n\r\n\t\tif (result[0] === ceil && e < 8 && descriptor.exponent === void 0) {\r\n\t\t\tresult[0] = 1;\r\n\t\t\te++;\r\n\t\t}\r\n\r\n\t\tu = result[1] = base === 10 && e === 1 ? bits ? \"kbit\" : \"kB\" : symbol[standard][bits ? \"bits\" : \"bytes\"][e];\r\n\r\n\t\tif (unix) {\r\n\t\t\tresult[1] = result[1].charAt(0);\r\n\r\n\t\t\tif (b.test(result[1])) {\r\n\t\t\t\tresult[0] = Math.floor(result[0]);\r\n\t\t\t\tresult[1] = \"\";\r\n\t\t\t}\r\n\t\t}\r\n\t}\r\n\r\n\t// Decorating a 'diff'\r\n\tif (neg) {\r\n\t\tresult[0] = -result[0];\r\n\t}\r\n\r\n\t// Setting optional precision\r\n\tif (precision > 0) {\r\n\t\tresult[0] = result[0].toPrecision(precision);\r\n\t}\r\n\r\n\t// Applying custom symbol\r\n\tresult[1] = symbols[result[1]] || result[1];\r\n\r\n\tif (locale === true) {\r\n\t\tresult[0] = result[0].toLocaleString();\r\n\t} else if (locale.length > 0) {\r\n\t\tresult[0] = result[0].toLocaleString(locale, localeOptions);\r\n\t} else if (separator.length > 0) {\r\n\t\tresult[0] = result[0].toString().replace(\".\", separator);\r\n\t}\r\n\r\n\tif (pad && Number.isInteger(result[0]) === false && round > 0) {\r\n\t\tconst x = separator || \".\",\r\n\t\t\ttmp = result[0].toString().split(x),\r\n\t\t\ts = tmp[1] || \"\",\r\n\t\t\tl = s.length,\r\n\t\t\tn = round - l;\r\n\r\n\t\tresult[0] = `${tmp[0]}${x}${s.padEnd(l + n, \"0\")}`;\r\n\t}\r\n\r\n\tif (full) {\r\n\t\tresult[1] = fullforms[e] ? fullforms[e] : fullform[standard][e] + (bits ? \"bit\" : \"byte\") + (result[0] === 1 ? \"\" : \"s\");\r\n\t}\r\n\r\n\t// Returning Array, Object, or String (default)\r\n\treturn output === \"array\" ? result : output === \"object\" ? {value: result[0], symbol: result[1], exponent: e, unit: u} : result.join(spacer);\r\n}\r\n\r\n// Partial application for functional programming\r\nfilesize.partial = opt => arg => filesize(arg, opt);\r\n\r\nexport default filesize;\r\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","test","toPrecision","toLocaleString","length","toString","replace","isInteger","x","tmp","split","s","l","n","padEnd","value","unit","join","partial","opt"],"mappings":";;;;yOAAA,IAAMA,EAAI,UACTC,EAAS,CACRC,IAAK,CACJC,KAAM,CAAC,MAAO,QAAS,QAAS,QAAS,QAAS,QAAS,QAAS,QAAS,SAC7EC,MAAO,CAAC,IAAK,MAAO,MAAO,MAAO,MAAO,MAAO,MAAO,MAAO,QAE/DC,MAAO,CACNF,KAAM,CAAC,MAAO,OAAQ,OAAQ,OAAQ,OAAQ,OAAQ,OAAQ,OAAQ,QACtEC,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,EAAMO,EAAMK,EAAMC,EAAWC,EAAQC,EAAeC,EAAKC,EAAKC,EAAQC,EAAKC,EAAOC,EAAGC,EAAMC,EAAWC,EAAQC,EAAUC,EAASC,EAAcC,EAHlIC,yDAAa,GAChCC,EAAS,GACZC,EAAM,KAGHC,MAAMvB,SACH,IAAIwB,UAAU,qBAGrBjC,GAA2B,IAApB6B,EAAW7B,KAClBsB,GAA2B,IAApBO,EAAWP,KAClBH,GAAyB,IAAnBU,EAAWV,IACjBR,EAAOkB,EAAWlB,MAAQ,GAC1BS,OAA6B,IAArBS,EAAWT,M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AAGgD,KAAMrC,GAAKS,EAAO6B,KAAKnC,UAItIhB,EAASoD,QAAU,SAAAC,UAAO,SAAApD,UAAOD,EAASC,EAAKoD"} \ No newline at end of file +{"version":3,"file":"filesize.min.js","sources":["../src/filesize.js"],"sourcesContent":["const symbol = {\n\t\tiec: {\n\t\t\tbits: [\"bit\", \"Kibit\", \"Mibit\", \"Gibit\", \"Tibit\", \"Pibit\", \"Eibit\", \"Zibit\", \"Yibit\"],\n\t\t\tbytes: [\"B\", \"KiB\", \"MiB\", \"GiB\", \"TiB\", \"PiB\", \"EiB\", \"ZiB\", \"YiB\"]\n\t\t},\n\t\tjedec: {\n\t\t\tbits: [\"bit\", \"Kbit\", \"Mbit\", \"Gbit\", \"Tbit\", \"Pbit\", \"Ebit\", \"Zbit\", \"Ybit\"],\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, 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\tpad = descriptor.pad === true;\n\tbase = descriptor.base || 10;\n\tround = descriptor.round !== void 0 ? descriptor.round : 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 : \" \";\n\tsymbols = descriptor.symbols || {};\n\tstandard = descriptor.standard in symbol ? descriptor.standard : \"iec\";\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] = 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] = symbol[standard][bits ? \"bits\" : \"bytes\"][e];\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":["symbol","iec","bits","bytes","jedec","fullform","roundingFuncs","floor","Math","ceil","filesize","arg","e","base","full","fullforms","locale","localeOptions","neg","num","output","pad","round","u","separator","spacer","standard","symbols","roundingFunc","precision","descriptor","result","val","isNaN","TypeError","Array","exponent","roundingMethod","Number","parseInt","log","pow","p","toPrecision","toLocaleString","length","toString","replace","isInteger","x","tmp","split","s","l","n","padEnd","value","unit","join","partial","opt"],"mappings":";;;;yOAAA,IAAMA,E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AGa,iBACZ5B,EAAO6B,OAAS,EAC1Bd,EAAO,GAAKA,EAAO,GAAGa,eAAe5B,EAAQC,GACnCO,EAAUqB,OAAS,IAC7Bd,EAAO,GAAKA,EAAO,GAAGe,WAAWC,QAAQ,IAAKvB,IAG3CH,IAAuC,IAAhCiB,OAAOU,UAAUjB,EAAO,KAAiBT,EAAQ,EAAG,KACxD2B,EAAIzB,GAAa,IACtB0B,EAAMnB,EAAO,GAAGe,WAAWK,MAAMF,GACjCG,EAAIF,EAAI,IAAM,GACdG,EAAID,EAAEP,OACNS,EAAIhC,EAAQ+B,EAEbtB,EAAO,aAAQmB,EAAI,WAAKD,UAAIG,EAAEG,OAAOF,EAAIC,EAAG,aAGzCxC,IACHiB,EAAO,GAAKhB,EAAUH,GAAKG,EAAUH,GAAKP,EAASqB,GAAUd,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,KAAKjC,UAItIf,EAASiD,QAAU,SAAAC,UAAO,SAAAjD,UAAOD,EAASC,EAAKiD"} \ No newline at end of file diff --git a/src/filesize.js b/src/filesize.js index fc81f80..92557a8 100644 --- a/src/filesize.js +++ b/src/filesize.js @@ -1,5 +1,4 @@ -const b = /^(b|B)$/, - symbol = { +const symbol = { iec: { bits: ["bit", "Kibit", "Mibit", "Gibit", "Tibit", "Pibit", "Eibit", "Zibit", "Yibit"], bytes: ["B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB"] @@ -29,23 +28,22 @@ 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, precision; + e, base, bits, ceil, full, fullforms, locale, localeOptions, neg, num, output, pad, round, u, separator, spacer, standard, symbols, roundingFunc, precision; if (isNaN(arg)) { throw new TypeError("Invalid number"); } bits = descriptor.bits === true; - unix = descriptor.unix === true; pad = descriptor.pad === true; base = descriptor.base || 10; - round = descriptor.round !== void 0 ? descriptor.round : unix ? 1 : 2; + round = descriptor.round !== void 0 ? descriptor.round : 2; locale = descriptor.locale !== void 0 ? descriptor.locale : ""; localeOptions = descriptor.localeOptions || {}; separator = descriptor.separator !== void 0 ? descriptor.separator : ""; - spacer = descriptor.spacer !== void 0 ? descriptor.spacer : unix ? "" : " "; + spacer = descriptor.spacer !== void 0 ? descriptor.spacer : " "; symbols = descriptor.symbols || {}; - standard = base === 2 ? descriptor.standard || "iec" : "jedec"; + standard = descriptor.standard in symbol ? descriptor.standard : "iec"; output = descriptor.output || "string"; full = descriptor.fullform === true; fullforms = descriptor.fullforms instanceof Array ? descriptor.fullforms : []; @@ -86,7 +84,7 @@ function filesize (arg, descriptor = {}) { // Zero is now a special case because bytes divide by 1 if (num === 0) { result[0] = 0; - u = result[1] = unix ? "" : symbol[standard][bits ? "bits" : "bytes"][e]; + u = result[1] = symbol[standard][bits ? "bits" : "bytes"][e]; } else { val = num / (base === 2 ? Math.pow(2, e * 10) : Math.pow(1000, e)); @@ -107,16 +105,7 @@ function filesize (arg, descriptor = {}) { e++; } - u = result[1] = base === 10 && e === 1 ? bits ? "kbit" : "kB" : symbol[standard][bits ? "bits" : "bytes"][e]; - - if (unix) { - result[1] = result[1].charAt(0); - - if (b.test(result[1])) { - result[0] = Math.floor(result[0]); - result[1] = ""; - } - } + u = result[1] = symbol[standard][bits ? "bits" : "bytes"][e]; } // Decorating a 'diff' diff --git a/test/filesize_test.js b/test/filesize_test.js index 844c2d9..f96145d 100644 --- a/test/filesize_test.js +++ b/test/filesize_test.js @@ -16,95 +16,77 @@ exports.filesize = { done(); }, base2: function (test) { - test.expect(48); - test.equal(filesize(this.kilobit, {base: 2, standard: "iec"}), "500 B", "Should be '500 B'"); - test.equal(filesize(this.kilobit, {base: 2, standard: "iec", round: 1}), "500 B", "Should be '500 B'"); - test.equal(filesize(this.kilobit, {base: 2, standard: "iec", round: 1, spacer: ""}), "500B", "Should be '500B'"); - test.equal(filesize(this.kilobit, {base: 2, standard: "iec", unix: true}), "500", "Should be '500'"); - test.equal(filesize(this.kilobit, {base: 2, standard: "iec", round: 1, bits: true}), "3.9 Kibit", "Should be '3.9 Kibit'"); - test.equal(filesize(this.kilobit, {base: 2, standard: "iec", bits: true}), "3.91 Kibit", "Should be '3.91 Kibit'"); - test.equal(filesize(this.kilobit, {base: 2, standard: "iec", unix: true, bits: true}), "3.9K", "Should be '3.9K'"); - test.equal(filesize(this.kilobit, {base: 2, standard: "iec", bits: true, output: "array"})[0], 3.91, "Should be '3.91'"); - test.equal(filesize(this.kilobit, {base: 2, standard: "iec", bits: true, output: "object"}).value, 3.91, "Should be '3.91'"); - test.equal(filesize(this.edgecase, {base: 2, standard: "iec"}), "1023 B", "Should be '1023 B'"); - test.equal(filesize(this.edgecase, {base: 2, standard: "iec", round: 1}), "1023 B", "Should be '1023 B'"); - test.equal(filesize(this.kibibyte, {base: 2, standard: "iec"}), "1 KiB", "Should be '1 KiB'"); - test.equal(filesize(this.kibibyte, {base: 2, standard: "iec", round: 1}), "1 KiB", "Should be '1 KiB'"); - test.equal(filesize(this.kibibyte, {base: 2, standard: "iec", round: 1, spacer: ""}), "1KiB", "Should be '1KiB'"); - test.equal(filesize(this.kibibyte, {base: 2, standard: "iec", unix: true}), "1K", "Should be '1K'"); - test.equal(filesize(this.kibibyte, {base: 2, standard: "iec", bits: true}), "8 Kibit", "Should be '8 Kibit'"); - test.equal(filesize(this.kibibyte, {base: 2, standard: "iec", round: 1, bits: true}), "8 Kibit", "Should be '8 Kibit'"); - test.equal(filesize(this.kibibyte, {base: 2, standard: "iec", unix: true, bits: true}), "8K", "Should be '8K'"); - test.equal(filesize(this.kibibyte, {base: 2, standard: "iec", exponent: 0}), "1024 B", "Should be '1024 B'"); - test.equal(filesize(this.kibibyte, {base: 2, standard: "iec", exponent: 0, output: "object"}).unit, "B", "Should be 'B'"); - test.equal(filesize(this.kibibyte, {base: 2, standard: "iec", output: "exponent"}), 1, "Should be '1'"); - test.equal(filesize(this.kibibyte, {base: 2, standard: "iec", output: "object"}).unit, "KiB", "Should be 'KiB'"); - test.equal(filesize(this.neg, {base: 2, standard: "iec"}), "-1 KiB", "Should be '-1 KiB'"); - test.equal(filesize(this.neg, {base: 2, standard: "iec", round: 1}), "-1 KiB", "Should be '-1 KiB'"); - test.equal(filesize(this.neg, {base: 2, standard: "iec", round: 1, spacer: ""}), "-1KiB", "Should be '-1KiB'"); - test.equal(filesize(this.neg, {base: 2, standard: "iec", unix: true}), "-1K", "Should be '-1K'"); - test.equal(filesize(this.neg, {base: 2, standard: "iec", bits: true}), "-8 Kibit", "Should be '-8 Kibit'"); - test.equal(filesize(this.neg, {base: 2, standard: "iec", round: 1, bits: true}), "-8 Kibit", "Should be '-8 Kibit'"); - test.equal(filesize(this.neg, {base: 2, standard: "iec", unix: true, bits: true}), "-8K", "Should be '-8K'"); - test.equal(filesize(this.byte, {base: 2, standard: "iec"}), "1 B", "Should be '1 B'"); - test.equal(filesize(this.byte, {base: 2, standard: "iec", round: 1}), "1 B", "Should be '1 B'"); - test.equal(filesize(this.byte, {base: 2, standard: "iec", round: 1, spacer: ""}), "1B", "Should be '1B'"); - test.equal(filesize(this.byte, {base: 2, standard: "iec", unix: true}), "1", "Should be '1'"); - test.equal(filesize(this.byte, {base: 2, standard: "iec", bits: true}), "8 bit", "Should be '8 bit'"); - test.equal(filesize(this.byte, {base: 2, standard: "iec", round: 1, bits: true}), "8 bit", "Should be '8 bit'"); - test.equal(filesize(this.byte, {base: 2, standard: "iec", unix: true, bits: true}), "8", "Should be '8'"); - test.equal(filesize(this.zero, {base: 2, standard: "iec"}), "0 B", "Should be '0 B'"); - test.equal(filesize(this.zero, {base: 2, standard: "iec", round: 1}), "0 B", "Should be '0 B'"); - test.equal(filesize(this.zero, {base: 2, standard: "iec", round: 1, spacer: ""}), "0B", "Should be '0B'"); - test.equal(filesize(this.zero, {base: 2, standard: "iec", unix: true}), "0", "Should be '0'"); - test.equal(filesize(this.zero, {base: 2, standard: "iec", bits: true}), "0 bit", "Should be '0 bit'"); - test.equal(filesize(this.zero, {base: 2, standard: "iec", round: 1, bits: true}), "0 bit", "Should be '0 bit'"); - test.equal(filesize(this.zero, {base: 2, standard: "iec", unix: true, bits: true}), "0", "Should be '0'"); - test.equal(filesize(this.huge, {base: 2, standard: "iec"}), "82718061255302770 YiB", "Should be '82718061255302770 YiB'"); - test.equal(filesize(this.huge, {base: 2, standard: "iec", bits: true}), "661744490042422100 Yibit", "Should be '661744490042422100 Yibit'"); - test.equal(filesize(this.small, {base: 2, standard: "iec"}), "0 B", "Should be '0 B'"); - test.equal(filesize(this.small, {base: 2, standard: "iec", bits: true}), "1 bit", "Should be '1 bit'"); - test.equal(filesize(this.petabyte, {base: 2, standard: "iec"}), "1 PiB", "Should be '1 PiB'"); + test.expect(40); + test.equal(filesize(this.kilobit, {base: 2}), "500 B", "Should be '500 B'"); + test.equal(filesize(this.kilobit, {base: 2, round: 1}), "500 B", "Should be '500 B'"); + test.equal(filesize(this.kilobit, {base: 2, round: 1, spacer: ""}), "500B", "Should be '500B'"); + test.equal(filesize(this.kilobit, {base: 2, round: 1, bits: true}), "3.9 Kibit", "Should be '3.9 Kibit'"); + test.equal(filesize(this.kilobit, {base: 2, bits: true}), "3.91 Kibit", "Should be '3.91 Kibit'"); + test.equal(filesize(this.kilobit, {base: 2, bits: true, output: "array"})[0], 3.91, "Should be '3.91'"); + test.equal(filesize(this.kilobit, {base: 2, bits: true, output: "object"}).value, 3.91, "Should be '3.91'"); + test.equal(filesize(this.edgecase, {base: 2}), "1023 B", "Should be '1023 B'"); + test.equal(filesize(this.edgecase, {base: 2, round: 1}), "1023 B", "Should be '1023 B'"); + test.equal(filesize(this.kibibyte, {base: 2}), "1 KiB", "Should be '1 KiB'"); + test.equal(filesize(this.kibibyte, {base: 2, standard: "jedec"}), "1 KB", "Should be '1 KB'"); + test.equal(filesize(this.kibibyte, {base: 2, standard: "invalid"}), "1 KiB", "Should be '1 KiB'"); + test.equal(filesize(this.kibibyte, {base: 2, round: 1}), "1 KiB", "Should be '1 KiB'"); + test.equal(filesize(this.kibibyte, {base: 2, round: 1, spacer: ""}), "1KiB", "Should be '1KiB'"); + test.equal(filesize(this.kibibyte, {base: 2, bits: true}), "8 Kibit", "Should be '8 Kibit'"); + test.equal(filesize(this.kibibyte, {base: 2, round: 1, bits: true}), "8 Kibit", "Should be '8 Kibit'"); + test.equal(filesize(this.kibibyte, {base: 2, exponent: 0}), "1024 B", "Should be '1024 B'"); + test.equal(filesize(this.kibibyte, {base: 2, exponent: 0, output: "object"}).unit, "B", "Should be 'B'"); + test.equal(filesize(this.kibibyte, {base: 2, output: "exponent"}), 1, "Should be '1'"); + test.equal(filesize(this.kibibyte, {base: 2, output: "object"}).unit, "KiB", "Should be 'KiB'"); + test.equal(filesize(this.neg, {base: 2}), "-1 KiB", "Should be '-1 KiB'"); + test.equal(filesize(this.neg, {base: 2, round: 1}), "-1 KiB", "Should be '-1 KiB'"); + test.equal(filesize(this.neg, {base: 2, round: 1, spacer: ""}), "-1KiB", "Should be '-1KiB'"); + test.equal(filesize(this.neg, {base: 2, bits: true}), "-8 Kibit", "Should be '-8 Kibit'"); + test.equal(filesize(this.neg, {base: 2, round: 1, bits: true}), "-8 Kibit", "Should be '-8 Kibit'"); + test.equal(filesize(this.byte, {base: 2}), "1 B", "Should be '1 B'"); + test.equal(filesize(this.byte, {base: 2, round: 1}), "1 B", "Should be '1 B'"); + test.equal(filesize(this.byte, {base: 2, round: 1, spacer: ""}), "1B", "Should be '1B'"); + test.equal(filesize(this.byte, {base: 2, bits: true}), "8 bit", "Should be '8 bit'"); + test.equal(filesize(this.byte, {base: 2, round: 1, bits: true}), "8 bit", "Should be '8 bit'"); + test.equal(filesize(this.zero, {base: 2}), "0 B", "Should be '0 B'"); + test.equal(filesize(this.zero, {base: 2, round: 1}), "0 B", "Should be '0 B'"); + test.equal(filesize(this.zero, {base: 2, round: 1, spacer: ""}), "0B", "Should be '0B'"); + test.equal(filesize(this.zero, {base: 2, bits: true}), "0 bit", "Should be '0 bit'"); + test.equal(filesize(this.zero, {base: 2, round: 1, bits: true}), "0 bit", "Should be '0 bit'"); + test.equal(filesize(this.huge, {base: 2}), "82718061255302770 YiB", "Should be '82718061255302770 YiB'"); + test.equal(filesize(this.huge, {base: 2, bits: true}), "661744490042422100 Yibit", "Should be '661744490042422100 Yibit'"); + test.equal(filesize(this.small, {base: 2}), "0 B", "Should be '0 B'"); + test.equal(filesize(this.small, {base: 2, bits: true}), "1 bit", "Should be '1 bit'"); + test.equal(filesize(this.petabyte, {base: 2}), "1 PiB", "Should be '1 PiB'"); test.done(); }, base10: function (test) { - test.expect(35); + test.expect(25); test.equal(filesize(this.kilobit, {base: 10}), "500 B", "Should be '500 B'"); test.equal(filesize(this.kilobit, {base: 10, round: 1}), "500 B", "Should be '500 B'"); test.equal(filesize(this.kilobit, {base: 10, round: 1, spacer: ""}), "500B", "Should be '500B'"); - test.equal(filesize(this.kilobit, {base: 10, unix: true}), "500", "Should be '500'"); - test.equal(filesize(this.kilobit, {base: 10, bits: true}), "4 kbit", "Should be '4 kbit'"); - test.equal(filesize(this.kilobit, {base: 10, round: 1, bits: true}), "4 kbit", "Should be '4 kbit'"); - test.equal(filesize(this.kilobit, {base: 10, unix: true, bits: true}), "4k", "Should be '4k'"); - test.equal(filesize(this.kibibyte, {base: 10}), "1.02 kB", "Should be '1.02 kB'"); - test.equal(filesize(this.kibibyte, {base: 10, round: 1}), "1 kB", "Should be '1 kB'"); - test.equal(filesize(this.kibibyte, {base: 10, round: 1, spacer: ""}), "1kB", "Should be '1kB'"); - test.equal(filesize(this.kibibyte, {base: 10, unix: true}), "1k", "Should be '1k'"); - test.equal(filesize(this.kibibyte, {base: 10, bits: true}), "8.19 kbit", "Should be '8.19 kbit'"); - test.equal(filesize(this.kibibyte, {base: 10, round: 1, bits: true}), "8.2 kbit", "Should be '8.2 kbit'"); - test.equal(filesize(this.kibibyte, {base: 10, unix: true, bits: true}), "8.2k", "Should be '8.2k'"); - test.equal(filesize(this.neg, {base: 10}), "-1.02 kB", "Should be '-1.02 kB'"); - test.equal(filesize(this.neg, {base: 10, round: 1}), "-1 kB", "Should be '-1 kB'"); - test.equal(filesize(this.neg, {base: 10, round: 1, spacer: ""}), "-1kB", "Should be '-1kB'"); - test.equal(filesize(this.neg, {base: 10, unix: true}), "-1k", "Should be '-1k'"); - test.equal(filesize(this.neg, {base: 10, bits: true}), "-8.19 kbit", "Should be '-8.19 kbit'"); - test.equal(filesize(this.neg, {base: 10, round: 1, bits: true}), "-8.2 kbit", "Should be '-8.2 kbit'"); - test.equal(filesize(this.neg, {base: 10, unix: true, bits: true}), "-8.2k", "Should be '-8.2k'"); + test.equal(filesize(this.kilobit, {base: 10, bits: true}), "4 Kibit", "Should be '4 Kibit'"); + test.equal(filesize(this.kilobit, {base: 10, round: 1, bits: true}), "4 Kibit", "Should be '4 Kibit'"); + test.equal(filesize(this.kibibyte, {base: 10}), "1.02 KiB", "Should be '1.02 KiB'"); + test.equal(filesize(this.kibibyte, {base: 10, round: 1}), "1 KiB", "Should be '1 KiB'"); + test.equal(filesize(this.kibibyte, {base: 10, round: 1, spacer: ""}), "1KiB", "Should be '1KiB'"); + test.equal(filesize(this.kibibyte, {base: 10, bits: true}), "8.19 Kibit", "Should be '8.19 Kibit'"); + test.equal(filesize(this.kibibyte, {base: 10, round: 1, bits: true}), "8.2 Kibit", "Should be '8.2 Kibit'"); + test.equal(filesize(this.neg, {base: 10}), "-1.02 KiB", "Should be '-1.02 KiB'"); + test.equal(filesize(this.neg, {base: 10, round: 1}), "-1 KiB", "Should be '-1 KiB'"); + test.equal(filesize(this.neg, {base: 10, round: 1, spacer: ""}), "-1KiB", "Should be '-1KiB'"); + test.equal(filesize(this.neg, {base: 10, bits: true}), "-8.19 Kibit", "Should be '-8.19 Kibit'"); + test.equal(filesize(this.neg, {base: 10, round: 1, bits: true}), "-8.2 Kibit", "Should be '-8.2 Kibit'"); test.equal(filesize(this.byte, {base: 10}), "1 B", "Should be '1 B'"); test.equal(filesize(this.byte, {base: 10, round: 1}), "1 B", "Should be '1 B'"); test.equal(filesize(this.byte, {base: 10, round: 1, spacer: ""}), "1B", "Should be '1B'"); - test.equal(filesize(this.byte, {base: 10, unix: true}), "1", "Should be '1'"); test.equal(filesize(this.byte, {base: 10, bits: true}), "8 bit", "Should be '8 bit'"); test.equal(filesize(this.byte, {base: 10, round: 1, bits: true}), "8 bit", "Should be '8 bit'"); - test.equal(filesize(this.byte, {base: 10, unix: true, bits: true}), "8", "Should be '8'"); test.equal(filesize(this.zero, {base: 10}), "0 B", "Should be '0 B'"); test.equal(filesize(this.zero, {base: 10, round: 1}), "0 B", "Should be '0 B'"); test.equal(filesize(this.zero, {base: 10, round: 1, spacer: ""}), "0B", "Should be '0B'"); - test.equal(filesize(this.zero, {base: 10, unix: true}), "0", "Should be '0'"); test.equal(filesize(this.zero, {base: 10, bits: true}), "0 bit", "Should be '0 bit'"); test.equal(filesize(this.zero, {base: 10, round: 1, bits: true}), "0 bit", "Should be '0 bit'"); - test.equal(filesize(this.zero, {base: 10, unix: true, bits: true}), "0", "Should be '0'"); test.done(); }, invalid: function (test) { @@ -117,7 +99,7 @@ exports.filesize = { symbols: function (test) { test.expect(2); test.equal(filesize(this.byte, {base: 10, symbols: {B: "Б"}}), "1 Б", "Should be '1 Б'"); - test.equal(filesize(this.kibibyte, {base: 10, symbols: {B: "Б"}}), "1.02 kB", "Should be '1.02 kB'"); + test.equal(filesize(this.kibibyte, {base: 10, symbols: {B: "Б"}}), "1.02 KiB", "Should be '1.02 KiB'"); test.done(); }, partial: function (test) { @@ -129,19 +111,20 @@ exports.filesize = { bits: function (test) { test.expect(3); test.equal(filesize(124, {bits: true, base: 10}), "992 bit", "Should be '992 bit'"); - test.equal(filesize(125, {bits: true, base: 10}), "1 kbit", "Should be '1 kbit'"); - test.equal(filesize(126, {bits: true, base: 10}), "1.01 kbit", "Should be '1.01 kbit'"); + test.equal(filesize(125, {bits: true, base: 10}), "1 Kibit", "Should be '1 Kibit'"); + test.equal(filesize(126, {bits: true, base: 10}), "1.01 Kibit", "Should be '1.01 Kibit'"); test.done(); }, fullform: function (test) { - test.expect(9); + test.expect(10); test.equal(filesize(0, {base: 10, fullform: true}), "0 bytes", "Should be '0 bytes'"); test.equal(filesize(0, {base: 10, fullform: true, output: "object"}).unit, "B", "Should be 'B'"); test.equal(filesize(1, {base: 10, bits: true, fullform: true}), "8 bits", "Should be '8 bits'"); test.equal(filesize(1, {base: 10, fullform: true}), "1 byte", "Should be '1 byte'"); - test.equal(filesize(this.kibibyte, {base: 10, fullform: true}), "1.02 kilobytes", "Should be '1.02 kilobytes'"); - test.equal(filesize(this.kibibyte, {base: 2, standard: "iec", fullform: true }), "1 kibibyte", "Should be '1 kibibyte'"); - test.equal(filesize(this.kibibyte, {base: 2, standard: "iec", fullform: true, output: "object"}).unit, "KiB", "Should be 'KiB'"); + test.equal(filesize(this.kibibyte, {base: 10, standard: "jedec", fullform: true}), "1.02 kilobytes", "Should be '1.02 kilobytes'"); + test.equal(filesize(this.kibibyte, {base: 10, fullform: true}), "1.02 kibibytes", "Should be '1.02 kibibytes'"); + test.equal(filesize(this.kibibyte, {base: 2, fullform: true }), "1 kibibyte", "Should be '1 kibibyte'"); + test.equal(filesize(this.kibibyte, {base: 2, fullform: true, output: "object"}).unit, "KiB", "Should be 'KiB'"); test.equal(filesize(this.kibibyte * 1.3, { base: 2, standard: "iec", @@ -153,64 +136,64 @@ exports.filesize = { exponent: function (test) { test.expect(2); test.equal(filesize(0, {base: 10, exponent: 0}), "0 B", "Should be '0 B'"); - test.equal(filesize(0, {base: 10, exponent: 2}), "0 MB", "Should be '0 MB'"); + test.equal(filesize(0, {base: 10, exponent: 2}), "0 MiB", "Should be '0 MiB'"); test.done(); }, separator: function (test) { test.expect(3); - test.equal(filesize(1040, {base: 10, separator: ""}), "1.04 kB", "Should be '1.04 kB'"); - test.equal(filesize(1040, {base: 10, separator: ","}), "1,04 kB", "Should be '1,04 kB'"); - test.equal(filesize(1040, {base: 10, separator: ",", round: 1, pad: true}), "1,0 kB", "Should be '1,0 kB'"); + test.equal(filesize(1040, {base: 10, separator: ""}), "1.04 KiB", "Should be '1.04 KiB'"); + test.equal(filesize(1040, {base: 10, separator: ","}), "1,04 KiB", "Should be '1,04 KiB'"); + test.equal(filesize(1040, {base: 10, separator: ",", round: 1, pad: true}), "1,0 KiB", "Should be '1,0 KiB'"); test.done(); }, locale: function (test) { test.expect(3); - test.equal(filesize(1040, {base: 10, locale: ""}), "1.04 kB", "Should be '1.04 kB'"); - test.equal(filesize(1040, {base: 10, locale: true}), Number(1.04).toLocaleString() + " kB", "Should be '" + Number(1.04).toLocaleString() + " kB'"); - test.equal(filesize(1040, {base: 10, locale: "de"}), Number(1.04).toLocaleString("de") + " kB", "Should be '" + Number(1.04).toLocaleString("de") + " kB'"); + test.equal(filesize(1040, {base: 10, locale: ""}), "1.04 KiB", "Should be '1.04 KiB'"); + test.equal(filesize(1040, {base: 10, locale: true}), Number(1.04).toLocaleString() + " KiB", "Should be '" + Number(1.04).toLocaleString() + " KiB'"); + test.equal(filesize(1040, {base: 10, locale: "de"}), Number(1.04).toLocaleString("de") + " KiB", "Should be '" + Number(1.04).toLocaleString("de") + " KiB'"); test.done(); }, localeOptions: function (test) { test.expect(4); - test.equal(filesize(this.kibibyte, {base: 10, locale: "de"}), Number(1.02).toLocaleString("de") + " kB", "Should be '" + Number(1.02).toLocaleString("de") + " kB'"); - test.equal(filesize(this.kibibyte, {base: 10, localeOptions: {minimumFractionDigits: 1}}), Number(1.02).toLocaleString(undefined, {minimumFractionDigits: 1}) + " kB", "Should be '" + Number(1.02).toLocaleString(undefined, {minimumFractionDigits: 1}) + " kB'"); + test.equal(filesize(this.kibibyte, {base: 10, locale: "de"}), Number(1.02).toLocaleString("de") + " KiB", "Should be '" + Number(1.02).toLocaleString("de") + " KiB'"); + test.equal(filesize(this.kibibyte, {base: 10, localeOptions: {minimumFractionDigits: 1}}), Number(1.02).toLocaleString(undefined, {minimumFractionDigits: 1}) + " KiB", "Should be '" + Number(1.02).toLocaleString(undefined, {minimumFractionDigits: 1}) + " KiB'"); test.equal(filesize(this.kibibyte, { base: 10, locale: true, localeOptions: {minimumFractionDigits: 1} - }), Number(1.02).toLocaleString(undefined, {minimumFractionDigits: 1}) + " kB", "Should be '" + Number(1.02).toLocaleString(undefined, {minimumFractionDigits: 1}) + " kB'"); + }), Number(1.02).toLocaleString(undefined, {minimumFractionDigits: 1}) + " KiB", "Should be '" + Number(1.02).toLocaleString(undefined, {minimumFractionDigits: 1}) + " KiB'"); test.equal(filesize(this.kibibyte, { base: 10, locale: "de", localeOptions: {minimumFractionDigits: 1} - }), Number(1.02).toLocaleString("de", {minimumFractionDigits: 1}) + " kB", "Should be '" + Number(1.02).toLocaleString("de", {minimumFractionDigits: 1}) + " kB'"); + }), Number(1.02).toLocaleString("de", {minimumFractionDigits: 1}) + " KiB", "Should be '" + Number(1.02).toLocaleString("de", {minimumFractionDigits: 1}) + " KiB'"); test.done(); }, roundingMethod: function (test) { test.expect(9); - test.equal(filesize(this.kibibyte, {base: 10, roundingMethod: "round"}), "1.02 kB", "Should be '1.02 kB'"); - test.equal(filesize(this.kibibyte, {base: 10, roundingMethod: "floor"}), "1.02 kB", "Should be '1.02 kB'"); - test.equal(filesize(this.kibibyte, {base: 10, roundingMethod: "ceil"}), "1.03 kB", "Should be '1.03 kB'"); - test.equal(filesize(this.kibibyte * 1.333, {base: 10, roundingMethod: "round"}), "1.36 kB", "Should be '1.36 kB'"); - test.equal(filesize(this.kibibyte * 1.333, {base: 10, roundingMethod: "floor"}), "1.36 kB", "Should be '1.36 kB'"); - test.equal(filesize(this.kibibyte * 1.333, {base: 10, roundingMethod: "ceil"}), "1.37 kB", "Should be '1.37 kB'"); - test.equal(filesize(this.kibibyte * 1.456, {base: 10, round: 1, roundingMethod: "round"}), "1.5 kB", "Should be '1.5 kB'"); - test.equal(filesize(this.kibibyte * 1.456, {base: 10, round: 1, roundingMethod: "floor"}), "1.4 kB", "Should be '1.4 kB'"); - test.equal(filesize(this.kibibyte * 1.456, {base: 10, round: 1, roundingMethod: "ceil"}), "1.5 kB", "Should be '1.5 kB'"); + test.equal(filesize(this.kibibyte, {base: 10, roundingMethod: "round"}), "1.02 KiB", "Should be '1.02 KiB'"); + test.equal(filesize(this.kibibyte, {base: 10, roundingMethod: "floor"}), "1.02 KiB", "Should be '1.02 KiB'"); + test.equal(filesize(this.kibibyte, {base: 10, roundingMethod: "ceil"}), "1.03 KiB", "Should be '1.03 KiB'"); + test.equal(filesize(this.kibibyte * 1.333, {base: 10, roundingMethod: "round"}), "1.36 KiB", "Should be '1.36 KiB'"); + test.equal(filesize(this.kibibyte * 1.333, {base: 10, roundingMethod: "floor"}), "1.36 KiB", "Should be '1.36 KiB'"); + test.equal(filesize(this.kibibyte * 1.333, {base: 10, roundingMethod: "ceil"}), "1.37 KiB", "Should be '1.37 KiB'"); + test.equal(filesize(this.kibibyte * 1.456, {base: 10, round: 1, roundingMethod: "round"}), "1.5 KiB", "Should be '1.5 KiB'"); + test.equal(filesize(this.kibibyte * 1.456, {base: 10, round: 1, roundingMethod: "floor"}), "1.4 KiB", "Should be '1.4 KiB'"); + test.equal(filesize(this.kibibyte * 1.456, {base: 10, round: 1, roundingMethod: "ceil"}), "1.5 KiB", "Should be '1.5 KiB'"); test.done(); }, precision: function (test) { test.expect(5); - test.equal(filesize(this.kibibyte * 1, {base: 10, precision: 3}), "1.02 kB", "Should be '1.02 kB'"); - test.equal(filesize(this.kibibyte * this.kibibyte * 10.25, {base: 10, precision: 3}), "10.8 MB", "Should be '10.8 MB'"); - test.equal(filesize(this.kibibyte * this.kibibyte * 10.25, {base: 10, precision: "x"}), "10.75 MB", "Should be '10.75 MB'"); - test.equal(filesize(this.kibibyte * this.kibibyte * this.kibibyte, {base: 10, precision: 3}), "1.07 GB", "Should be '1.07 GB'"); - test.equal(filesize(Math.pow(this.kibibyte, 10), {base: 10, precision: 3}), "1e+6 YB", "Should be '1e+6 YB'"); + test.equal(filesize(this.kibibyte * 1, {base: 10, precision: 3}), "1.02 KiB", "Should be '1.02 KiB'"); + test.equal(filesize(this.kibibyte * this.kibibyte * 10.25, {base: 10, precision: 3}), "10.8 MiB", "Should be '10.8 MiB'"); + test.equal(filesize(this.kibibyte * this.kibibyte * 10.25, {base: 10, precision: "x"}), "10.75 MiB", "Should be '10.75 MiB'"); + test.equal(filesize(this.kibibyte * this.kibibyte * this.kibibyte, {base: 10, precision: 3}), "1.07 GiB", "Should be '1.07 GiB'"); + test.equal(filesize(Math.pow(this.kibibyte, 10), {base: 10, precision: 3}), "1e+6 YiB", "Should be '1e+6 YiB'"); test.done(); }, defaults: function (test) { test.expect(2); - test.equal(filesize(this.kibibyte), "1.02 kB", "Should be '1.02 kB'"); + test.equal(filesize(this.kibibyte), "1.02 KiB", "Should be '1.02 KiB'"); test.equal(filesize(this.kibibyte, {base: 2}), "1 KiB", "Should be '1 KiB'"); test.done(); }