@visulima/humanizer
Version:
Humanizer is a library for humanizing data in a human-readable form.
2 lines (1 loc) • 3.31 kB
JavaScript
var M=Object.defineProperty;var B=(e,t)=>M(e,"name",{value:t,configurable:!0});var d=Object.defineProperty,n=B((e,t)=>d(e,"name",{value:t,configurable:!0}),"i");const E={iec:[{long:"Bytes",short:"B"},{long:"Kibibytes",short:"KiB"},{long:"Mebibytes",short:"MiB"},{long:"Gibibytes",short:"GiB"},{long:"Tebibytes",short:"TiB"},{long:"Pebibytes",short:"PiB"},{long:"Exbibytes",short:"EiB"},{long:"Zebibytes",short:"ZiB"},{long:"Yobibytes",short:"YiB"}],iec_octet:[{long:"Octets",short:"o"},{long:"Kibioctets",short:"Kio"},{long:"Mebioctets",short:"Mio"},{long:"Gibioctets",short:"Gio"},{long:"Tebioctets",short:"Tio"},{long:"Pebioctets",short:"Pio"},{long:"Exbioctets",short:"Eio"},{long:"Zebioctets",short:"Zio"},{long:"Yobioctets",short:"Yio"}],metric:[{long:"Bytes",short:"Bytes"},{long:"Kilobytes",short:"KB"},{long:"Megabytes",short:"MB"},{long:"Gigabytes",short:"GB"},{long:"Terabytes",short:"TB"},{long:"Petabytes",short:"PB"},{long:"Exabytes",short:"EB"},{long:"Zettabytes",short:"ZB"},{long:"Yottabytes",short:"YB"}],metric_octet:[{long:"Octets",short:"o"},{long:"Kilo-octets",short:"ko"},{long:"Mega-octets",short:"Mo"},{long:"Giga-octets",short:"Go"},{long:"Tera-octets",short:"To"},{long:"Peta-octets",short:"Po"},{long:"Exa-octets",short:"Eo"},{long:"Zetta-octets",short:"Zo"},{long:"Yotta-octets",short:"Yo"}]},w=n((e,t)=>{const r=new Intl.NumberFormat(t).format(11111).replaceAll(new RegExp("\\p{Number}","gu"),""),o=new Intl.NumberFormat(t).format(1.1).replaceAll(new RegExp("\\p{Number}","gu"),"");return Number.parseFloat(e.replaceAll(new RegExp(`\\${r}`,"g"),"").replace(new RegExp(`\\${o}`),"."))},"parseLocalizedNumber"),f=n(e=>{if(e===2)return 1024;if(e===10)return 1e3;throw new TypeError("Unsupported base.")},"fromBase"),N=n((e,t)=>{const r={base:2,locale:"en-US",units:"metric",...t};if(typeof e!="string"||e.length===0)throw new TypeError("Value is not a string or is empty.");if(e.length>100)throw new TypeError("Value exceeds the maximum length of 100 characters.");const o=/^(?<value>-?(?:\d+(([.,])\d+)*)?[.,]?\d+) *(?<type>bytes?|b|kb|kib|mb|mib|gb|gib|tb|tib|pb|pib|eb|eib|zb|zib|yb|yib|(kilo|kibi|mega|mebi|giga|gibi|tera|tebi|peta|pebi|exa|exbi|zetta|zebi|yotta|yobi)?bytes)?$/i.exec(e)?.groups;if(!o)return Number.NaN;const a=w(o.value,r.locale),i=(o.type??"Bytes").toUpperCase().replace(/^KIBI/,"KILO").replace(/^MIBI/,"MEGA").replace(/^GIBI/,"GIGA").replace(/^TEBI/,"TERA").replace(/^PEBI/,"PETA").replace(/^EXBI/,"EXA").replace(/^ZEBI/,"ZETTA").replace(/^YIBI/,"YOTTA").replace(/^(.)IB$/,"$1B"),l=E[r.units].findIndex(g=>g.short[0].toUpperCase()===i[0]),b=f(r.base);return a*b**l},"parseBytes"),P=n((e,t)=>{if(typeof e!="number"||!Number.isFinite(e))throw new TypeError("Bytesize is not a number.");const{base:r,decimals:o,locale:a,long:i,unit:l,units:b,...g}={base:2,decimals:0,locale:"en-US",long:!1,units:"metric",...t},p=f(r),I=Math.abs(e),m=t?.space??!0?" ":"",s=E[b],c=s.findIndex(h=>h.short===l);if(e===0){const h=Math.min(0,Math.max(c,s.length-1));return"0"+m+s[h][i?"long":"short"]}const y=c===-1?Math.min(Math.floor(Math.log(I)/Math.log(p)),s.length-1):c,T=s[y][i?"long":"short"],x=e/p**y,u=o<0?void 0:o;return new Intl.NumberFormat(a,{maximumFractionDigits:u,minimumFractionDigits:u,...g}).format(x)+m+T},"formatBytes");export{P as formatBytes,N as parseBytes};