This repository has been archived by the owner on Jun 25, 2024. It is now read-only.
forked from KilledByAPixel/JSONCrush
-
Notifications
You must be signed in to change notification settings - Fork 1
/
JSONCrush.min.js
1 lines (1 loc) · 1.83 KB
/
JSONCrush.min.js
1
"use strict";const JSONCrush=function(e){let t=[];const n="-_.!~*'()";for(let e=127;--e;)(e>=48&&e<=57||e>=65&&e<=90||e>=97&&e<=122||n.includes(String.fromCharCode(e)))&&t.push(String.fromCharCode(e));for(let e=32;e<255;++e){let n=String.fromCharCode(e);"\\"==n||t.includes(n)||t.unshift(n)}const r=((e,t)=>{let n=t.length,r="";const l=e=>encodeURI(encodeURIComponent(e)).replace(/%../g,"i").length,o=e=>{let t=e.charCodeAt(0),n=e.charCodeAt(e.length-1);return t>=56320&&t<=57343||n>=55296&&n<=56319};let i={};for(let t=2;t<50;t++)for(let n=0;n<e.length-t;++n){let r=e.substr(n,t);if(i[r])continue;if(o(r))continue;let l=1;for(let o=e.indexOf(r,n+t);o>=0;++l)o=e.indexOf(r,o+t);l>1&&(i[r]=l)}for(;;){for(;n--&&e.includes(t[n]););if(n<0)break;let o,s=t[n],u=0,f=l(s);for(let e in i){let t=i[e],n=(t-1)*l(e)-(t+1)*f;r.length||(n-=l("")),n<=0?delete i[e]:n>u&&(o=e,u=n)}if(!o)break;e=e.split(o).join(s)+s+o,r=s+r;let h={};for(let t in i){let n=t.split(o).join(s),r=0;for(let t=e.indexOf(n);t>=0;++r)t=e.indexOf(n,t+n.length);r>1&&(h[n]=r)}i=h}return{a:e,b:r}})(e=JSONCrushSwap(e=e.replace(new RegExp("","g"),"")),t);let l=r.a;return r.b.length&&(l+=""+r.b),l+="_",encodeURIComponent(l)},JSONUncrush=function(e){const t=(e=e.substring(0,e.length-1)).split("");let n=t[0];if(t.length>1){let e=t[1];for(let t of e){let e=n.split(t);n=e.join(e.pop())}}return JSONCrushSwap(n,0)};function JSONCrushSwap(e,t=1){const n=[['"',"'"],["':","!"],[",'","~"],["}",")","\\","\\"],["{","(","\\","\\"]],r=(e,t)=>{let n=new RegExp(`${(t[2]?t[2]:"")+t[0]}|${(t[3]?t[3]:"")+t[1]}`,"g");return e.replace(n,e=>e===t[0]?t[1]:t[0])};if(t)for(let t=0;t<n.length;++t)e=r(e,n[t]);else for(let t=n.length;t--;)e=r(e,n[t]);return e}"undefined"!=typeof module&&module.exports?module.exports={JSONCrush:JSONCrush,JSONUncrush:JSONUncrush}:(window.JSONCrush=JSONCrush,window.JSONUncrush=JSONUncrush);