(function(c,l){typeof exports=="object"&&typeof module<"u"?l(exports):typeof define=="function"&&define.amd?define(["exports"],l):(c=typeof globalThis<"u"?globalThis:c||self,l(c.huffmanjs={}))})(this,function(c){"use strict";function l(e){const t={};let o="";for(const n of e)o+=n.charCodeAt(0).toString(2).padStart(8,"0");for(let n=0;no.charCodeAt(0));return t[0]>0&&(t[0]*=255,t[0]++),t.reduce((o,n)=>o+n,0)}function h(e){let t="";for(const r in e){const i=e[r],f=16-i.length%16,u=i.padStart(i.length+f,"0"),d=parseInt(r).toString(2).padStart(16,"0");t+=d+u}const o=a(t)[0],n=a(o.length.toString(2).padStart(16,"0"))[0];return[o,n]}function b(e){const t={};o(e[0]);function o(n,r="1"){n!==null&&(n.char!==null&&(t[n.char]=r),o(n.left,r+"1"),o(n.right,r+"0"))}return t}function y(e){const t={};for(let o=0;oH.charCodeAt(0));const t=y(e),o=B(t),n=b(o),r=C(e,n),i=a(r),[f,u,d]=S(i,n);return[f,n,u,d]}function B(e){const t=Object.keys(e).map(o=>new p(o,e[o])).sort(g);for(;t.length>1;){const o=t.shift(),n=t.shift(),r=new p(null,o.freq+n.freq);r.left=o,r.right=n,t.push(r),t.sort(g)}return t}function a(e){const t=[];let o=0;for(let n=0;n0&&(t=t.slice(0,-e.padEnd));let o=[],n="";for(const r of t){n+=r;const i=e.bitmap[n];i!==void 0&&(o.push(String.fromCharCode(i)),n="")}return o.join("")}function g(e,t){return e.freq-t.freq}class p{constructor(t,o){this.char=t,this.freq=o,this.left=null,this.right=null}}c.compress=q,c.decompress=k,c.serializeHuffmanCode=h,c.unserializeHuffmanCode=l,Object.defineProperty(c,Symbol.toStringTag,{value:"Module"})});