bit-crush
Version:
Javascript dictionary encoder
2 lines (1 loc) • 1.95 kB
JavaScript
class t{constructor(t){const n=[..."abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ",..."0123456789",..."+-*/()[]{}^%$!@.,_<>?:;",..."αβγδεζηθικλμνξοπρστυφχψωΑΒΓΔΕΖΗΘΙΚΛΜΝΞΟΠΡΣΤΥΦΧΨΩ",..."абвгдеёжзийклмнопрстуфхцчшщыэюяАБВГДЕЁЖЗИЙКЛМНОПРСТУФХЦЧШЩъЫьЭЮЯ"];this.availableChars=n.filter(n=>!t.includes(n)),this.index=0}getNextChar(){const t=this.availableChars[this.index]||String.fromCharCode(192+this.index);return this.index++,t}}function n(t,n){var e;return`O=${n=(e=n).includes("\n")?"`"+e.replace(/`/g,"\\`")+"`":e.split('"').length>=e.split("'").length?`'${e.replace(/'/g,"\\'")}'`:`"${e.replace(/"/g,'\\"')}"`};for(o of'${t.map(t=>t.to).reverse().join("")}')with(O.split(o))O=join(pop());eval(O)`}var e;function r(t,n){let r=[],o=0,s=!1;for(let i=2;!s;i++){let a=!1;for(let s=0;s<t.length-i;s++){const c=t.substring(s,s+i),h=(l=c,t.split(l).length-1);if(h>1){a=!0,r=r.filter(t=>-1===c.indexOf(t.word)||t.wordOccurences>h);const t=h*i-(h+i+2);t>0&&t>=o*(n===e.NONE?1:.8)&&r.push({word:c,wordOccurences:h,savings:t}),o=Math.max(o,t)}}a||(s=!0)}var l;return r.sort((t,n)=>n.savings-t.savings)}function o(o){const s=[e.NONE,e.MAX,e.MIN].map(s=>function(o,s){const l=new t(o);let i=[],a=o,c=null,h=!1;for(;!h;){const t=l.getNextChar(),o=r(a,s);if(0===o.length)break;const g=o[0].savings,f=o.filter(t=>t.savings>=.8*g);let u;if(s===e.NONE&&(u=0),s===e.MAX){let t=0;f.forEach((n,e)=>{n.word.length>t&&(u=e,t=n.word.length)})}if(s===e.MIN){let t=1/0;f.forEach((n,e)=>{n.word.length<t&&(u=e,t=n.word.length)})}const d=o[u].word,N=a.split(d).join(t)+t+d,p=[...i];p.push({from:d,to:t});const v=n(p,N);null===c||c.length>v.length?(c=v,a=N,i=p):h=!0}const g=n(i,a);return o.length<g.length?o:g}(o,s));return s.sort((t,n)=>t.length-n.length)[0]}!function(t){t[t.NONE=0]="NONE",t[t.MAX=1]="MAX",t[t.MIN=2]="MIN"}(e||(e={}));export default o;