diff --git a/lib/filesize.es6.js b/lib/filesize.es6.js index 801473f..143697e 100644 --- a/lib/filesize.es6.js +++ b/lib/filesize.es6.js @@ -11,6 +11,28 @@ (global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.filesize = factory()); })(this, (function () { 'use strict'; + const ARRAY = "array"; + const BIT = "bit"; + const BITS = "bits"; + const BYTE = "byte"; + const BYTES = "bytes"; + const EMPTY = ""; + const EXPONENT = "exponent"; + const FUNCTION = "function"; + const IEC = "iec"; + const INVALID_NUMBER = "Invalid number"; + const INVALID_ROUND = "Invalid rounding method"; + const JEDEC = "jedec"; + const OBJECT = "object"; + const PERIOD = "."; + const ROUND = "round"; + const S = "s"; + const SI_KBIT = "kbit"; + const SI_KBYTE = "kB"; + const SPACE = " "; + const STRING = "string"; + const ZERO = "0"; + const strings = { symbol: { iec: { @@ -36,23 +58,23 @@ * @param {Object} descriptor [Optional] Flags * @return {String} Readable file size String */ - function filesize (arg, {bits = false, pad = false, base = -1, round = 2, locale = "", localeOptions = {}, separator = "", spacer = " ", symbols = {}, standard = "", output = "string", fullform = false, fullforms = [], exponent = -1, roundingMethod = "round", precision = 0} = {}) { + function filesize (arg, {bits = false, pad = false, base = -1, round = 2, locale = EMPTY, localeOptions = {}, separator = EMPTY, spacer = SPACE, symbols = {}, standard = EMPTY, output = STRING, fullform = false, fullforms = [], exponent = -1, roundingMethod = ROUND, precision = 0} = {}) { let e = exponent, num = Number(arg), result = [], val = 0, - u = ""; + u = EMPTY; // Sync base & standard if (base === -1 && standard.length === 0) { base = 10; - standard = "iec"; + standard = JEDEC; } else if (base === -1 && standard.length > 0) { - standard = standard === "iec" ? "iec" : "jedec"; - base = standard === "iec" ? 10 : 2; + standard = standard === IEC ? IEC : JEDEC; + base = standard === IEC ? 2 : 10; } else { base = base === 2 ? 2 : 10; - standard = base === 10 ? "iec" : "jedec"; + standard = base === 10 ? JEDEC : IEC; } const ceil = base === 10 ? 1000 : 1024, @@ -61,11 +83,11 @@ roundingFunc = Math[roundingMethod]; if (isNaN(arg)) { - throw new TypeError("Invalid number"); + throw new TypeError(INVALID_NUMBER); } - if (typeof roundingFunc !== "function") { - throw new TypeError("Invalid rounding method"); + if (typeof roundingFunc !== FUNCTION) { + throw new TypeError(INVALID_ROUND); } // Flipping a negative number to determine the size @@ -91,14 +113,14 @@ e = 8; } - if (output === "exponent") { + if (output === EXPONENT) { return e; } // Zero is now a special case because bytes divide by 1 if (num === 0) { result[0] = 0; - u = result[1] = strings.symbol[standard][bits ? "bits" : "bytes"][e]; + u = result[1] = strings.symbol[standard][bits ? BITS : BYTES][e]; } else { val = num / (base === 2 ? Math.pow(2, e * 10) : Math.pow(1000, e)); @@ -119,7 +141,7 @@ e++; } - u = result[1] = strings.symbol[standard][bits ? "bits" : "bytes"][e]; + u = result[1] = base === 10 && e === 1 ? bits ? SI_KBIT : SI_KBYTE : strings.symbol[standard][bits ? BITS : BYTES][e]; } // Decorating a 'diff' @@ -140,25 +162,25 @@ } else if (locale.length > 0) { result[0] = result[0].toLocaleString(locale, localeOptions); } else if (separator.length > 0) { - result[0] = result[0].toString().replace(".", separator); + result[0] = result[0].toString().replace(PERIOD, separator); } if (pad && Number.isInteger(result[0]) === false && round > 0) { - const x = separator || ".", + const x = separator || PERIOD, tmp = result[0].toString().split(x), - s = tmp[1] || "", + s = tmp[1] || EMPTY, l = s.length, n = round - l; - result[0] = `${tmp[0]}${x}${s.padEnd(l + n, "0")}`; + result[0] = `${tmp[0]}${x}${s.padEnd(l + n, ZERO)}`; } if (full) { - result[1] = fullforms[e] ? fullforms[e] : strings.fullform[standard][e] + (bits ? "bit" : "byte") + (result[0] === 1 ? "" : "s"); + result[1] = fullforms[e] ? fullforms[e] : strings.fullform[standard][e] + (bits ? BIT : BYTE) + (result[0] === 1 ? EMPTY : S); } // Returning Array, Object, or String (default) - return output === "array" ? result : output === "object" ? {value: result[0], symbol: result[1], exponent: e, unit: u} : result.join(spacer); + return output === ARRAY ? result : output === OBJECT ? {value: result[0], symbol: result[1], exponent: e, unit: u} : result.join(spacer); } // Partial application for functional programming diff --git a/lib/filesize.es6.min.js b/lib/filesize.es6.min.js index a395200..077dc2a 100644 --- a/lib/filesize.es6.min.js +++ b/lib/filesize.es6.min.js @@ -2,5 +2,5 @@ 2022 Jason Mulligan @version 9.0.8 */ -!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={symbol:{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"]}},fullform:{iec:["","kibi","mebi","gibi","tebi","pebi","exbi","zebi","yobi"],jedec:["","kilo","mega","giga","tera","peta","exa","zetta","yotta"]}};function t(t,{bits:e=!1,pad:o=!1,base:n=-1,round:b=2,locale:r="",localeOptions:l={},separator:s="",spacer:a=" ",symbols:c={},standard:f="",output:u="string",fullform:d=!1,fullforms:p=[],exponent:g=-1,roundingMethod:y="round",precision:h=0}={}){let m=g,B=Number(t),M=[],T=0,j="";-1===n&&0===f.length?(n=10,f="iec"):-1===n&&f.length>0?n="iec"===(f="iec"===f?"iec":"jedec")?10:2:f=10===(n=2===n?2:10)?"iec":"jedec";const w=10===n?1e3:1024,x=!0===d,E=B<0,N=Math[y];if(isNaN(t))throw new TypeError("Invalid number");if("function"!=typeof N)throw new TypeError("Invalid rounding method");if(E&&(B=-B),(-1===m||isNaN(m))&&(m=Math.floor(Math.log(B)/Math.log(w)),m<0&&(m=0)),m>8&&(h>0&&(h+=8-m),m=8),"exponent"===u)return m;if(0===B)M[0]=0,j=M[1]=i.symbol[f][e?"bits":"bytes"][m];else{T=B/(2===n?Math.pow(2,10*m):Math.pow(1e3,m)),e&&(T*=8,T>=w&&m<8&&(T/=w,m++));const t=Math.pow(10,m>0?b:0);M[0]=N(T*t)/t,M[0]===w&&m<8&&-1===g&&(M[0]=1,m++),j=M[1]=i.symbol[f][e?"bits":"bytes"][m]}if(E&&(M[0]=-M[0]),h>0&&(M[0]=M[0].toPrecision(h)),M[1]=c[M[1]]||M[1],!0===r?M[0]=M[0].toLocaleString():r.length>0?M[0]=M[0].toLocaleString(r,l):s.length>0&&(M[0]=M[0].toString().replace(".",s)),o&&!1===Number.isInteger(M[0])&&b>0){const i=s||".",t=M[0].toString().split(i),e=t[1]||"",o=e.length,n=b-o;M[0]=`${t[0]}${i}${e.padEnd(o+n,"0")}`}return x&&(M[1]=p[m]?p[m]:i.fullform[f][m]+(e?"bit":"byte")+(1===M[0]?"":"s")),"array"===u?M:"object"===u?{value:M[0],symbol:M[1],exponent:m,unit:j}:M.join(a)}return t.partial=i=>e=>t(e,i),t})); +!function(t,i){"object"==typeof exports&&"undefined"!=typeof module?module.exports=i():"function"==typeof define&&define.amd?define(i):(t="undefined"!=typeof globalThis?globalThis:t||self).filesize=i()}(this,(function(){"use strict";const t="bits",i="bytes",e="",o="iec",n="jedec",b={symbol:{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"]}},fullform:{iec:["","kibi","mebi","gibi","tebi","pebi","exbi","zebi","yobi"],jedec:["","kilo","mega","giga","tera","peta","exa","zetta","yotta"]}};function r(r,{bits:l=!1,pad:s=!1,base:a=-1,round:f=2,locale:u="",localeOptions:p={},separator:c="",spacer:d=" ",symbols:g={},standard:h="",output:y="string",fullform:B=!1,fullforms:m=[],exponent:M=-1,roundingMethod:T="round",precision:w=0}={}){let x=M,E=Number(r),j=[],N=0,P=e;-1===a&&0===h.length?(a=10,h=n):-1===a&&h.length>0?a=(h=h===o?o:n)===o?2:10:h=10===(a=2===a?2:10)?n:o;const k=10===a?1e3:1024,G=!0===B,K=E<0,S=Math[T];if(isNaN(r))throw new TypeError("Invalid number");if("function"!=typeof S)throw new TypeError("Invalid rounding method");if(K&&(E=-E),(-1===x||isNaN(x))&&(x=Math.floor(Math.log(E)/Math.log(k)),x<0&&(x=0)),x>8&&(w>0&&(w+=8-x),x=8),"exponent"===y)return x;if(0===E)j[0]=0,P=j[1]=b.symbol[h][l?t:i][x];else{N=E/(2===a?Math.pow(2,10*x):Math.pow(1e3,x)),l&&(N*=8,N>=k&&x<8&&(N/=k,x++));const e=Math.pow(10,x>0?f:0);j[0]=S(N*e)/e,j[0]===k&&x<8&&-1===M&&(j[0]=1,x++),P=j[1]=10===a&&1===x?l?"kbit":"kB":b.symbol[h][l?t:i][x]}if(K&&(j[0]=-j[0]),w>0&&(j[0]=j[0].toPrecision(w)),j[1]=g[j[1]]||j[1],!0===u?j[0]=j[0].toLocaleString():u.length>0?j[0]=j[0].toLocaleString(u,p):c.length>0&&(j[0]=j[0].toString().replace(".",c)),s&&!1===Number.isInteger(j[0])&&f>0){const t=c||".",i=j[0].toString().split(t),o=i[1]||e,n=o.length,b=f-n;j[0]=`${i[0]}${t}${o.padEnd(n+b,"0")}`}return G&&(j[1]=m[x]?m[x]:b.fullform[h][x]+(l?"bit":"byte")+(1===j[0]?e:"s")),"array"===y?j:"object"===y?{value:j[0],symbol:j[1],exponent:x,unit:P}:j.join(d)}return r.partial=t=>i=>r(i,t),r})); //# sourceMappingURL=filesize.es6.min.js.map diff --git a/lib/filesize.es6.min.js.map b/lib/filesize.es6.min.js.map index fbe30d4..7385db8 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/strings.js","../src/filesize.js"],"sourcesContent":["\"use strict\";\r\n\r\nexport const strings = {\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};\r\n","\"use strict\";\r\n\r\nimport {strings} from \"./strings\";\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, {bits = false, pad = false, base = -1, round = 2, locale = \"\", localeOptions = {}, separator = \"\", spacer = \" \", symbols = {}, standard = \"\", output = \"string\", fullform = false, fullforms = [], exponent = -1, roundingMethod = \"round\", precision = 0} = {}) {\r\n\tlet e = exponent,\r\n\t\tnum = Number(arg),\r\n\t\tresult = [],\r\n\t\tval = 0,\r\n\t\tu = \"\";\r\n\r\n\t// Sync base & standard\r\n\tif (base === -1 && standard.length === 0) {\r\n\t\tbase = 10;\r\n\t\tstandard = \"iec\";\r\n\t} else if (base === -1 && standard.length > 0) {\r\n\t\tstandard = standard === \"iec\" ? \"iec\" : \"jedec\";\r\n\t\tbase = standard === \"iec\" ? 10 : 2;\r\n\t} else {\r\n\t\tbase = base === 2 ? 2 : 10;\r\n\t\tstandard = base === 10 ? \"iec\" : \"jedec\";\r\n\t}\r\n\r\n\tconst ceil = base === 10 ? 1000 : 1024,\r\n\t\tfull = fullform === true,\r\n\t\tneg = num < 0,\r\n\t\troundingFunc = Math[roundingMethod];\r\n\r\n\tif (isNaN(arg)) {\r\n\t\tthrow new TypeError(\"Invalid number\");\r\n\t}\r\n\r\n\tif (typeof roundingFunc !== \"function\") {\r\n\t\tthrow new TypeError(\"Invalid rounding method\");\r\n\t}\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] = strings.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 && exponent === -1) {\r\n\t\t\tresult[0] = 1;\r\n\t\t\te++;\r\n\t\t}\r\n\r\n\t\tu = result[1] = strings.symbol[standard][bits ? \"bits\" : \"bytes\"][e];\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] : strings.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":["strings","symbol","iec","bits","bytes","jedec","fullform","filesize","arg","pad","base","round","locale","localeOptions","separator","spacer","symbols","standard","output","fullforms","exponent","roundingMethod","precision","e","num","Number","result","val","u","length","ceil","full","neg","roundingFunc","Math","isNaN","TypeError","floor","log","pow","p","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AQC,GACnCC,EAAUe,OAAS,IAC7BH,EAAO,GAAKA,EAAO,GAAGiB,WAAWC,QAAQ,IAAK9B,IAG3CL,IAAuC,IAAhCgB,OAAOoB,UAAUnB,EAAO,KAAiBf,EAAQ,EAAG,CAC9D,MAAMmC,EAAIhC,GAAa,IACtBiC,EAAMrB,EAAO,GAAGiB,WAAWK,MAAMF,GACjCG,EAAIF,EAAI,IAAM,GACdG,EAAID,EAAEpB,OACNsB,EAAIxC,EAAQuC,EAEbxB,EAAO,GAAK,GAAGqB,EAAI,KAAKD,IAAIG,EAAEG,OAAOF,EAAIC,EAAG,OAQ7C,OALIpB,IACHL,EAAO,GAAKP,EAAUI,GAAKJ,EAAUI,GAAKvB,EAAQM,SAASW,GAAUM,IAAMpB,EAAO,MAAQ,SAAyB,IAAduB,EAAO,GAAW,GAAK,MAI3G,UAAXR,EAAqBQ,EAAoB,WAAXR,EAAsB,CAACmC,MAAO3B,EAAO,GAAIzB,OAAQyB,EAAO,GAAIN,SAAUG,EAAG+B,KAAM1B,GAAKF,EAAO6B,KAAKxC,UAItIR,EAASiD,QAAUC,GAAOjD,GAAOD,EAASC,EAAKiD"} \ No newline at end of file +{"version":3,"file":"filesize.es6.min.js","sources":["../src/constants.js","../src/strings.js","../src/filesize.js"],"sourcesContent":["\"use strict\";\r\n\r\nexport const ARRAY = \"array\";\r\nexport const BIT = \"bit\";\r\nexport const BITS = \"bits\";\r\nexport const BYTE = \"byte\";\r\nexport const BYTES = \"bytes\";\r\nexport const EMPTY = \"\";\r\nexport const EXPONENT = \"exponent\";\r\nexport const FUNCTION = \"function\";\r\nexport const IEC = \"iec\";\r\nexport const INVALID_NUMBER = \"Invalid number\";\r\nexport const INVALID_ROUND = \"Invalid rounding method\";\r\nexport const JEDEC = \"jedec\";\r\nexport const OBJECT = \"object\";\r\nexport const PERIOD = \".\";\r\nexport const ROUND = \"round\";\r\nexport const S = \"s\";\r\nexport const SI_KBIT = \"kbit\";\r\nexport const SI_KBYTE = \"kB\";\r\nexport const SPACE = \" \";\r\nexport const STRING = \"string\";\r\nexport const ZERO = \"0\";\r\n","\"use strict\";\r\n\r\nexport const strings = {\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};\r\n","\"use strict\";\r\n\r\nimport {BITS, BYTES, EMPTY, EXPONENT, FUNCTION, IEC, INVALID_NUMBER, INVALID_ROUND, JEDEC, SI_KBIT, SI_KBYTE, SPACE, PERIOD, ZERO, ARRAY, OBJECT, S, BIT, BYTE, STRING, ROUND} from \"./constants\";\r\nimport {strings} from \"./strings\";\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, {bits = false, pad = false, base = -1, round = 2, locale = EMPTY, localeOptions = {}, separator = EMPTY, spacer = SPACE, symbols = {}, standard = EMPTY, output = STRING, fullform = false, fullforms = [], exponent = -1, roundingMethod = ROUND, precision = 0} = {}) {\r\n\tlet e = exponent,\r\n\t\tnum = Number(arg),\r\n\t\tresult = [],\r\n\t\tval = 0,\r\n\t\tu = EMPTY;\r\n\r\n\t// Sync base & standard\r\n\tif (base === -1 && standard.length === 0) {\r\n\t\tbase = 10;\r\n\t\tstandard = JEDEC;\r\n\t} else if (base === -1 && standard.length > 0) {\r\n\t\tstandard = standard === IEC ? IEC : JEDEC;\r\n\t\tbase = standard === IEC ? 2 : 10;\r\n\t} else {\r\n\t\tbase = base === 2 ? 2 : 10;\r\n\t\tstandard = base === 10 ? JEDEC : IEC;\r\n\t}\r\n\r\n\tconst ceil = base === 10 ? 1000 : 1024,\r\n\t\tfull = fullform === true,\r\n\t\tneg = num < 0,\r\n\t\troundingFunc = Math[roundingMethod];\r\n\r\n\tif (isNaN(arg)) {\r\n\t\tthrow new TypeError(INVALID_NUMBER);\r\n\t}\r\n\r\n\tif (typeof roundingFunc !== FUNCTION) {\r\n\t\tthrow new TypeError(INVALID_ROUND);\r\n\t}\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] = strings.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 && exponent === -1) {\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 ? SI_KBIT : SI_KBYTE : strings.symbol[standard][bits ? BITS : BYTES][e];\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(PERIOD, separator);\r\n\t}\r\n\r\n\tif (pad && Number.isInteger(result[0]) === false && round > 0) {\r\n\t\tconst x = separator || PERIOD,\r\n\t\t\ttmp = result[0].toString().split(x),\r\n\t\t\ts = tmp[1] || EMPTY,\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, ZERO)}`;\r\n\t}\r\n\r\n\tif (full) {\r\n\t\tresult[1] = fullforms[e] ? fullforms[e] : strings.fullform[standard][e] + (bits ? BIT : BYTE) + (result[0] === 1 ? EMPTY : 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":["BITS","BYTES","EMPTY","IEC","JEDEC","strings","symbol","iec","bits","bytes","jedec","fullform","filesize","arg","pad","base","round","locale","localeOptions","separator","spacer","symbols","standard","output","fullforms","exponent","roundingMethod","precision","e","num","Number","result","val","u","length","ceil","full","neg","roundingFunc","Math","isNaN","TypeError","floor","log","pow","p","toPrecision","toLocaleString","toString","replace","isInteger","x","tmp","split","s","l","n","padEnd","value","unit","join","partial","opt"],"mappings":";;;;yOAEO,MAEMA,EAAO,OAEPC,EAAQ,QACRC,EAAQ,GAGRC,EAAM,MAGNC,EAAQ,QCXRC,EAAU,CACtBC,OAAQ,CACPC,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,SAAU,CACTJ,IAAK,CAAC,GAAI,OAAQ,OAAQ,OAAQ,OAAQ,OAAQ,OAAQ,OAAQ,QAClEG,MAAO,CAAC,GAAI,OAAQ,OAAQ,OAAQ,OAAQ,OAAQ,MAAO,QAAS,WCFtE,SAASE,EAAUC,GAAKL,KAACA,GAAO,EAAKM,IAAEA,GAAM,EAAKC,KAAEA,GAAO,EAAEC,MAAEA,EAAQ,EAACC,OAAEA,EAASf,GAAKgB,cAAEA,EAAgB,GAAEC,UAAEA,EAAYjB,GAAKkB,OAAEA,EFO5G,IEP0HC,QAAEA,EAAU,GAAEC,SAAEA,EAAWpB,GAAKqB,OAAEA,EFQ3J,SER0KZ,SAAEA,GAAW,E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ib/filesize.esm.js index 55c7bb4..7ce2f99 100644 --- a/lib/filesize.esm.js +++ b/lib/filesize.esm.js @@ -5,6 +5,28 @@ * @license BSD-3-Clause * @version 9.0.8 */ +const ARRAY = "array"; +const BIT = "bit"; +const BITS = "bits"; +const BYTE = "byte"; +const BYTES = "bytes"; +const EMPTY = ""; +const EXPONENT = "exponent"; +const FUNCTION = "function"; +const IEC = "iec"; +const INVALID_NUMBER = "Invalid number"; +const INVALID_ROUND = "Invalid rounding method"; +const JEDEC = "jedec"; +const OBJECT = "object"; +const PERIOD = "."; +const ROUND = "round"; +const S = "s"; +const SI_KBIT = "kbit"; +const SI_KBYTE = "kB"; +const SPACE = " "; +const STRING = "string"; +const ZERO = "0"; + const strings = { symbol: { iec: { @@ -30,23 +52,23 @@ const strings = { * @param {Object} descriptor [Optional] Flags * @return {String} Readable file size String */ -function filesize (arg, {bits = false, pad = false, base = -1, round = 2, locale = "", localeOptions = {}, separator = "", spacer = " ", symbols = {}, standard = "", output = "string", fullform = false, fullforms = [], exponent = -1, roundingMethod = "round", precision = 0} = {}) { +function filesize (arg, {bits = false, pad = false, base = -1, round = 2, locale = EMPTY, localeOptions = {}, separator = EMPTY, spacer = SPACE, symbols = {}, standard = EMPTY, output = STRING, fullform = false, fullforms = [], exponent = -1, roundingMethod = ROUND, precision = 0} = {}) { let e = exponent, num = Number(arg), result = [], val = 0, - u = ""; + u = EMPTY; // Sync base & standard if (base === -1 && standard.length === 0) { base = 10; - standard = "iec"; + standard = JEDEC; } else if (base === -1 && standard.length > 0) { - standard = standard === "iec" ? "iec" : "jedec"; - base = standard === "iec" ? 10 : 2; + standard = standard === IEC ? IEC : JEDEC; + base = standard === IEC ? 2 : 10; } else { base = base === 2 ? 2 : 10; - standard = base === 10 ? "iec" : "jedec"; + standard = base === 10 ? JEDEC : IEC; } const ceil = base === 10 ? 1000 : 1024, @@ -55,11 +77,11 @@ function filesize (arg, {bits = false, pad = false, base = -1, round = 2, locale roundingFunc = Math[roundingMethod]; if (isNaN(arg)) { - throw new TypeError("Invalid number"); + throw new TypeError(INVALID_NUMBER); } - if (typeof roundingFunc !== "function") { - throw new TypeError("Invalid rounding method"); + if (typeof roundingFunc !== FUNCTION) { + throw new TypeError(INVALID_ROUND); } // Flipping a negative number to determine the size @@ -85,14 +107,14 @@ function filesize (arg, {bits = false, pad = false, base = -1, round = 2, locale e = 8; } - if (output === "exponent") { + if (output === EXPONENT) { return e; } // Zero is now a special case because bytes divide by 1 if (num === 0) { result[0] = 0; - u = result[1] = strings.symbol[standard][bits ? "bits" : "bytes"][e]; + u = result[1] = strings.symbol[standard][bits ? BITS : BYTES][e]; } else { val = num / (base === 2 ? Math.pow(2, e * 10) : Math.pow(1000, e)); @@ -113,7 +135,7 @@ function filesize (arg, {bits = false, pad = false, base = -1, round = 2, locale e++; } - u = result[1] = strings.symbol[standard][bits ? "bits" : "bytes"][e]; + u = result[1] = base === 10 && e === 1 ? bits ? SI_KBIT : SI_KBYTE : strings.symbol[standard][bits ? BITS : BYTES][e]; } // Decorating a 'diff' @@ -134,25 +156,25 @@ function filesize (arg, {bits = false, pad = false, base = -1, round = 2, locale } else if (locale.length > 0) { result[0] = result[0].toLocaleString(locale, localeOptions); } else if (separator.length > 0) { - result[0] = result[0].toString().replace(".", separator); + result[0] = result[0].toString().replace(PERIOD, separator); } if (pad && Number.isInteger(result[0]) === false && round > 0) { - const x = separator || ".", + const x = separator || PERIOD, tmp = result[0].toString().split(x), - s = tmp[1] || "", + s = tmp[1] || EMPTY, l = s.length, n = round - l; - result[0] = `${tmp[0]}${x}${s.padEnd(l + n, "0")}`; + result[0] = `${tmp[0]}${x}${s.padEnd(l + n, ZERO)}`; } if (full) { - result[1] = fullforms[e] ? fullforms[e] : strings.fullform[standard][e] + (bits ? "bit" : "byte") + (result[0] === 1 ? "" : "s"); + result[1] = fullforms[e] ? fullforms[e] : strings.fullform[standard][e] + (bits ? BIT : BYTE) + (result[0] === 1 ? EMPTY : S); } // Returning Array, Object, or String (default) - return output === "array" ? result : output === "object" ? {value: result[0], symbol: result[1], exponent: e, unit: u} : result.join(spacer); + return output === ARRAY ? result : output === OBJECT ? {value: result[0], symbol: result[1], exponent: e, unit: u} : result.join(spacer); } // Partial application for functional programming diff --git a/lib/filesize.esm.min.js b/lib/filesize.esm.min.js index 64ec18d..b8a93af 100644 --- a/lib/filesize.esm.min.js +++ b/lib/filesize.esm.min.js @@ -2,5 +2,5 @@ 2022 Jason Mulligan @version 9.0.8 */ -const i={symbol:{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"]}},fullform:{iec:["","kibi","mebi","gibi","tebi","pebi","exbi","zebi","yobi"],jedec:["","kilo","mega","giga","tera","peta","exa","zetta","yotta"]}};function t(t,{bits:e=!1,pad:o=!1,base:b=-1,round:n=2,locale:r="",localeOptions:a={},separator:l="",spacer:s=" ",symbols:c={},standard:p="",output:u="string",fullform:g=!1,fullforms:d=[],exponent:f=-1,roundingMethod:B="round",precision:h=0}={}){let y=f,m=Number(t),M=[],w=0,E="";-1===b&&0===p.length?(b=10,p="iec"):-1===b&&p.length>0?b="iec"===(p="iec"===p?"iec":"jedec")?10:2:p=10===(b=2===b?2:10)?"iec":"jedec";const j=10===b?1e3:1024,x=!0===g,N=m<0,T=Math[B];if(isNaN(t))throw new TypeError("Invalid number");if("function"!=typeof T)throw new TypeError("Invalid rounding method");if(N&&(m=-m),(-1===y||isNaN(y))&&(y=Math.floor(Math.log(m)/Math.log(j)),y<0&&(y=0)),y>8&&(h>0&&(h+=8-y),y=8),"exponent"===u)return y;if(0===m)M[0]=0,E=M[1]=i.symbol[p][e?"bits":"bytes"][y];else{w=m/(2===b?Math.pow(2,10*y):Math.pow(1e3,y)),e&&(w*=8,w>=j&&y<8&&(w/=j,y++));const t=Math.pow(10,y>0?n:0);M[0]=T(w*t)/t,M[0]===j&&y<8&&-1===f&&(M[0]=1,y++),E=M[1]=i.symbol[p][e?"bits":"bytes"][y]}if(N&&(M[0]=-M[0]),h>0&&(M[0]=M[0].toPrecision(h)),M[1]=c[M[1]]||M[1],!0===r?M[0]=M[0].toLocaleString():r.length>0?M[0]=M[0].toLocaleString(r,a):l.length>0&&(M[0]=M[0].toString().replace(".",l)),o&&!1===Number.isInteger(M[0])&&n>0){const i=l||".",t=M[0].toString().split(i),e=t[1]||"",o=e.length,b=n-o;M[0]=`${t[0]}${i}${e.padEnd(o+b,"0")}`}return x&&(M[1]=d[y]?d[y]:i.fullform[p][y]+(e?"bit":"byte")+(1===M[0]?"":"s")),"array"===u?M:"object"===u?{value:M[0],symbol:M[1],exponent:y,unit:E}:M.join(s)}t.partial=i=>e=>t(e,i);export{t as default}; +const t={symbol:{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"]}},fullform:{iec:["","kibi","mebi","gibi","tebi","pebi","exbi","zebi","yobi"],jedec:["","kilo","mega","giga","tera","peta","exa","zetta","yotta"]}};function i(i,{bits:e=!1,pad:o=!1,base:b=-1,round:n=2,locale:r="",localeOptions:a={},separator:l="",spacer:s=" ",symbols:c={},standard:p="",output:u="string",fullform:d=!1,fullforms:g=[],exponent:B=-1,roundingMethod:f="round",precision:h=0}={}){let y=B,m=Number(i),M=[],j=0,w="";-1===b&&0===p.length?(b=10,p="jedec"):-1===b&&p.length>0?b="iec"===(p="iec"===p?"iec":"jedec")?2:10:p=10===(b=2===b?2:10)?"jedec":"iec";const E=10===b?1e3:1024,x=!0===d,N=m<0,T=Math[f];if(isNaN(i))throw new TypeError("Invalid number");if("function"!=typeof T)throw new TypeError("Invalid rounding method");if(N&&(m=-m),(-1===y||isNaN(y))&&(y=Math.floor(Math.log(m)/Math.log(E)),y<0&&(y=0)),y>8&&(h>0&&(h+=8-y),y=8),"exponent"===u)return y;if(0===m)M[0]=0,w=M[1]=t.symbol[p][e?"bits":"bytes"][y];else{j=m/(2===b?Math.pow(2,10*y):Math.pow(1e3,y)),e&&(j*=8,j>=E&&y<8&&(j/=E,y++));const i=Math.pow(10,y>0?n:0);M[0]=T(j*i)/i,M[0]===E&&y<8&&-1===B&&(M[0]=1,y++),w=M[1]=10===b&&1===y?e?"kbit":"kB":t.symbol[p][e?"bits":"bytes"][y]}if(N&&(M[0]=-M[0]),h>0&&(M[0]=M[0].toPrecision(h)),M[1]=c[M[1]]||M[1],!0===r?M[0]=M[0].toLocaleString():r.length>0?M[0]=M[0].toLocaleString(r,a):l.length>0&&(M[0]=M[0].toString().replace(".",l)),o&&!1===Number.isInteger(M[0])&&n>0){const t=l||".",i=M[0].toString().split(t),e=i[1]||"",o=e.length,b=n-o;M[0]=`${i[0]}${t}${e.padEnd(o+b,"0")}`}return x&&(M[1]=g[y]?g[y]:t.fullform[p][y]+(e?"bit":"byte")+(1===M[0]?"":"s")),"array"===u?M:"object"===u?{value:M[0],symbol:M[1],exponent:y,unit:w}:M.join(s)}i.partial=t=>e=>i(e,t);export{i 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 206a5d9..dea1d97 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/strings.js","../src/filesize.js"],"sourcesContent":["\"use strict\";\r\n\r\nexport const strings = {\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};\r\n","\"use strict\";\r\n\r\nimport {strings} from \"./strings\";\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, {bits = false, pad = false, base = -1, round = 2, locale = \"\", localeOptions = {}, separator = \"\", spacer = \" \", symbols = {}, standard = \"\", output = \"string\", fullform = false, fullforms = [], exponent = -1, roundingMethod = \"round\", precision = 0} = {}) {\r\n\tlet e = exponent,\r\n\t\tnum = Number(arg),\r\n\t\tresult = [],\r\n\t\tval = 0,\r\n\t\tu = \"\";\r\n\r\n\t// Sync base & standard\r\n\tif (base === -1 && standard.length === 0) {\r\n\t\tbase = 10;\r\n\t\tstandard = \"iec\";\r\n\t} else if (base === -1 && standard.length > 0) {\r\n\t\tstandard = standard === \"iec\" ? \"iec\" : \"jedec\";\r\n\t\tbase = standard === \"iec\" ? 10 : 2;\r\n\t} else {\r\n\t\tbase = base === 2 ? 2 : 10;\r\n\t\tstandard = base === 10 ? \"iec\" : \"jedec\";\r\n\t}\r\n\r\n\tconst ceil = base === 10 ? 1000 : 1024,\r\n\t\tfull = fullform === true,\r\n\t\tneg = num < 0,\r\n\t\troundingFunc = Math[roundingMethod];\r\n\r\n\tif (isNaN(arg)) {\r\n\t\tthrow new TypeError(\"Invalid number\");\r\n\t}\r\n\r\n\tif (typeof roundingFunc !== \"function\") {\r\n\t\tthrow new TypeError(\"Invalid rounding method\");\r\n\t}\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] = strings.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 && exponent === -1) {\r\n\t\t\tresult[0] = 1;\r\n\t\t\te++;\r\n\t\t}\r\n\r\n\t\tu = result[1] = strings.symbol[standard][bits ? \"bits\" : \"bytes\"][e];\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] : strings.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":["strings","symbol","iec","bits","bytes","jedec","fullform","filesize","arg","pad","base","round","locale","localeOptions","separator","spacer","symbols","standard","output","fullforms","exponent","roundingMethod","precision","e","num","Number","result","val","u","length","ceil","full","neg","roundingFunc","Math","isNaN","TypeError","floor","log","pow","p","toPrecision","toLocaleString","toString","replace","isInteger","x","tmp","split","s","l","n","padEnd","value","unit","join","partial","opt"],"mappings":";;;;AAEO,MAAMA,EAAU,CACtBC,OAAQ,CACPC,IAAK,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EAAO,KAAiBf,EAAQ,EAAG,CAC9D,MAAMmC,EAAIhC,GAAa,IACtBiC,EAAMrB,EAAO,GAAGiB,WAAWK,MAAMF,GACjCG,EAAIF,EAAI,IAAM,GACdG,EAAID,EAAEpB,OACNsB,EAAIxC,EAAQuC,EAEbxB,EAAO,GAAK,GAAGqB,EAAI,KAAKD,IAAIG,EAAEG,OAAOF,EAAIC,EAAG,OAQ7C,OALIpB,IACHL,EAAO,GAAKP,EAAUI,GAAKJ,EAAUI,GAAKvB,EAAQM,SAASW,GAAUM,IAAMpB,EAAO,MAAQ,SAAyB,IAAduB,EAAO,GAAW,GAAK,MAI3G,UAAXR,EAAqBQ,EAAoB,WAAXR,EAAsB,CAACmC,MAAO3B,EAAO,GAAIzB,OAAQyB,EAAO,GAAIN,SAAUG,EAAG+B,KAAM1B,GAAKF,EAAO6B,KAAKxC,GAItIR,EAASiD,QAAUC,GAAOjD,GAAOD,EAASC,EAAKiD"} \ No newline at end of file +{"version":3,"file":"filesize.esm.min.js","sources":["../src/constants.js","../src/strings.js","../src/filesize.js"],"sourcesContent":["\"use strict\";\r\n\r\nexport const ARRAY = \"array\";\r\nexport const BIT = \"bit\";\r\nexport const BITS = \"bits\";\r\nexport const BYTE = \"byte\";\r\nexport const BYTES = \"bytes\";\r\nexport const EMPTY = \"\";\r\nexport const EXPONENT = \"exponent\";\r\nexport const FUNCTION = \"function\";\r\nexport const IEC = \"iec\";\r\nexport const INVALID_NUMBER = \"Invalid number\";\r\nexport const INVALID_ROUND = \"Invalid rounding method\";\r\nexport const JEDEC = \"jedec\";\r\nexport const OBJECT = \"object\";\r\nexport const PERIOD = \".\";\r\nexport const ROUND = \"round\";\r\nexport const S = \"s\";\r\nexport const SI_KBIT = \"kbit\";\r\nexport const SI_KBYTE = \"kB\";\r\nexport const SPACE = \" \";\r\nexport const STRING = \"string\";\r\nexport const ZERO = \"0\";\r\n","\"use strict\";\r\n\r\nexport const strings = {\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};\r\n","\"use strict\";\r\n\r\nimport {BITS, BYTES, EMPTY, EXPONENT, FUNCTION, IEC, INVALID_NUMBER, INVALID_ROUND, JEDEC, SI_KBIT, SI_KBYTE, SPACE, PERIOD, ZERO, ARRAY, OBJECT, S, BIT, BYTE, STRING, ROUND} from \"./constants\";\r\nimport {strings} from \"./strings\";\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, {bits = false, pad = false, base = -1, round = 2, locale = EMPTY, localeOptions = {}, separator = EMPTY, spacer = SPACE, symbols = {}, standard = EMPTY, output = STRING, fullform = false, fullforms = [], exponent = -1, roundingMethod = ROUND, precision = 0} = {}) {\r\n\tlet e = exponent,\r\n\t\tnum = Number(arg),\r\n\t\tresult = [],\r\n\t\tval = 0,\r\n\t\tu = EMPTY;\r\n\r\n\t// Sync base & standard\r\n\tif (base === -1 && standard.length === 0) {\r\n\t\tbase = 10;\r\n\t\tstandard = JEDEC;\r\n\t} else if (base === -1 && standard.length > 0) {\r\n\t\tstandard = standard === IEC ? IEC : JEDEC;\r\n\t\tbase = standard === IEC ? 2 : 10;\r\n\t} else {\r\n\t\tbase = base === 2 ? 2 : 10;\r\n\t\tstandard = base === 10 ? JEDEC : IEC;\r\n\t}\r\n\r\n\tconst ceil = base === 10 ? 1000 : 1024,\r\n\t\tfull = fullform === true,\r\n\t\tneg = num < 0,\r\n\t\troundingFunc = Math[roundingMethod];\r\n\r\n\tif (isNaN(arg)) {\r\n\t\tthrow new TypeError(INVALID_NUMBER);\r\n\t}\r\n\r\n\tif (typeof roundingFunc !== FUNCTION) {\r\n\t\tthrow new TypeError(INVALID_ROUND);\r\n\t}\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] = strings.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 && exponent === -1) {\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 ? SI_KBIT : SI_KBYTE : strings.symbol[standard][bits ? BITS : BYTES][e];\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(PERIOD, separator);\r\n\t}\r\n\r\n\tif (pad && Number.isInteger(result[0]) === false && round > 0) {\r\n\t\tconst x = separator || PERIOD,\r\n\t\t\ttmp = result[0].toString().split(x),\r\n\t\t\ts = tmp[1] || EMPTY,\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, ZERO)}`;\r\n\t}\r\n\r\n\tif (full) {\r\n\t\tresult[1] = fullforms[e] ? fullforms[e] : strings.fullform[standard][e] + (bits ? BIT : BYTE) + (result[0] === 1 ? EMPTY : 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":["strings","symbol","iec","bits","bytes","jedec","fullform","filesize","arg","pad","base","round","locale","localeOptions","separator","spacer","symbols","standard","output","fullforms","exponent","roundingMethod","precision","e","num","Number","result","val","u","length","ceil","full","neg","roundingFunc","Math","isNaN","TypeError","floor","log","pow","p","toPrecision","toLocaleString","toString","replace","isInteger","x","tmp","split","s","l","n","padEnd","value","unit","join","partial","opt"],"mappings":";;;;AAEO,MCAMA,EAAU,CACtBC,OAAQ,CACPC,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,SAAU,CACTJ,IAAK,CAAC,GAAI,OAAQ,OAAQ,OAAQ,OAAQ,OAAQ,OAAQ,OAAQ,QAClEG,MAAO,CAAC,GAAI,OAAQ,OAAQ,OAAQ,OAAQ,OAAQ,MAAO,QAAS,WCFtE,SAASE,EAAUC,GAAKL,KAACA,GAAO,EAAKM,IAAEA,GAAM,EAAKC,KAAEA,GAAO,EAAEC,MAAEA,EAAQ,EAACC,OAAEA,EFNrD,GEMmEC,cAAEA,EAAgB,GAAEC,UAAEA,EFNzF,GEM0GC,OAAEA,EFO5G,IEP0HC,QAAEA,EAAU,GAAEC,SAAEA,EFN1I,GEM0JC,OAAEA,EFQ3J,SER0KZ,SAAEA,GAAW,EAAKa,UAAEA,EAAY,GAAEC,SAAEA,GAAW,EAAEC,eAAEA,EFG9N,QEHoPC,UAAEA,EAAY,GAAK,IAC3R,IAAIC,EAAIH,EACPI,EAAMC,OAAOjB,GACbkB,EAAS,GACTC,EAAM,EACNC,EFXmB,IEcN,IAAVlB,GAAmC,IAApBO,EAASY,QAC3BnB,EAAO,GACPO,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AMA,EAAO,IAIjBJ,EAAY,IACfI,EAAO,GAAKA,EAAO,GAAGe,YAAYnB,IAInCI,EAAO,GAAKV,EAAQU,EAAO,KAAOA,EAAO,IAE1B,IAAXd,EACHc,EAAO,GAAKA,EAAO,GAAGgB,iBACZ9B,EAAOiB,OAAS,EAC1BH,EAAO,GAAKA,EAAO,GAAGgB,eAAe9B,EAAQC,GACnCC,EAAUe,OAAS,IAC7BH,EAAO,GAAKA,EAAO,GAAGiB,WAAWC,QFtGb,IEsG6B9B,IAG9CL,IAAuC,IAAhCgB,OAAOoB,UAAUnB,EAAO,KAAiBf,EAAQ,EAAG,CAC9D,MAAMmC,EAAIhC,GF1GU,IE2GnBiC,EAAMrB,EAAO,GAAGiB,WAAWK,MAAMF,GACjCG,EAAIF,EAAI,IFpHU,GEqHlBG,EAAID,EAAEpB,OACNsB,EAAIxC,EAAQuC,EAEbxB,EAAO,GAAK,GAAGqB,EAAI,KAAKD,IAAIG,EAAEG,OAAOF,EAAIC,EFzGvB,OEiHnB,OALIpB,IACHL,EAAO,GAAKP,EAAUI,GAAKJ,EAAUI,GAAKvB,EAAQM,SAASW,GAAUM,IAAMpB,EFhI1D,MAEC,SE8H6F,IAAduB,EAAO,GF5HrF,GAUJ,MAfI,UEqIbR,EAAmBQ,EFzHL,WEyHcR,EAAoB,CAACmC,MAAO3B,EAAO,GAAIzB,OAAQyB,EAAO,GAAIN,SAAUG,EAAG+B,KAAM1B,GAAKF,EAAO6B,KAAKxC,GAIlIR,EAASiD,QAAUC,GAAOjD,GAAOD,EAASC,EAAKiD"} \ No newline at end of file diff --git a/lib/filesize.js b/lib/filesize.js index 95f85f7..5f67073 100644 --- a/lib/filesize.js +++ b/lib/filesize.js @@ -6,206 +6,238 @@ * @version 9.0.8 */ (function (global, factory) { - typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() : - typeof define === 'function' && define.amd ? define(factory) : - (global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.filesize = factory()); + typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() : + typeof define === 'function' && define.amd ? define(factory) : + (global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.filesize = factory()); })(this, (function () { 'use strict'; - var strings = { - symbol: { - 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"] - } - }, - fullform: { - iec: ["", "kibi", "mebi", "gibi", "tebi", "pebi", "exbi", "zebi", "yobi"], - jedec: ["", "kilo", "mega", "giga", "tera", "peta", "exa", "zetta", "yotta"] - } - }; - - /** - * filesize - * - * @method filesize - * @param {Mixed} arg String, Int or Float to transform - * @param {Object} descriptor [Optional] Flags - * @return {String} Readable file size String - */ - - function filesize(arg) { - var _ref = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}, - _ref$bits = _ref.bits, - bits = _ref$bits === void 0 ? false : _ref$bits, - _ref$pad = _ref.pad, - pad = _ref$pad === void 0 ? false : _ref$pad, - _ref$base = _ref.base, - base = _ref$base === void 0 ? -1 : _ref$base, - _ref$round = _ref.round, - round = _ref$round === void 0 ? 2 : _ref$round, - _ref$locale = _ref.locale, - locale = _ref$locale === void 0 ? "" : _ref$locale, - _ref$localeOptions = _ref.localeOptions, - localeOptions = _ref$localeOptions === void 0 ? {} : _ref$localeOptions, - _ref$separator = _ref.separator, - separator = _ref$separator === void 0 ? "" : _ref$separator, - _ref$spacer = _ref.spacer, - spacer = _ref$spacer === void 0 ? " " : _ref$spacer, - _ref$symbols = _ref.symbols, - symbols = _ref$symbols === void 0 ? {} : _ref$symbols, - _ref$standard = _ref.standard, - standard = _ref$standard === void 0 ? "" : _ref$standard, - _ref$output = _ref.output, - output = _ref$output === void 0 ? "string" : _ref$output, - _ref$fullform = _ref.fullform, - fullform = _ref$fullform === void 0 ? false : _ref$fullform, - _ref$fullforms = _ref.fullforms, - fullforms = _ref$fullforms === void 0 ? [] : _ref$fullforms, - _ref$exponent = _ref.exponent, - exponent = _ref$exponent === void 0 ? -1 : _ref$exponent, - _ref$roundingMethod = _ref.roundingMethod, - roundingMethod = _ref$roundingMethod === void 0 ? "round" : _ref$roundingMethod, - _ref$precision = _ref.precision, - precision = _ref$precision === void 0 ? 0 : _ref$precision; - - var e = exponent, - num = Number(arg), - result = [], - val = 0, - u = ""; // Sync base & standard - - if (base === -1 && standard.length === 0) { - base = 10; - standard = "iec"; - } else if (base === -1 && standard.length > 0) { - standard = standard === "iec" ? "iec" : "jedec"; - base = standard === "iec" ? 10 : 2; - } else { - base = base === 2 ? 2 : 10; - standard = base === 10 ? "iec" : "jedec"; - } - - var ceil = base === 10 ? 1000 : 1024, - full = fullform === true, - neg = num < 0, - roundingFunc = Math[roundingMethod]; - - if (isNaN(arg)) { - throw new TypeError("Invalid number"); - } - - if (typeof roundingFunc !== "function") { - throw new TypeError("Invalid rounding method"); - } // Flipping a negative number to determine the size - - - if (neg) { - num = -num; - } // Determining the exponent - - - if (e === -1 || isNaN(e)) { - e = Math.floor(Math.log(num) / Math.log(ceil)); - - if (e < 0) { - e = 0; - } - } // Exceeding supported length, time to reduce & multiply - - - if (e > 8) { - if (precision > 0) { - precision += 8 - e; - } - - e = 8; - } - - if (output === "exponent") { - return e; - } // Zero is now a special case because bytes divide by 1 - - - if (num === 0) { - result[0] = 0; - u = result[1] = strings.symbol[standard][bits ? "bits" : "bytes"][e]; - } else { - val = num / (base === 2 ? Math.pow(2, e * 10) : Math.pow(1000, e)); - - if (bits) { - val = val * 8; - - if (val >= ceil && e < 8) { - val = val / ceil; - e++; - } - } - - var p = Math.pow(10, e > 0 ? round : 0); - result[0] = roundingFunc(val * p) / p; - - if (result[0] === ceil && e < 8 && exponent === -1) { - result[0] = 1; - e++; - } - - u = result[1] = strings.symbol[standard][bits ? "bits" : "bytes"][e]; - } // Decorating a 'diff' - - - if (neg) { - result[0] = -result[0]; - } // Setting optional precision - - - if (precision > 0) { - result[0] = result[0].toPrecision(precision); - } // Applying custom symbol + function _typeof(obj) { + "@babel/helpers - typeof"; + + return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { + return typeof obj; + } : function (obj) { + return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; + }, _typeof(obj); + } + + var ARRAY = "array"; + var BIT = "bit"; + var BITS = "bits"; + var BYTE = "byte"; + var BYTES = "bytes"; + var EMPTY = ""; + var EXPONENT = "exponent"; + var FUNCTION = "function"; + var IEC = "iec"; + var INVALID_NUMBER = "Invalid number"; + var INVALID_ROUND = "Invalid rounding method"; + var JEDEC = "jedec"; + var OBJECT = "object"; + var PERIOD = "."; + var ROUND = "round"; + var S = "s"; + var SI_KBIT = "kbit"; + var SI_KBYTE = "kB"; + var SPACE = " "; + var STRING = "string"; + var ZERO = "0"; + + var strings = { + symbol: { + 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"] + } + }, + fullform: { + iec: ["", "kibi", "mebi", "gibi", "tebi", "pebi", "exbi", "zebi", "yobi"], + jedec: ["", "kilo", "mega", "giga", "tera", "peta", "exa", "zetta", "yotta"] + } + }; + + /** + * filesize + * + * @method filesize + * @param {Mixed} arg String, Int or Float to transform + * @param {Object} descriptor [Optional] Flags + * @return {String} Readable file size String + */ + + function filesize(arg) { + var _ref = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}, + _ref$bits = _ref.bits, + bits = _ref$bits === void 0 ? false : _ref$bits, + _ref$pad = _ref.pad, + pad = _ref$pad === void 0 ? false : _ref$pad, + _ref$base = _ref.base, + base = _ref$base === void 0 ? -1 : _ref$base, + _ref$round = _ref.round, + round = _ref$round === void 0 ? 2 : _ref$round, + _ref$locale = _ref.locale, + locale = _ref$locale === void 0 ? EMPTY : _ref$locale, + _ref$localeOptions = _ref.localeOptions, + localeOptions = _ref$localeOptions === void 0 ? {} : _ref$localeOptions, + _ref$separator = _ref.separator, + separator = _ref$separator === void 0 ? EMPTY : _ref$separator, + _ref$spacer = _ref.spacer, + spacer = _ref$spacer === void 0 ? SPACE : _ref$spacer, + _ref$symbols = _ref.symbols, + symbols = _ref$symbols === void 0 ? {} : _ref$symbols, + _ref$standard = _ref.standard, + standard = _ref$standard === void 0 ? EMPTY : _ref$standard, + _ref$output = _ref.output, + output = _ref$output === void 0 ? STRING : _ref$output, + _ref$fullform = _ref.fullform, + fullform = _ref$fullform === void 0 ? false : _ref$fullform, + _ref$fullforms = _ref.fullforms, + fullforms = _ref$fullforms === void 0 ? [] : _ref$fullforms, + _ref$exponent = _ref.exponent, + exponent = _ref$exponent === void 0 ? -1 : _ref$exponent, + _ref$roundingMethod = _ref.roundingMethod, + roundingMethod = _ref$roundingMethod === void 0 ? ROUND : _ref$roundingMethod, + _ref$precision = _ref.precision, + precision = _ref$precision === void 0 ? 0 : _ref$precision; + + var e = exponent, + num = Number(arg), + result = [], + val = 0, + u = EMPTY; // Sync base & standard + + if (base === -1 && standard.length === 0) { + base = 10; + standard = JEDEC; + } else if (base === -1 && standard.length > 0) { + standard = standard === IEC ? IEC : JEDEC; + base = standard === IEC ? 2 : 10; + } else { + base = base === 2 ? 2 : 10; + standard = base === 10 ? JEDEC : IEC; + } + + var ceil = base === 10 ? 1000 : 1024, + full = fullform === true, + neg = num < 0, + roundingFunc = Math[roundingMethod]; + + if (isNaN(arg)) { + throw new TypeError(INVALID_NUMBER); + } + + if (_typeof(roundingFunc) !== FUNCTION) { + throw new TypeError(INVALID_ROUND); + } // Flipping a negative number to determine the size + + + if (neg) { + num = -num; + } // Determining the exponent + + + if (e === -1 || isNaN(e)) { + e = Math.floor(Math.log(num) / Math.log(ceil)); + + if (e < 0) { + e = 0; + } + } // Exceeding supported length, time to reduce & multiply + + + if (e > 8) { + if (precision > 0) { + precision += 8 - e; + } + + e = 8; + } + + if (output === EXPONENT) { + return e; + } // Zero is now a special case because bytes divide by 1 + + + if (num === 0) { + result[0] = 0; + u = result[1] = strings.symbol[standard][bits ? BITS : BYTES][e]; + } else { + val = num / (base === 2 ? Math.pow(2, e * 10) : Math.pow(1000, e)); + + if (bits) { + val = val * 8; + + if (val >= ceil && e < 8) { + val = val / ceil; + e++; + } + } + + var p = Math.pow(10, e > 0 ? round : 0); + result[0] = roundingFunc(val * p) / p; + + if (result[0] === ceil && e < 8 && exponent === -1) { + result[0] = 1; + e++; + } + + u = result[1] = base === 10 && e === 1 ? bits ? SI_KBIT : SI_KBYTE : strings.symbol[standard][bits ? BITS : BYTES][e]; + } // Decorating a 'diff' + + + if (neg) { + 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]; + result[1] = symbols[result[1]] || result[1]; - if (locale === true) { - result[0] = result[0].toLocaleString(); - } else if (locale.length > 0) { - result[0] = result[0].toLocaleString(locale, localeOptions); - } else if (separator.length > 0) { - result[0] = result[0].toString().replace(".", separator); - } + if (locale === true) { + result[0] = result[0].toLocaleString(); + } else if (locale.length > 0) { + result[0] = result[0].toLocaleString(locale, localeOptions); + } else if (separator.length > 0) { + result[0] = result[0].toString().replace(PERIOD, separator); + } - if (pad && Number.isInteger(result[0]) === false && round > 0) { - var x = separator || ".", - tmp = result[0].toString().split(x), - s = tmp[1] || "", - l = s.length, - n = round - l; - result[0] = "".concat(tmp[0]).concat(x).concat(s.padEnd(l + n, "0")); - } + if (pad && Number.isInteger(result[0]) === false && round > 0) { + var x = separator || PERIOD, + tmp = result[0].toString().split(x), + s = tmp[1] || EMPTY, + l = s.length, + n = round - l; + result[0] = "".concat(tmp[0]).concat(x).concat(s.padEnd(l + n, ZERO)); + } - if (full) { - result[1] = fullforms[e] ? fullforms[e] : strings.fullform[standard][e] + (bits ? "bit" : "byte") + (result[0] === 1 ? "" : "s"); - } // Returning Array, Object, or String (default) + if (full) { + result[1] = fullforms[e] ? fullforms[e] : strings.fullform[standard][e] + (bits ? BIT : BYTE) + (result[0] === 1 ? EMPTY : S); + } // Returning Array, Object, or String (default) - return output === "array" ? result : output === "object" ? { - value: result[0], - symbol: result[1], - exponent: e, - unit: u - } : result.join(spacer); - } // Partial application for functional programming + return output === ARRAY ? result : output === OBJECT ? { + value: result[0], + symbol: result[1], + exponent: e, + unit: u + } : result.join(spacer); + } // Partial application for functional programming - filesize.partial = function (opt) { - return function (arg) { - return filesize(arg, opt); - }; - }; + filesize.partial = function (opt) { + return function (arg) { + return filesize(arg, opt); + }; + }; - return filesize; + return filesize; })); diff --git a/lib/filesize.min.js b/lib/filesize.min.js index c7836d1..bc45a2c 100644 --- a/lib/filesize.min.js +++ b/lib/filesize.min.js @@ -2,5 +2,5 @@ 2022 Jason Mulligan @version 9.0.8 */ -!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={symbol:{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"]}},fullform:{iec:["","kibi","mebi","gibi","tebi","pebi","exbi","zebi","yobi"],jedec:["","kilo","mega","giga","tera","peta","exa","zetta","yotta"]}};function t(t){var e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},o=e.bits,n=void 0!==o&&o,r=e.pad,b=void 0!==r&&r,a=e.base,d=void 0===a?-1:a,l=e.round,s=void 0===l?2:l,c=e.locale,f=void 0===c?"":c,u=e.localeOptions,p=void 0===u?{}:u,v=e.separator,g=void 0===v?"":v,h=e.spacer,y=void 0===h?" ":h,m=e.symbols,B=void 0===m?{}:m,M=e.standard,T=void 0===M?"":M,j=e.output,w=void 0===j?"string":j,x=e.fullform,E=void 0!==x&&x,N=e.fullforms,P=void 0===N?[]:N,G=e.exponent,K=void 0===G?-1:G,S=e.roundingMethod,Y=void 0===S?"round":S,Z=e.precision,z=void 0===Z?0:Z,I=K,k=Number(t),L=[],O=0,q="";-1===d&&0===T.length?(d=10,T="iec"):-1===d&&T.length>0?d="iec"===(T="iec"===T?"iec":"jedec")?10:2:T=10===(d=2===d?2:10)?"iec":"jedec";var A=10===d?1e3:1024,C=!0===E,D=k<0,F=Math[Y];if(isNaN(t))throw new TypeError("Invalid number");if("function"!=typeof F)throw new TypeError("Invalid rounding method");if(D&&(k=-k),(-1===I||isNaN(I))&&(I=Math.floor(Math.log(k)/Math.log(A)))<0&&(I=0),I>8&&(z>0&&(z+=8-I),I=8),"exponent"===w)return I;if(0===k)L[0]=0,q=L[1]=i.symbol[T][n?"bits":"bytes"][I];else{O=k/(2===d?Math.pow(2,10*I):Math.pow(1e3,I)),n&&(O*=8)>=A&&I<8&&(O/=A,I++);var H=Math.pow(10,I>0?s:0);L[0]=F(O*H)/H,L[0]===A&&I<8&&-1===K&&(L[0]=1,I++),q=L[1]=i.symbol[T][n?"bits":"bytes"][I]}if(D&&(L[0]=-L[0]),z>0&&(L[0]=L[0].toPrecision(z)),L[1]=B[L[1]]||L[1],!0===f?L[0]=L[0].toLocaleString():f.length>0?L[0]=L[0].toLocaleString(f,p):g.length>0&&(L[0]=L[0].toString().replace(".",g)),b&&!1===Number.isInteger(L[0])&&s>0){var J=g||".",Q=L[0].toString().split(J),R=Q[1]||"",U=R.length,V=s-U;L[0]="".concat(Q[0]).concat(J).concat(R.padEnd(U+V,"0"))}return C&&(L[1]=P[I]?P[I]:i.fullform[T][I]+(n?"bit":"byte")+(1===L[0]?"":"s")),"array"===w?L:"object"===w?{value:L[0],symbol:L[1],exponent:I,unit:q}:L.join(y)}return t.partial=function(i){return function(e){return t(e,i)}},t})); +!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";function i(t){return i="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(i){return typeof i}:function(i){return i&&"function"==typeof Symbol&&i.constructor===Symbol&&i!==Symbol.prototype?"symbol":typeof i},i(t)}var t="array",o="bits",e="byte",n="bytes",r="",b="exponent",l="function",a="iec",d="Invalid number",f="Invalid rounding method",u="jedec",s="object",c=".",p="round",y="kbit",m="string",v={symbol:{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"]}},fullform:{iec:["","kibi","mebi","gibi","tebi","pebi","exbi","zebi","yobi"],jedec:["","kilo","mega","giga","tera","peta","exa","zetta","yotta"]}};function g(g){var h=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},B=h.bits,M=void 0!==B&&B,S=h.pad,T=void 0!==S&&S,w=h.base,x=void 0===w?-1:w,E=h.round,j=void 0===E?2:E,N=h.locale,P=void 0===N?r:N,k=h.localeOptions,G=void 0===k?{}:k,K=h.separator,Y=void 0===K?r:K,Z=h.spacer,z=void 0===Z?" ":Z,I=h.symbols,L=void 0===I?{}:I,O=h.standard,q=void 0===O?r:O,A=h.output,C=void 0===A?m:A,D=h.fullform,F=void 0!==D&&D,H=h.fullforms,J=void 0===H?[]:H,Q=h.exponent,R=void 0===Q?-1:Q,U=h.roundingMethod,V=void 0===U?p:U,W=h.precision,X=void 0===W?0:W,$=R,_=Number(g),ii=[],ti=0,oi=r;-1===x&&0===q.length?(x=10,q=u):-1===x&&q.length>0?x=(q=q===a?a:u)===a?2:10:q=10===(x=2===x?2:10)?u:a;var ei=10===x?1e3:1024,ni=!0===F,ri=_<0,bi=Math[V];if(isNaN(g))throw new TypeError(d);if(i(bi)!==l)throw new TypeError(f);if(ri&&(_=-_),(-1===$||isNaN($))&&($=Math.floor(Math.log(_)/Math.log(ei)))<0&&($=0),$>8&&(X>0&&(X+=8-$),$=8),C===b)return $;if(0===_)ii[0]=0,oi=ii[1]=v.symbol[q][M?o:n][$];else{ti=_/(2===x?Math.pow(2,10*$):Math.pow(1e3,$)),M&&(ti*=8)>=ei&&$<8&&(ti/=ei,$++);var li=Math.pow(10,$>0?j:0);ii[0]=bi(ti*li)/li,ii[0]===ei&&$<8&&-1===R&&(ii[0]=1,$++),oi=ii[1]=10===x&&1===$?M?y:"kB":v.symbol[q][M?o:n][$]}if(ri&&(ii[0]=-ii[0]),X>0&&(ii[0]=ii[0].toPrecision(X)),ii[1]=L[ii[1]]||ii[1],!0===P?ii[0]=ii[0].toLocaleString():P.length>0?ii[0]=ii[0].toLocaleString(P,G):Y.length>0&&(ii[0]=ii[0].toString().replace(c,Y)),T&&!1===Number.isInteger(ii[0])&&j>0){var ai=Y||c,di=ii[0].toString().split(ai),fi=di[1]||r,ui=fi.length,si=j-ui;ii[0]="".concat(di[0]).concat(ai).concat(fi.padEnd(ui+si,"0"))}return ni&&(ii[1]=J[$]?J[$]:v.fullform[q][$]+(M?"bit":e)+(1===ii[0]?r:"s")),C===t?ii:C===s?{value:ii[0],symbol:ii[1],exponent:$,unit:oi}:ii.join(z)}return g.partial=function(i){return function(t){return g(t,i)}},g})); //# sourceMappingURL=filesize.min.js.map diff --git a/lib/filesize.min.js.map b/lib/filesize.min.js.map index acd2fc2..02748b4 100644 --- a/lib/filesize.min.js.map +++ b/lib/filesize.min.js.map @@ -1 +1 @@ -{"version":3,"file":"filesize.min.js","sources":["../src/strings.js","../src/filesize.js"],"sourcesContent":["\"use strict\";\r\n\r\nexport const strings = {\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};\r\n","\"use strict\";\r\n\r\nimport {strings} from \"./strings\";\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, {bits = false, pad = false, base = -1, round = 2, locale = \"\", localeOptions = {}, separator = \"\", spacer = \" \", symbols = {}, standard = \"\", output = \"string\", fullform = false, fullforms = [], exponent = -1, roundingMethod = \"round\", precision = 0} = {}) {\r\n\tlet e = exponent,\r\n\t\tnum = Number(arg),\r\n\t\tresult = [],\r\n\t\tval = 0,\r\n\t\tu = \"\";\r\n\r\n\t// Sync base & standard\r\n\tif (base === -1 && standard.length === 0) {\r\n\t\tbase = 10;\r\n\t\tstandard = \"iec\";\r\n\t} else if (base === -1 && standard.length > 0) {\r\n\t\tstandard = standard === \"iec\" ? \"iec\" : \"jedec\";\r\n\t\tbase = standard === \"iec\" ? 10 : 2;\r\n\t} else {\r\n\t\tbase = base === 2 ? 2 : 10;\r\n\t\tstandard = base === 10 ? \"iec\" : \"jedec\";\r\n\t}\r\n\r\n\tconst ceil = base === 10 ? 1000 : 1024,\r\n\t\tfull = fullform === true,\r\n\t\tneg = num < 0,\r\n\t\troundingFunc = Math[roundingMethod];\r\n\r\n\tif (isNaN(arg)) {\r\n\t\tthrow new TypeError(\"Invalid number\");\r\n\t}\r\n\r\n\tif (typeof roundingFunc !== \"function\") {\r\n\t\tthrow new TypeError(\"Invalid rounding method\");\r\n\t}\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] = strings.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 && exponent === -1) {\r\n\t\t\tresult[0] = 1;\r\n\t\t\te++;\r\n\t\t}\r\n\r\n\t\tu = result[1] = strings.symbol[standard][bits ? \"bits\" : \"bytes\"][e];\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] : strings.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":["strings","symbol","iec","bits","bytes","jedec","fullform","filesize","arg","_ref","arguments","length","undefined","_ref$bits","_ref$pad","pad","_ref$base","base","_ref$round","round","_ref$locale","locale","_ref$localeOptions","localeOptions","_ref$separator","separator","_ref$spacer","spacer","_ref$symbols","symbols","_ref$standard","standard","_ref$output","output","_ref$fullform","_ref$fullforms","fullforms","_ref$exponent","exponent","_ref$roundingMethod","roundingMethod","_ref$precision","precision","e","num","Number","result","val","u","ceil","full","neg","roundingFunc","Math","isNaN","TypeError","floor","log","pow","p","toPrecision","toLocaleString","toString","replace","isInteger","x","tmp","split","s","l","n","concat","padEnd","value","unit","join","partial","opt"],"mappings":";;;;yOAEO,IAAMA,EAAU,CACtBC,OAAQ,CACPC,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,SAAU,CACTJ,IAAK,CAAC,GAAI,OAAQ,OAAQ,OAAQ,OAAQ,OAAQ,OAAQ,OAAQ,QAClEG,MAAO,CAAC,GAAI,OAAQ,OAAQ,OAAQ,OAAQ,OAAQ,MAAO,QAAS,WCHtE,SAASE,EAAUC,GAAsQ,IAAAC,EAAAC,UAAAC,OAAA,QAAAC,IAAAF,UAAA,GAAAA,UAAA,GAAJ,GAAIG,EAAAJ,EAAhQN,KAAAA,cAAgQU,EAAAC,EAAAL,EAAlPM,IAAAA,cAAkPD,EAAAE,EAAAP,EAArOQ,KAAAA,OAAO,IAAAD,GAAC,EAA6NA,EAAAE,EAAAT,EAA1NU,MAAAA,aAAQ,EAAkND,EAAAE,EAAAX,EAA/MY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,EAAO,GAAKV,EAAUO,GAAKP,EAAUO,GAAK3C,EAAQM,SAASyB,GAAUY,IAAMxC,EAAO,MAAQ,SAAyB,IAAd2C,EAAO,GAAW,GAAK,MAI3G,UAAXb,EAAqBa,EAAoB,WAAXb,EAAsB,CAACwC,MAAO3B,EAAO,GAAI7C,OAAQ6C,EAAO,GAAIR,SAAUK,EAAG+B,KAAM1B,GAAKF,EAAO6B,KAAKhD,UAItIpB,EAASqE,QAAU,SAAAC,GAAG,OAAI,SAAArE,GAAG,OAAID,EAASC,EAAKqE"} \ No newline at end of file +{"version":3,"file":"filesize.min.js","sources":["../src/constants.js","../src/strings.js","../src/filesize.js"],"sourcesContent":["\"use strict\";\r\n\r\nexport const ARRAY = \"array\";\r\nexport const BIT = \"bit\";\r\nexport const BITS = \"bits\";\r\nexport const BYTE = \"byte\";\r\nexport const BYTES = \"bytes\";\r\nexport const EMPTY = \"\";\r\nexport const EXPONENT = \"exponent\";\r\nexport const FUNCTION = \"function\";\r\nexport const IEC = \"iec\";\r\nexport const INVALID_NUMBER = \"Invalid number\";\r\nexport const INVALID_ROUND = \"Invalid rounding method\";\r\nexport const JEDEC = \"jedec\";\r\nexport const OBJECT = \"object\";\r\nexport const PERIOD = \".\";\r\nexport const ROUND = \"round\";\r\nexport const S = \"s\";\r\nexport const SI_KBIT = \"kbit\";\r\nexport const SI_KBYTE = \"kB\";\r\nexport const SPACE = \" \";\r\nexport const STRING = \"string\";\r\nexport const ZERO = \"0\";\r\n","\"use strict\";\r\n\r\nexport const strings = {\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};\r\n","\"use strict\";\r\n\r\nimport {BITS, BYTES, EMPTY, EXPONENT, FUNCTION, IEC, INVALID_NUMBER, INVALID_ROUND, JEDEC, SI_KBIT, SI_KBYTE, SPACE, PERIOD, ZERO, ARRAY, OBJECT, S, BIT, BYTE, STRING, ROUND} from \"./constants\";\r\nimport {strings} from \"./strings\";\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, {bits = false, pad = false, base = -1, round = 2, locale = EMPTY, localeOptions = {}, separator = EMPTY, spacer = SPACE, symbols = {}, standard = EMPTY, output = STRING, fullform = false, fullforms = [], exponent = -1, roundingMethod = ROUND, precision = 0} = {}) {\r\n\tlet e = exponent,\r\n\t\tnum = Number(arg),\r\n\t\tresult = [],\r\n\t\tval = 0,\r\n\t\tu = EMPTY;\r\n\r\n\t// Sync base & standard\r\n\tif (base === -1 && standard.length === 0) {\r\n\t\tbase = 10;\r\n\t\tstandard = JEDEC;\r\n\t} else if (base === -1 && standard.length > 0) {\r\n\t\tstandard = standard === IEC ? IEC : JEDEC;\r\n\t\tbase = standard === IEC ? 2 : 10;\r\n\t} else {\r\n\t\tbase = base === 2 ? 2 : 10;\r\n\t\tstandard = base === 10 ? JEDEC : IEC;\r\n\t}\r\n\r\n\tconst ceil = base === 10 ? 1000 : 1024,\r\n\t\tfull = fullform === true,\r\n\t\tneg = num < 0,\r\n\t\troundingFunc = Math[roundingMethod];\r\n\r\n\tif (isNaN(arg)) {\r\n\t\tthrow new TypeError(INVALID_NUMBER);\r\n\t}\r\n\r\n\tif (typeof roundingFunc !== FUNCTION) {\r\n\t\tthrow new TypeError(INVALID_ROUND);\r\n\t}\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] = strings.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 && exponent === -1) {\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 ? SI_KBIT : SI_KBYTE : strings.symbol[standard][bits ? BITS : BYTES][e];\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(PERIOD, separator);\r\n\t}\r\n\r\n\tif (pad && Number.isInteger(result[0]) === false && round > 0) {\r\n\t\tconst x = separator || PERIOD,\r\n\t\t\ttmp = result[0].toString().split(x),\r\n\t\t\ts = tmp[1] || EMPTY,\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, ZERO)}`;\r\n\t}\r\n\r\n\tif (full) {\r\n\t\tresult[1] = fullforms[e] ? fullforms[e] : strings.fullform[standard][e] + (bits ? BIT : BYTE) + (result[0] === 1 ? EMPTY : 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":["ARRAY","BITS","BYTE","BYTES","EMPTY","EXPONENT","FUNCTION","IEC","INVALID_NUMBER","INVALID_ROUND","JEDEC","OBJECT","PERIOD","ROUND","SI_KBIT","STRING","strings","symbol","iec","bits","bytes","jedec","fullform","filesize","arg","_ref","arguments","length","undefined","_ref$bits","_ref$pad","pad","_ref$base","base","_ref$round","round","_ref$locale","locale","_ref$localeOptions","localeOptions","_ref$separator","separator","_ref$spacer","spacer","_ref$symbols","symbols","_ref$standard","standard","_ref$output","output","_ref$fullform","_ref$fullforms","fullforms","_ref$exponent","exponent","_ref$roundingMethod","roundingMethod","_ref$precision","precision","e","num","Number","result","val","u","ceil","full","neg","roundingFunc","Math","isNaN","TypeError","_typeof","floor","log","pow","p","toPrecision","toLocaleString","toString","replace","isInteger","x","tmp","split","s","l","n","concat","padEnd","value","unit","join","partial","opt"],"mappings":";;;;+cAEO,IAAMA,EAAQ,QAERC,EAAO,OACPC,EAAO,OACPC,EAAQ,QACRC,EAAQ,GACRC,EAAW,WACXC,EAAW,WACXC,EAAM,MACNC,EAAiB,iBACjBC,EAAgB,0BAChBC,EAAQ,QACRC,EAAS,SACTC,EAAS,IACTC,EAAQ,QAERC,EAAU,OAGVC,EAAS,SCnBTC,EAAU,CACtBC,OAAQ,CACPC,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,SAAU,CACTJ,IAAK,CAAC,GAAI,OAAQ,OAAQ,OAAQ,OAAQ,OAAQ,OAAQ,OAAQ,QAClEG,MAAO,CAAC,GAAI,OAAQ,OAAQ,OAAQ,OAAQ,OAAQ,MAAO,QAAS,WCFtE,SAASE,EAAUC,GAA6Q,IAAAC,EAAAC,UAAAC,OAAA,QAAAC,IAAAF,UAAA,GAAAA,UAAA,GAAJ,GAAIG,EAAAJ,EAAvQN,KAAAA,cAAuQU,EAAAC,EAAAL,EAAzPM,IAAAA,cAAyPD,EAAAE,EAAAP,EAA5OQ,KAAAA,OAAO,IAAAD,GAAC,EAAoOA,EAAAE,EAAAT,EAAjOU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GAAI,IAAM/E,EACdkF,GAAID,GAAE1D,OACN4D,GAAIpD,EAAQmD,GAEbxB,GAAO,GAAP,GAAA0B,OAAeL,GAAI,IAAKD,OAAAA,WAAIG,GAAEI,OAAOH,GAAIC,GFzGvB,MEiHnB,OALIrB,KACHJ,GAAO,GAAKV,EAAUO,GAAKP,EAAUO,GAAK3C,EAAQM,SAASyB,GAAUY,IAAMxC,EFhI1D,MEgIuEjB,IAAuB,IAAd4D,GAAO,GAAW1D,EFlHpG,MEsHT6C,IAAWjD,EAAQ8D,GAASb,IAAWtC,EAAS,CAAC+E,MAAO5B,GAAO,GAAI7C,OAAQ6C,GAAO,GAAIR,SAAUK,EAAGgC,KAAM3B,IAAKF,GAAO8B,KAAKjD,UAIlIpB,EAASsE,QAAU,SAAAC,GAAG,OAAI,SAAAtE,GAAG,OAAID,EAASC,EAAKsE"} \ No newline at end of file diff --git a/src/constants.js b/src/constants.js new file mode 100644 index 0000000..218ba08 --- /dev/null +++ b/src/constants.js @@ -0,0 +1,23 @@ +"use strict"; + +export const ARRAY = "array"; +export const BIT = "bit"; +export const BITS = "bits"; +export const BYTE = "byte"; +export const BYTES = "bytes"; +export const EMPTY = ""; +export const EXPONENT = "exponent"; +export const FUNCTION = "function"; +export const IEC = "iec"; +export const INVALID_NUMBER = "Invalid number"; +export const INVALID_ROUND = "Invalid rounding method"; +export const JEDEC = "jedec"; +export const OBJECT = "object"; +export const PERIOD = "."; +export const ROUND = "round"; +export const S = "s"; +export const SI_KBIT = "kbit"; +export const SI_KBYTE = "kB"; +export const SPACE = " "; +export const STRING = "string"; +export const ZERO = "0"; diff --git a/src/filesize.js b/src/filesize.js index af49521..6b81d2d 100644 --- a/src/filesize.js +++ b/src/filesize.js @@ -1,5 +1,28 @@ "use strict"; +import { + ARRAY, + BIT, + BITS, + BYTE, + BYTES, + EMPTY, + EXPONENT, + FUNCTION, + IEC, + INVALID_NUMBER, + INVALID_ROUND, + JEDEC, + OBJECT, + PERIOD, + ROUND, + S, + SI_KBIT, + SI_KBYTE, + SPACE, + STRING, + ZERO +} from "./constants"; import {strings} from "./strings"; /** @@ -10,23 +33,40 @@ import {strings} from "./strings"; * @param {Object} descriptor [Optional] Flags * @return {String} Readable file size String */ -function filesize (arg, {bits = false, pad = false, base = -1, round = 2, locale = "", localeOptions = {}, separator = "", spacer = " ", symbols = {}, standard = "", output = "string", fullform = false, fullforms = [], exponent = -1, roundingMethod = "round", precision = 0} = {}) { +function filesize (arg, { + bits = false, + pad = false, + base = -1, + round = 2, + locale = EMPTY, + localeOptions = {}, + separator = EMPTY, + spacer = SPACE, + symbols = {}, + standard = EMPTY, + output = STRING, + fullform = false, + fullforms = [], + exponent = -1, + roundingMethod = ROUND, + precision = 0 +} = {}) { let e = exponent, num = Number(arg), result = [], val = 0, - u = ""; + u = EMPTY; // Sync base & standard if (base === -1 && standard.length === 0) { base = 10; - standard = "iec"; + standard = JEDEC; } else if (base === -1 && standard.length > 0) { - standard = standard === "iec" ? "iec" : "jedec"; - base = standard === "iec" ? 10 : 2; + standard = standard === IEC ? IEC : JEDEC; + base = standard === IEC ? 2 : 10; } else { base = base === 2 ? 2 : 10; - standard = base === 10 ? "iec" : "jedec"; + standard = base === 10 ? JEDEC : IEC; } const ceil = base === 10 ? 1000 : 1024, @@ -35,11 +75,11 @@ function filesize (arg, {bits = false, pad = false, base = -1, round = 2, locale roundingFunc = Math[roundingMethod]; if (isNaN(arg)) { - throw new TypeError("Invalid number"); + throw new TypeError(INVALID_NUMBER); } - if (typeof roundingFunc !== "function") { - throw new TypeError("Invalid rounding method"); + if (typeof roundingFunc !== FUNCTION) { + throw new TypeError(INVALID_ROUND); } // Flipping a negative number to determine the size @@ -65,14 +105,14 @@ function filesize (arg, {bits = false, pad = false, base = -1, round = 2, locale e = 8; } - if (output === "exponent") { + if (output === EXPONENT) { return e; } // Zero is now a special case because bytes divide by 1 if (num === 0) { result[0] = 0; - u = result[1] = strings.symbol[standard][bits ? "bits" : "bytes"][e]; + u = result[1] = strings.symbol[standard][bits ? BITS : BYTES][e]; } else { val = num / (base === 2 ? Math.pow(2, e * 10) : Math.pow(1000, e)); @@ -93,7 +133,7 @@ function filesize (arg, {bits = false, pad = false, base = -1, round = 2, locale e++; } - u = result[1] = strings.symbol[standard][bits ? "bits" : "bytes"][e]; + u = result[1] = base === 10 && e === 1 ? bits ? SI_KBIT : SI_KBYTE : strings.symbol[standard][bits ? BITS : BYTES][e]; } // Decorating a 'diff' @@ -114,25 +154,30 @@ function filesize (arg, {bits = false, pad = false, base = -1, round = 2, locale } else if (locale.length > 0) { result[0] = result[0].toLocaleString(locale, localeOptions); } else if (separator.length > 0) { - result[0] = result[0].toString().replace(".", separator); + result[0] = result[0].toString().replace(PERIOD, separator); } if (pad && Number.isInteger(result[0]) === false && round > 0) { - const x = separator || ".", + const x = separator || PERIOD, tmp = result[0].toString().split(x), - s = tmp[1] || "", + s = tmp[1] || EMPTY, l = s.length, n = round - l; - result[0] = `${tmp[0]}${x}${s.padEnd(l + n, "0")}`; + result[0] = `${tmp[0]}${x}${s.padEnd(l + n, ZERO)}`; } if (full) { - result[1] = fullforms[e] ? fullforms[e] : strings.fullform[standard][e] + (bits ? "bit" : "byte") + (result[0] === 1 ? "" : "s"); + result[1] = fullforms[e] ? fullforms[e] : strings.fullform[standard][e] + (bits ? BIT : BYTE) + (result[0] === 1 ? EMPTY : S); } // Returning Array, Object, or String (default) - return output === "array" ? result : output === "object" ? {value: result[0], symbol: result[1], exponent: e, unit: u} : result.join(spacer); + return output === ARRAY ? result : output === OBJECT ? { + value: result[0], + symbol: result[1], + exponent: e, + unit: u + } : result.join(spacer); } // Partial application for functional programming diff --git a/test/filesize_test.js b/test/filesize_test.js index 27f333f..afa3297 100644 --- a/test/filesize_test.js +++ b/test/filesize_test.js @@ -16,76 +16,75 @@ exports.filesize = { done(); }, base2: function (test) { - test.expect(39); - 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 Kbit", "Should be '3.9 Kbit'"); - test.equal(filesize(this.kilobit, {base: 2, bits: true}), "3.91 Kbit", "Should be '3.91 Kbit'"); - 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 KB", "Should be '1 KB'"); - test.equal(filesize(this.kibibyte, {base: 2, standard: "jedec"}), "1 KB", "Should be '1 KB'"); - test.equal(filesize(this.kibibyte, {base: 2, round: 1}), "1 KB", "Should be '1 KB'"); - test.equal(filesize(this.kibibyte, {base: 2, round: 1, spacer: ""}), "1KB", "Should be '1KB'"); - test.equal(filesize(this.kibibyte, {base: 2, bits: true}), "8 Kbit", "Should be '8 Kbit'"); - test.equal(filesize(this.kibibyte, {base: 2, round: 1, bits: true}), "8 Kbit", "Should be '8 Kbit'"); - 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, "KB", "Should be 'KB'"); - test.equal(filesize(this.neg, {base: 2}), "-1 KB", "Should be '-1 KB'"); - test.equal(filesize(this.neg, {base: 2, round: 1}), "-1 KB", "Should be '-1 KB'"); - test.equal(filesize(this.neg, {base: 2, round: 1, spacer: ""}), "-1KB", "Should be '-1KB'"); - test.equal(filesize(this.neg, {base: 2, bits: true}), "-8 Kbit", "Should be '-8 Kbit'"); - test.equal(filesize(this.neg, {base: 2, round: 1, bits: true}), "-8 Kbit", "Should be '-8 Kbit'"); - 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 YB", "Should be '82718061255302770 YB'"); - test.equal(filesize(this.huge, {base: 2, bits: true}), "661744490042422100 Ybit", "Should be '661744490042422100 Ybit'"); - 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 PB", "Should be '1 PB'"); + test.expect(38); + 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", 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", 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", 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", 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", 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.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", 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.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", 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.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.done(); }, base10: function (test) { test.expect(25); - test.equal(filesize(this.kilobit), "500 B", "Should be '500 B'"); - test.equal(filesize(this.kilobit, {round: 1}), "500 B", "Should be '500 B'"); - test.equal(filesize(this.kilobit, {round: 1, spacer: ""}), "500B", "Should be '500B'"); - test.equal(filesize(this.kilobit, {bits: true}), "4 Kibit", "Should be '4 Kibit'"); - test.equal(filesize(this.kilobit, {round: 1, bits: true}), "4 Kibit", "Should be '4 Kibit'"); - test.equal(filesize(this.kibibyte), "1.02 KiB", "Should be '1.02 KiB'"); - test.equal(filesize(this.kibibyte, {round: 1}), "1 KiB", "Should be '1 KiB'"); - test.equal(filesize(this.kibibyte, {round: 1, spacer: ""}), "1KiB", "Should be '1KiB'"); - test.equal(filesize(this.kibibyte, {bits: true}), "8.19 Kibit", "Should be '8.19 Kibit'"); - test.equal(filesize(this.kibibyte, {round: 1, bits: true}), "8.2 Kibit", "Should be '8.2 Kibit'"); - test.equal(filesize(this.neg), "-1.02 KiB", "Should be '-1.02 KiB'"); - test.equal(filesize(this.neg, {round: 1}), "-1 KiB", "Should be '-1 KiB'"); - test.equal(filesize(this.neg, {round: 1, spacer: ""}), "-1KiB", "Should be '-1KiB'"); - test.equal(filesize(this.neg, {bits: true}), "-8.19 Kibit", "Should be '-8.19 Kibit'"); - test.equal(filesize(this.neg, {round: 1, bits: true}), "-8.2 Kibit", "Should be '-8.2 Kibit'"); - test.equal(filesize(this.byte), "1 B", "Should be '1 B'"); - test.equal(filesize(this.byte, {round: 1}), "1 B", "Should be '1 B'"); - test.equal(filesize(this.byte, {round: 1, spacer: ""}), "1B", "Should be '1B'"); - test.equal(filesize(this.byte, {bits: true}), "8 bit", "Should be '8 bit'"); - test.equal(filesize(this.byte, {round: 1, bits: true}), "8 bit", "Should be '8 bit'"); - test.equal(filesize(this.zero), "0 B", "Should be '0 B'"); - test.equal(filesize(this.zero, {round: 1}), "0 B", "Should be '0 B'"); - test.equal(filesize(this.zero, {round: 1, spacer: ""}), "0B", "Should be '0B'"); - test.equal(filesize(this.zero, {bits: true}), "0 bit", "Should be '0 bit'"); - test.equal(filesize(this.zero, {round: 1, bits: true}), "0 bit", "Should be '0 bit'"); + 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, 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.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, 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.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, 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.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, 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.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, 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.done(); }, invalid: function (test) { @@ -97,103 +96,102 @@ exports.filesize = { }, symbols: function (test) { test.expect(2); - test.equal(filesize(this.byte, {symbols: {B: "Б"}}), "1 Б", "Should be '1 Б'"); - test.equal(filesize(this.kibibyte, {symbols: {B: "Б"}}), "1.02 KiB", "Should be '1.02 KiB'"); + 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.done(); }, partial: function (test) { test.expect(1); - test.size = filesize.partial({exponent: 0}); + test.size = filesize.partial({base: 10, exponent: 0}); test.equal(test.size(this.kibibyte), "1024 B", "Should be '1024 B'"); test.done(); }, bits: function (test) { test.expect(3); - test.equal(filesize(124, {bits: true}), "992 bit", "Should be '992 bit'"); - test.equal(filesize(125, {bits: true}), "1 Kibit", "Should be '1 Kibit'"); - test.equal(filesize(126, {bits: true}), "1.01 Kibit", "Should be '1.01 Kibit'"); + 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.done(); }, fullform: function (test) { - test.expect(10); - test.equal(filesize(0, {fullform: true}), "0 bytes", "Should be '0 bytes'"); - test.equal(filesize(0, {fullform: true, output: "object"}).unit, "B", "Should be 'B'"); - test.equal(filesize(1, {bits: true, fullform: true}), "8 bits", "Should be '8 bits'"); - test.equal(filesize(1, {fullform: true}), "1 byte", "Should be '1 byte'"); - test.equal(filesize(this.kibibyte, {standard: "jedec", fullform: true}), "1 kilobyte", "Should be '1 kilobyte'"); - test.equal(filesize(this.kibibyte, {fullform: true}), "1.02 kibibytes", "Should be '1.02 kibibytes'"); - test.equal(filesize(this.kibibyte, {base: 2, fullform: true }), "1 kilobyte", "Should be '1 kilobyte'"); - test.equal(filesize(this.kibibyte, {base: 2, fullform: true, output: "object"}).unit, "KB", "Should be 'KB'"); + test.expect(9); + 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 * 1.3, { base: 2, standard: "iec", fullform: true - }), "1.3 kilobytes", "Should be '1.3 kilobytes'"); - test.equal(filesize(0, {fullform: true, fullforms: ["байт"]}), "0 байт", "Should be '0 байт'"); + }), "1.3 kibibytes", "Should be '1.3 kibibytes'"); + test.equal(filesize(0, {base: 10, fullform: true, fullforms: ["байт"]}), "0 байт", "Should be '0 байт'"); test.done(); }, exponent: function (test) { test.expect(2); - test.equal(filesize(0, {exponent: 0}), "0 B", "Should be '0 B'"); - test.equal(filesize(0, {exponent: 2}), "0 MiB", "Should be '0 MiB'"); + 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.done(); }, separator: function (test) { test.expect(3); - test.equal(filesize(1040, {separator: ""}), "1.04 KiB", "Should be '1.04 KiB'"); - test.equal(filesize(1040, {separator: ","}), "1,04 KiB", "Should be '1,04 KiB'"); - test.equal(filesize(1040, {separator: ",", round: 1, pad: true}), "1,0 KiB", "Should be '1,0 KiB'"); + 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.done(); }, locale: function (test) { test.expect(3); - test.equal(filesize(1040, {locale: ""}), "1.04 KiB", "Should be '1.04 KiB'"); - test.equal(filesize(1040, {locale: true}), Number(1.04).toLocaleString() + " KiB", "Should be '" + Number(1.04).toLocaleString() + " KiB'"); - test.equal(filesize(1040, {locale: "de"}), Number(1.04).toLocaleString("de") + " KiB", "Should be '" + Number(1.04).toLocaleString("de") + " KiB'"); + 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.done(); }, localeOptions: function (test) { test.expect(4); - test.equal(filesize(this.kibibyte, {locale: "de"}), Number(1.02).toLocaleString("de") + " KiB", "Should be '" + Number(1.02).toLocaleString("de") + " KiB'"); - test.equal(filesize(this.kibibyte, {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: "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: true, localeOptions: {minimumFractionDigits: 1} - }), Number(1.02).toLocaleString(undefined, {minimumFractionDigits: 1}) + " KiB", "Should be '" + Number(1.02).toLocaleString(undefined, {minimumFractionDigits: 1}) + " KiB'"); + }), 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", localeOptions: {minimumFractionDigits: 1} - }), Number(1.02).toLocaleString("de", {minimumFractionDigits: 1}) + " KiB", "Should be '" + Number(1.02).toLocaleString("de", {minimumFractionDigits: 1}) + " KiB'"); + }), Number(1.02).toLocaleString("de", {minimumFractionDigits: 1}) + " kB", "Should be '" + Number(1.02).toLocaleString("de", {minimumFractionDigits: 1}) + " kB'"); test.done(); }, roundingMethod: function (test) { test.expect(9); - test.equal(filesize(this.kibibyte, {roundingMethod: "round"}), "1.02 KiB", "Should be '1.02 KiB'"); - test.equal(filesize(this.kibibyte, {roundingMethod: "floor"}), "1.02 KiB", "Should be '1.02 KiB'"); - test.equal(filesize(this.kibibyte, {roundingMethod: "ceil"}), "1.03 KiB", "Should be '1.03 KiB'"); - test.equal(filesize(this.kibibyte * 1.333, {roundingMethod: "round"}), "1.36 KiB", "Should be '1.36 KiB'"); - test.equal(filesize(this.kibibyte * 1.333, {roundingMethod: "floor"}), "1.36 KiB", "Should be '1.36 KiB'"); - test.equal(filesize(this.kibibyte * 1.333, {roundingMethod: "ceil"}), "1.37 KiB", "Should be '1.37 KiB'"); - test.equal(filesize(this.kibibyte * 1.456, {round: 1, roundingMethod: "round"}), "1.5 KiB", "Should be '1.5 KiB'"); - test.equal(filesize(this.kibibyte * 1.456, {round: 1, roundingMethod: "floor"}), "1.4 KiB", "Should be '1.4 KiB'"); - test.equal(filesize(this.kibibyte * 1.456, {round: 1, roundingMethod: "ceil"}), "1.5 KiB", "Should be '1.5 KiB'"); + 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.done(); }, precision: function (test) { test.expect(5); - test.equal(filesize(this.kibibyte * 1, {precision: 3}), "1.02 KiB", "Should be '1.02 KiB'"); - test.equal(filesize(this.kibibyte * this.kibibyte * 10.25, {precision: 3}), "10.8 MiB", "Should be '10.8 MiB'"); - test.equal(filesize(this.kibibyte * this.kibibyte * 10.25, {precision: "x"}), "10.75 MiB", "Should be '10.75 MiB'"); - test.equal(filesize(this.kibibyte * this.kibibyte * this.kibibyte, {precision: 3}), "1.07 GiB", "Should be '1.07 GiB'"); - test.equal(filesize(Math.pow(this.kibibyte, 10), {precision: 3}), "1e+6 YiB", "Should be '1e+6 YiB'"); + 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.done(); }, defaults: function (test) { test.expect(2); - test.equal(filesize(this.kibibyte), "1.02 KiB", "Should be '1.02 KiB'"); - test.equal(filesize(this.kibibyte, {base: 2}), "1 KB", "Should be '1 KB'"); + test.equal(filesize(this.kibibyte), "1.02 kB", "Should be '1.02 kB'"); + test.equal(filesize(this.kibibyte, {base: 2}), "1 KiB", "Should be '1 KiB'"); test.done(); } };