UNPKG

1.89 kBJavaScriptView Raw
1!function(e,i){"object"==typeof exports&&"undefined"!=typeof module?module.exports=i():"function"==typeof define&&define.amd?define(i):(e=e||self).filesize=i()}(this,(function(){"use strict";const e=/^(b|B)$/,i={iec:{bits:["b","Kib","Mib","Gib","Tib","Pib","Eib","Zib","Yib"],bytes:["B","KiB","MiB","GiB","TiB","PiB","EiB","ZiB","YiB"]},jedec:{bits:["b","Kb","Mb","Gb","Tb","Pb","Eb","Zb","Yb"],bytes:["B","KB","MB","GB","TB","PB","EB","ZB","YB"]}},b={iec:["","kibi","mebi","gibi","tebi","pebi","exbi","zebi","yobi"],jedec:["","kilo","mega","giga","tera","peta","exa","zetta","yotta"]};function t(t,o={}){if(isNaN(t))throw new TypeError("Invalid number");let n=[],a=0,B=!0===o.bits,r=!0===o.unix,d=o.base||2,s=void 0!==o.round?o.round:r?1:2,c=void 0!==o.locale?o.locale:"",f=o.localeOptions||{},u=void 0!==o.separator?o.separator:"",y=void 0!==o.spacer?o.spacer:r?"":" ",M=o.symbols||{},h=2===d&&o.standard||"jedec",l=o.output||"string",m=!0===o.fullform,p=o.fullforms instanceof Array?o.fullforms:[],v=void 0!==o.exponent?o.exponent:-1,j=Number(t),N=j<0,g=d>2?1e3:1024;return N&&(j=-j),(-1===v||isNaN(v))&&(v=Math.floor(Math.log(j)/Math.log(g)))<0&&(v=0),v>8&&(v=8),"exponent"===l?v:(0===j?(n[0]=0,n[1]=r?"":i[h][B?"bits":"bytes"][v]):(a=j/(2===d?Math.pow(2,10*v):Math.pow(1e3,v)),B&&(a*=8)>=g&&v<8&&(a/=g,v++),n[0]=Number(a.toFixed(v>0?s:0)),n[0]===g&&v<8&&void 0===o.exponent&&(n[0]=1,v++),n[1]=10===d&&1===v?B?"kb":"kB":i[h][B?"bits":"bytes"][v],r&&(n[1]="jedec"===h?n[1].charAt(0):v>0?n[1].replace(/B$/,""):n[1],e.test(n[1])&&(n[0]=Math.floor(n[0]),n[1]=""))),N&&(n[0]=-n[0]),n[1]=M[n[1]]||n[1],!0===c?n[0]=n[0].toLocaleString():c.length>0?n[0]=n[0].toLocaleString(c,f):u.length>0&&(n[0]=n[0].toString().replace(".",u)),"array"===l?n:(m&&(n[1]=p[v]?p[v]:b[h][v]+(B?"bit":"byte")+(1===n[0]?"":"s")),"object"===l?{value:n[0],symbol:n[1]}:n.join(y)))}return t.partial=e=>i=>t(i,e),t}));