UNPKG

bit-crush

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