forked from KilledByAPixel/JSONCrush
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathJSONCrush.min.js
3 lines (3 loc) · 1.4 KB
/
JSONCrush.min.js
1
2
3
JSONCrush=r=>{r=r.replace(/\u0001/g,""),r=JSONCrushSwap(r);let e=[];for(n=127;--n;)(48<=n&&57>=n||65<=n&&90>=n||97<=n&&122>=n||"-_.!~*'()".includes(String.fromCharCode(n)))&&e.push(String.fromCharCode(n));n=!0;for(let i in e)if(!r.includes(e[i])){n=0;break}if(n)for(n=2;255>n;++n)"\\"==(i=String.fromCharCode(n))||e.includes(i)||e.unshift(i);let f,g,o,t,a,l,u,h=r;for(r=1,n="";;){let s=t=o=f=0;for(i=e.length;!f&&i--;)!~h.indexOf(e[i])&&(f=e[i]);if(!f)break;if(g){let c={};for(l in g){let d=h.indexOf(l);for(c[l]=0;~d;c[l]++)d=h.indexOf(l,d+l.length)}g=c}else for(g=c={},a=1;r&&50>a;a++)for(r=i=0;++i<h.length-a;)if(!c[l=h.substr(d=i,a)]&&~(d=h.indexOf(l,d+a)))for(r=a,c[l]=1;~d;c[l]++)d=h.indexOf(l,d+a);for(let p in g)d=encodeURI(p).replace(/%../g,"i").length,(d=(u=g[p])*d-d-(u+1)*encodeURI(f).replace(/%../g,"i").length)&&(d>s||d==s&&u>t)&&(s=d,t=u,o=p),1>d&&delete g[p];for(let v in c={},g)c[v.split(o).join(f)]=1;if(g=c,!o)break;h=h.split(o).join(f)+f+o,n=f+n}return encodeURIComponent(h+"\u0001"+n)}
JSONUncrush=r=>{let e=r.split("\u0001");for(let n in r=e[0],e=e[1])r=(r=r.split(e[n])).join(r.pop());return JSONCrushSwap(r,0)}
JSONCrushSwap=(r,e=1)=>{n=(r,e)=>r.replace(new RegExp((e[2]?e[2]:"")+e[0]+"|"+((e[3]?e[3]:"")+e[1]),"g"),r=>r===e[0]?e[1]:e[0]);let i=[['"',"'"],["':","!"],[",'","~"],["}",")","\\","\\"],["{","(","\\","\\"]];if(e)for(let f=0;f<i.length;++f)r=n(r,i[f]);else for(f=i.length;f--;)r=n(r,i[f]);return r};