lz-ts
Version:
LZ-based compression algorithm for TypeScript projects (Browser, Node.js)
2 lines (1 loc) • 3.89 kB
JavaScript
var _=Object.defineProperty;var S=Object.getOwnPropertyDescriptor;var j=Object.getOwnPropertyNames;var k=Object.prototype.hasOwnProperty;var g=e=>_(e,"__esModule",{value:!0});var b=(e,s)=>{for(var c in s)_(e,c,{get:s[c],enumerable:!0})},B=(e,s,c,r)=>{if(s&&typeof s=="object"||typeof s=="function")for(let o of j(s))!k.call(e,o)&&(c||o!=="default")&&_(e,o,{get:()=>s[o],enumerable:!(r=S(s,o))||r.enumerable});return e};var R=(e=>(s,c)=>e&&e.get(s)||(c=B(g({}),s,1),e&&e.set(s,c),c))(typeof WeakMap!="undefined"?new WeakMap:0);var m={};b(m,{compress:()=>T,compressToURI:()=>z,decompress:()=>D,decompressFromURI:()=>J});var v=String.fromCharCode,A="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+-$",y={},U=(e,s)=>{if(!y[e]){y[e]={};for(var c=0;c<e.length;c++)y[e][e.charAt(c)]=c}return y[e][s]},z=e=>e==null?"":M(e,6,s=>A.charAt(s)),J=e=>e==null?"":(e=e.replace(/ /g,"+"),O(e.length,32,s=>U(A,e.charAt(s)))),T=e=>e==null?"":JSON.stringify(M(e,15,s=>v(s+32))+" "),D=e=>e==null?"":e==""?null:(e=JSON.parse(e),O(e.length,16384,s=>e.charCodeAt(s)-32)),M=(e,s,c)=>{if(e==null)return"";let r,o,h={},d={},w="",x="",p="",u=2,a=3,f=2,l=[],i=0,t=0,n;for(n=0;n<e.length;n+=1)if(w=e.charAt(n),Object.prototype.hasOwnProperty.call(h,w)||(h[w]=a++,d[w]=!0),x=p+w,Object.prototype.hasOwnProperty.call(h,x))p=x;else{if(Object.prototype.hasOwnProperty.call(d,p)){if(p.charCodeAt(0)<256){for(r=0;r<f;r++)i=i<<1,t==s-1?(t=0,l.push(c(i)),i=0):t++;for(o=p.charCodeAt(0),r=0;r<8;r++)i=i<<1|o&1,t==s-1?(t=0,l.push(c(i)),i=0):t++,o=o>>1}else{for(o=1,r=0;r<f;r++)i=i<<1|o,t==s-1?(t=0,l.push(c(i)),i=0):t++,o=0;for(o=p.charCodeAt(0),r=0;r<16;r++)i=i<<1|o&1,t==s-1?(t=0,l.push(c(i)),i=0):t++,o=o>>1}u--,u==0&&(u=Math.pow(2,f),f++),delete d[p]}else for(o=h[p],r=0;r<f;r++)i=i<<1|o&1,t==s-1?(t=0,l.push(c(i)),i=0):t++,o=o>>1;u--,u==0&&(u=Math.pow(2,f),f++),h[x]=a++,p=String(w)}if(p!==""){if(Object.prototype.hasOwnProperty.call(d,p)){if(p.charCodeAt(0)<256){for(r=0;r<f;r++)i=i<<1,t==s-1?(t=0,l.push(c(i)),i=0):t++;for(o=p.charCodeAt(0),r=0;r<8;r++)i=i<<1|o&1,t==s-1?(t=0,l.push(c(i)),i=0):t++,o=o>>1}else{for(o=1,r=0;r<f;r++)i=i<<1|o,t==s-1?(t=0,l.push(c(i)),i=0):t++,o=0;for(o=p.charCodeAt(0),r=0;r<16;r++)i=i<<1|o&1,t==s-1?(t=0,l.push(c(i)),i=0):t++,o=o>>1}u--,u==0&&(u=Math.pow(2,f),f++),delete d[p]}else for(o=h[p],r=0;r<f;r++)i=i<<1|o&1,t==s-1?(t=0,l.push(c(i)),i=0):t++,o=o>>1;u--,u==0&&(u=Math.pow(2,f),f++)}for(o=2,r=0;r<f;r++)i=i<<1|o&1,t==s-1?(t=0,l.push(c(i)),i=0):t++,o=o>>1;for(;;)if(i=i<<1,t==s-1){l.push(c(i));break}else t++;return l.join("")},O=(e,s,c)=>{let r=[],o=4,h=4,d=3,w="",x=[],p,u,a,f,l,i,t,n={val:c(0),position:s,index:1};for(p=0;p<3;p+=1)r[p]=p;for(a=0,l=Math.pow(2,2),i=1;i!=l;)f=n.val&n.position,n.position>>=1,n.position==0&&(n.position=s,n.val=c(n.index++)),a|=(f>0?1:0)*i,i<<=1;switch(a){case 0:for(a=0,l=Math.pow(2,8),i=1;i!=l;)f=n.val&n.position,n.position>>=1,n.position==0&&(n.position=s,n.val=c(n.index++)),a|=(f>0?1:0)*i,i<<=1;t=v(a);break;case 1:for(a=0,l=Math.pow(2,16),i=1;i!=l;)f=n.val&n.position,n.position>>=1,n.position==0&&(n.position=s,n.val=c(n.index++)),a|=(f>0?1:0)*i,i<<=1;t=v(a);break;case 2:return""}for(r[3]=t,u=t,x.push(t);;){if(n.index>e)return"";for(a=0,l=Math.pow(2,d),i=1;i!=l;)f=n.val&n.position,n.position>>=1,n.position==0&&(n.position=s,n.val=c(n.index++)),a|=(f>0?1:0)*i,i<<=1;switch(t=a){case 0:for(a=0,l=Math.pow(2,8),i=1;i!=l;)f=n.val&n.position,n.position>>=1,n.position==0&&(n.position=s,n.val=c(n.index++)),a|=(f>0?1:0)*i,i<<=1;r[h++]=v(a),t=h-1,o--;break;case 1:for(a=0,l=Math.pow(2,16),i=1;i!=l;)f=n.val&n.position,n.position>>=1,n.position==0&&(n.position=s,n.val=c(n.index++)),a|=(f>0?1:0)*i,i<<=1;r[h++]=v(a),t=h-1,o--;break;case 2:return x.join("")}if(o==0&&(o=Math.pow(2,d),d++),r[t])w=r[t];else if(t===h)w=u+u.charAt(0);else return null;x.push(w),r[h++]=u+w.charAt(0),o--,u=w,o==0&&(o=Math.pow(2,d),d++)}};module.exports=R(m);