-
Notifications
You must be signed in to change notification settings - Fork 163
/
fuzzysort.min.js
2 lines (2 loc) · 6.5 KB
/
fuzzysort.min.js
1
2
// https://github.com/farzher/fuzzysort v3.1.0
((r,e)=>{"function"==typeof define&&define.amd?define([],e):"object"==typeof module&&module.exports?module.exports=e():r.fuzzysort=e()})(this,c=>{var f=r=>{"number"==typeof r?r=""+r:"string"!=typeof r&&(r="");var e=u(r);return x(r,{t:e.i,o:e.v,u:e.l})};class M{get["indexes"](){return this.p.slice(0,this.p.g).sort((r,e)=>r-e)}set["indexes"](r){return this.p=r}["highlight"](r,e){return((r,e="<b>",f="</b>")=>{for(var t="function"==typeof e?e:void 0,i=r.target,a=i.length,o=r.indexes,n="",v=0,u=0,s=!1,l=[],c=0;c<a;++c){var p=i[c];if(o[u]===c){if(++u,s||(s=!0,t?(l.push(n),n=""):n+=e),u===o.length){t?(l.push(t(n+=p,v++)),n="",l.push(i.substr(c+1))):n+=p+f+i.substr(c+1);break}}else s&&(s=!1,t?(l.push(t(n,v++)),n=""):n+=f);n+=p}return t?l:n})(this,r,e)}get["score"](){return e(this.h)}set["score"](r){this.h=A(r)}}class j extends Array{get["score"](){return e(this.h)}set["score"](r){this.h=A(r)}}var o,n,r,t,x=(r,e)=>{var f=new M;return f.target=r,f.obj=e.obj??Q,f.h=e.h??O,f.p=e.p??[],f.t=e.t??"",f.o=e.o??Q,f.k=e.k??Q,f.u=e.u??0,f},e=r=>r===O?0:1<r?r:Math.E**(-2*((1-r)**.04307-1)),A=r=>0===r?O:1<r?r:1-Math.pow(Math.log(r)/-2+1,1/.04307),i=r=>{"number"==typeof r?r=""+r:"string"!=typeof r&&(r=""),r=r.trim();var e=u(r),f=[];if(e.S)for(var t,i=r.split(/\s+/),i=[...new Set(i)],a=0;a<i.length;a++)""!==i[a]&&(t=u(i[a]),f.push({v:t.v,i:i[a].toLowerCase(),S:!1}));return{v:e.v,i:e.i,S:e.S,l:e.l,_:f}},L=r=>{var e;return 999<r.length?f(r):(void 0===(e=a.get(r))&&(e=f(r),a.set(r,e)),e)},D=r=>{var e;return 999<r.length?i(r):(void 0===(e=l.get(r))&&(e=i(r),l.set(r,e)),e)},F=(r,e,f=!1,t=!1)=>{if(!1===f&&r.S)return C(r,e,t);for(var f=r.i,i=r.v,a=i[0],o=e.o,n=i.length,v=o.length,u=0,s=0,l=0;;){if(a===o[s]){if(q[l++]=s,++u===n)break;a=i[u]}if(v<=++s)return Q}var u=0,c=!1,p=0,b=e.k,d=(b===Q&&(b=e.k=N(e.target)),0);if((s=0===q[0]?0:b[q[0]-1])!==v)for(;;)if(v<=s){if(u<=0)break;if(200<++d)break;--u;var w=z[--p],s=b[w]}else if(i[u]===o[s]){if(z[p++]=s,++u===n){c=!0;break}++s}else s=b[s];var g=n<=1?-1:e.t.indexOf(f,q[0]),h=!!~g,y=h&&(0===g||e.k[g-1]===g);if(h&&!y)for(var k=0;k<b.length;k=b[k])if(!(k<=g)){for(var S=0;S<n&&i[S]===e.o[k+S];S++);if(S===n){g=k,y=!0;break}}t=r=>{for(var e=0,f=0,t=1;t<n;++t)r[t]-r[t-1]!=1&&(e-=r[t],++f);if(e-=(12+(r[n-1]-r[0]-(n-1)))*f,0!==r[0]&&(e-=r[0]*r[0]*.2),c){for(var i=1,t=b[0];t<v;t=b[t])++i;24<i&&(e*=10*(i-24))}else e*=1e3;return e-=(v-n)/2,h&&(e/=1+n*n*1),y&&(e/=1+n*n*1),e-=(v-n)/2};if(c)if(y){for(k=0;k<n;++k)q[k]=g+k;_=q,m=t(q)}else _=z,m=t(z);else{if(h)for(var k=0;k<n;++k)q[k]=g+k;var _,m=t(_=q)}e.h=m;for(k=0;k<n;++k)e.p[k]=_[k];e.p.g=n;r=new M;return r.target=e.target,r.h=e.h,r.p=e.p,r},C=(r,e,f)=>{for(var t=new Set,i=0,a=Q,o=0,n=r._,v=n.length,u=0,s=()=>{for(let r=u-1;0<=r;r--)e.k[S[2*r+0]]=S[2*r+1]},l=!1,c=0;c<v;++c){E[c]=O;var p=n[c],a=F(p,e);if(f){if(a===Q)continue;l=!0}else if(a===Q)return s(),Q;if(!(c===v-1)){var b=a.p,d=!0;for(let r=0;r<b.g-1;r++)if(b[r+1]-b[r]!=1){d=!1;break}if(d){var w=b[b.g-1]+1,g=e.k[w-1];for(let r=w-1;0<=r&&g===e.k[r];r--)e.k[r]=w,S[2*u+0]=r,S[2*u+1]=g,u++}}i+=a.h/v,E[c]=a.h/v,a.p[0]<o&&(i-=2*(o-a.p[0]));for(var o=a.p[0],h=0;h<a.p.g;++h)t.add(a.p[h])}if(f&&!l)return Q;s();var y=F(r,e,!0);if(y!==Q&&y.h>i){if(f)for(c=0;c<v;++c)E[c]=y.h/v;return y}(a=f?e:a).h=i;var k,c=0;for(k of t)a.p[c++]=k;return a.p.g=c,a},v=r=>r.replace(/\p{Script=Latin}+/gu,r=>r.normalize("NFD")).replace(/[\u0300-\u036f]/g,""),u=r=>{for(var e=(r=v(r)).length,f=r.toLowerCase(),t=[],i=0,a=!1,o=0;o<e;++o){var n=t[o]=f.charCodeAt(o);32===n?a=!0:i|=1<<(97<=n&&n<=122?n-97:48<=n&&n<=57?26:n<=127?30:31)}return{v:t,l:i,S:a,i:f}},s=r=>{for(var e=r.length,f=[],t=0,i=!1,a=!1,o=0;o<e;++o){var n=r.charCodeAt(o),v=65<=n&&n<=90,n=v||97<=n&&n<=122||48<=n&&n<=57,u=v&&!i||!a||!n,i=v,a=n;u&&(f[t++]=o)}return f},N=r=>{for(var e=(r=v(r)).length,f=s(r),t=[],i=f[0],a=0,o=0;o<e;++o)o<i?t[o]=i:(i=f[++a],t[o]=void 0===i?e:i);return t},a=new Map,l=new Map,q=[],z=[],S=[],B=[],E=[],G=[],H=[],I=(r,e)=>{var f=r[e];if(void 0!==f)return f;if("function"==typeof e)return e(r);for(var t=e,i=(t=Array.isArray(e)?t:e.split(".")).length,a=-1;r&&++a<i;)r=r[t[a]];return r},J=r=>"object"==typeof r&&"number"==typeof r.u,K=1/0,O=-K,P=[],Q=(P.total=0,null),R=f(""),T=(o=[],n=0,t=r=>{for(var e=o[i=0],f=1;f<n;){var t=f+1,i=f;t<n&&o[t].h<o[f].h&&(i=t),o[i-1>>1]=o[i],f=1+(i<<1)}for(var a=i-1>>1;0<i&&e.h<o[a].h;a=(i=a)-1>>1)o[i]=o[a];o[i]=e},(r={}).add=r=>{var e=n;o[n++]=r;for(var f=e-1>>1;0<e&&r.h<o[f].h;f=(e=f)-1>>1)o[e]=o[f];o[e]=r},r.m=r=>{var e;if(0!==n)return e=o[0],o[0]=o[--n],t(),e},r.M=r=>{if(0!==n)return o[0]},r.C=r=>{o[0]=r,t()},r);return{single:(r,e)=>{var f;return!r||!e||(r=D(r),J(e)||(e=L(e)),((f=r.l)&e.u)!==f)?Q:F(r,e)},go:(r,e,f)=>{if(!r)return f?.all?((r,e)=>{var f=[],t=(f.total=r.length,e?.limit||K);if(e?.key)for(var i=0;i<r.length;i++){var a=r[i];var o=I(a,e.key);if(o==Q)continue;if(!J(o))o=L(o);var n=x(o.target,{h:o.h,obj:a});f.push(n);if(f.length>=t)return f}else if(e?.keys)for(var i=0;i<r.length;i++){var a=r[i];var v=new j(e.keys.length);for(var u=e.keys.length-1;u>=0;--u){var o=I(a,e.keys[u]);if(!o){v[u]=R;continue}if(!J(o))o=L(o);o.h=O;o.p.g=0;v[u]=o}v.obj=a;v.h=O;f.push(v);if(f.length>=t)return f}else for(var i=0;i<r.length;i++){var o=r[i];if(o==Q)continue;if(!J(o))o=L(o);o.h=O;o.p.g=0;f.push(o);if(f.length>=t)return f}return f})(e,f):P;var t=D(r),i=t.l,a=t.S,o=A(f?.threshold||0),n=f?.limit||K,v=0,u=0,s=e.length;function l(r){v<n?(T.add(r),++v):(++u,r.h>T.M().h&&T.C(r))}if(f?.key)for(var c=f.key,p=0;p<s;++p){var b=e[p];!(m=I(b,c))||(i&(m=J(m)?m:L(m)).u)!==i||(M=F(t,m))===Q||M.h<o||(M.obj=b,l(M))}else if(f?.keys){var d=f.keys,w=d.length;r:for(p=0;p<s;++p){for(var b=e[p],g=0,h=0;h<w;++h){c=d[h];(m=I(b,c))?(J(m)||(m=L(m)),g|=(G[h]=m).u):G[h]=R}if((i&g)===i){if(a)for(let r=0;r<t._.length;r++)B[r]=O;for(h=0;h<w;++h)if((m=G[h])===R)H[h]=R;else if(H[h]=F(t,m,!1,a),H[h]===Q)H[h]=R;else if(a)for(let r=0;r<t._.length;r++)-1e3<E[r]&&B[r]>O&&(_=(B[r]+E[r])/4)>B[r]&&(B[r]=_),E[r]>B[r]&&(B[r]=E[r]);if(a){for(let r=0;r<t._.length;r++)if(B[r]===O)continue r}else{var y=!1;for(let r=0;r<w;r++)if(H[r].h!==O){y=!0;break}if(!y)continue}var k=new j(w);for(let r=0;r<w;r++)k[r]=H[r];if(a){var S=0;for(let r=0;r<t._.length;r++)S+=B[r]}else{var _,S=O;for(let r=0;r<w;r++)(S=-1e3<(M=k[r]).h&&O<S&&S<(_=(S+M.h)/4)?_:S)<M.h&&(S=M.h)}if(k.obj=b,k.h=S,f?.scoreFn){if(!(S=f.scoreFn(k)))continue;S=A(S),k.h=S}S<o||l(k)}}}else for(var m,M,p=0;p<s;++p)!(m=e[p])||(i&(m=J(m)?m:L(m)).u)!==i||(M=F(t,m))===Q||M.h<o||l(M);if(0===v)return P;for(var C=new Array(v),p=v-1;0<=p;--p)C[p]=T.m();return C.total=v+u,C},prepare:f,cleanup:()=>{a.clear(),l.clear()}}});