1
0
mirror of https://github.com/nextapps-de/flexsearch.git synced 2025-10-03 00:21:51 +02:00
Files
flexsearch/dist/flexsearch.node.js
2019-02-08 20:11:53 +01:00

34 lines
13 KiB
JavaScript

/*
FlexSearch v0.5.0
Copyright 2019 Nextapps GmbH
Author: Thomas Wilkerling
Released under the Apache 2.0 Licence
https://github.com/nextapps-de/flexsearch
*/
'use strict';(function(h,w,C){let f;(f=C.define)&&f.amd?f([],function(){return w}):(f=C.modules)?f[h.toLowerCase()]=w:"object"===typeof exports?module.exports=w:C[h]=w})("FlexSearch",function(){function h(a,b){const c=b?b.id:a&&a.id;this.id=c||0===c?c:R++;this.init(a,b);C(this,"index",function(){return this.b});C(this,"length",function(){return Object.keys(this.b).length})}function w(a,b){a=a.concat.apply([],a);b&&(D(b)||(x=b.split(":"),1<x.length?b=S:(x=x[0],b=T)),a.sort(b));return a}function C(a,
b,c){Object.defineProperty(a,b,{get:c})}function f(a){return new RegExp(a,"g")}function y(a,b){for(let c=0;c<b.length;c+=2)a=a.replace(b[c],b[c+1]);return a}function H(a,b,c,d,e,g,l){if(b[c])return b[c];e=e?(9-(l||6))*g+(l||6)*e:g;b[c]=e;e>=l&&(a=a[9-(e+.5>>0)],a=a[c]||(a[c]=[]),a[a.length]=d);return e}function K(a,b){if(a){const c=Object.keys(a);for(let d=0,e=c.length;d<e;d++){const e=c[d],l=a[e];if(l)for(let c=0,d=l.length;c<d;c++)if(l[c]===b){1===d?delete a[e]:l.splice(c,1);break}else z(l[c])&&
K(l[c],b)}}}function L(a){let b="",c="";var d="";for(let e=0;e<a.length;e++){const g=a[e];if(g!==c)if(e&&"h"===g){if(d="a"===d||"e"===d||"i"===d||"o"===d||"u"===d||"y"===d,("a"===c||"e"===c||"i"===c||"o"===c||"u"===c||"y"===c)&&d||" "===c)b+=g}else b+=g;d=e===a.length-1?"":a[e+1];c=g}return b}function U(a,b){a=a.length-b.length;return 0>a?1:a?-1:0}function V(a,b){a=a.length-b.length;return 0>a?-1:a?1:0}function T(a,b){a=a[x];b=b[x];return a<b?-1:a>b?1:0}function S(a,b){const c=x.length;for(let d=
0;d<c;d++)a=a[x[d]],b=b[x[d]];return a<b?-1:a>b?1:0}function W(a,b,c,d){let e=[],g;const l=a.length;if(1<l){a.sort(V);const f=u();let n=a[0],m=n.length,q=0;for(;q<m;)f["@"+n[q++]]=1;let p,h=0,r=0;for(;++r<l;){let E=!1;const F=r===l-1;g=[];n=a[r];m=n.length;for(q=0;q<m;){p=n[q++];var k="@"+p;if(f[k]){const a=f[k];if(a===r){if(F){if(e[h++]=c?c[k]:p,b&&h===b)return e}else f[k]=r+1;E=!0}else d&&(k=g[a]||(g[a]=[]),k[k.length]=p)}}if(!E&&!d)break}if(d&&(h=e.length,(r=g.length)&&(!b||h<b)))for(;r--;)if(p=
g[r])for(q=0,m=p.length;q<m;q++)if(e[h++]=c?c["@"+p[q]]:p[q],b&&h===b)return e}else if(l)if(c)for(a=a[0],d=a.length,b&&b<d&&(d=b),e=Array(d),b=0;b<d;b++)e[b]=c["@"+a[b]];else e=a[0],b&&e.length>b&&(e=e.slice(0,b));return e}function I(a){return"string"===typeof a}function D(a){return"function"===typeof a}function z(a){return"object"===typeof a}function B(a){return"undefined"===typeof a}function M(a){const b=Array(a);for(let c=0;c<a;c++)b[c]=u();return b}function u(){return Object.create(null)}const v=
{encode:"icase",c:"forward",s:!1,cache:!1,async:!1,B:!1,v:!1,threshold:0,depth:0,a:!1},N={memory:{encode:"extra",c:"strict",threshold:7},speed:{encode:"icase",c:"strict",threshold:7,depth:2},match:{encode:"extra",c:"full"},score:{encode:"extra",c:"strict",threshold:5,depth:4},balance:{encode:"balance",c:"strict",threshold:6,depth:3},fastest:{encode:"icase",c:"strict",threshold:9,depth:1}},J=[];let R=0;const O=f("\\W+"),P={},Q={};h.create=function(a){return new h(a)};h.registerMatcher=function(a){for(const b in a)a.hasOwnProperty(b)&&
J.push(f(b),a[b]);return this};h.registerEncoder=function(a,b){G[a]=b.bind(G);return this};h.registerLanguage=function(a,b){P[a]=b.filter;Q[a]=b.stemmer;return this};h.encode=function(a,b){return G[a](b)};h.prototype.init=function(a,b){this.l=[];if(b){var c=b.preset;a=b}else a||(a=v),c=a.preset;b={};I(a)?(b=N[a],a={}):c&&(b=N[c]);this.c=a.tokenize||b.c||this.c||v.c;this.v=a.rtl||this.v||v.v;this.async="undefined"===typeof Promise||B(c=a.async)?this.async||v.async:c;this.threshold=B(c=a.threshold)?
b.threshold||this.threshold||v.threshold:c;this.depth=B(c=a.depth)?b.depth||this.depth||v.depth:c;this.s=B(c=a.suggest)?this.s||v.s:c;this.m=(c=B(c=a.encode)?b.encode||v.encode:c)&&G[c]&&G[c].bind(G)||(D(c)?c:this.m||!1);(c=a.matcher)&&this.addMatcher(c);if(c=a.filter){c=P[c]||c;b=this.m;var d=u();if(c)for(var e=0;e<c.length;e++){var g=b?b(c[e]):c[e];d[g]=String.fromCharCode(65E3-c.length+e)}this.filter=c=d}if(c=a.stemmer){var l;b=Q[c]||c;d=this.m;e=[];if(b)for(l in b)b.hasOwnProperty(l)&&(g=d?d(l):
l,e.push(f("(?=.{"+(g.length+3)+",})"+g+"$"),d?d(b[l]):b[l]));this.stemmer=l=e}this.a=g=(c=a.doc)?c:this.a||v.a;this.g=M(10-(this.threshold||0));this.h=u();this.b=u();if(g){this.f=u();a.doc=null;l=g.index=[];b=g.ref={};d=g.field;e=g.tag;g.id=g.id.split(":");if(e){this.w=u();e.constructor===Array||(g.tag=e=[e]);for(let a=0;a<e.length;a++)this.w[e[a]]=u(),e[a]=e[a].split(":")}if(d)for(d.constructor===Array||(g.field=d=[d]),g=0;g<d.length;g++)b[d[g]]=g,d[g]=d[g].split(":"),l[g]=new h(a),l[g].f=this.f,
e&&(l[g].w=this.w)}this.u=!0;this.j=(this.cache=c=B(c=a.cache)?this.cache||v.cache:c)?new X(c):!1;return this};h.prototype.encode=function(a){a&&J.length&&(a=y(a,J));a&&this.l.length&&(a=y(a,this.l));a&&this.m&&(a=this.m(a));a&&this.stemmer&&(a=y(a,this.stemmer));return a};h.prototype.addMatcher=function(a){const b=this.l;for(const c in a)a.hasOwnProperty(c)&&b.push(f(c),a[c]);return this};h.prototype.add=function(a,b,c,d,e){if(this.a&&z(a))return this.o("add",a,b);if(b&&I(b)&&(a||0===a)){var g="@"+
a;if(this.b[g]&&!d)return this.update(a,b);if(!e){if(this.async&&"function"!==typeof importScripts){let e=this;g=new Promise(function(c){setTimeout(function(){e.add(a,b,null,d,!0);e=null;c()})});if(c)g.then(c);else return g;return this}if(c)return this.add(a,b,null,d,!0),c(),this}b=this.encode(b);if(!b.length)return this;c=this.c;e=D(c)?c(b):b.split(O);const h=u();h._ctx=u();const p=this.threshold,E=this.depth,r=this.g,t=e.length,F=this.v;for(let b=0;b<t;b++){var l=e[b];if(l){var k=l.length,f=(F?
b+1:t-b)/t,n="";switch(c){case "reverse":case "both":for(var m=k;--m;)n=l[m]+n,H(r,h,n,a,F?1:(k-m)/k,f,p);n="";case "forward":for(m=0;m<k;m++)n+=l[m],H(r,h,n,a,F?(m+1)/k:1,f,p);break;case "full":for(m=0;m<k;m++){const b=(F?m+1:k-m)/k;for(let c=k;c>m;c--)n=l.substring(m,c),H(r,h,n,a,b,f,p)}break;default:if(k=H(r,h,l,a,1,f,p),E&&1<t&&k>=p)for(k=h._ctx[l]||(h._ctx[l]=u()),l=this.h[l]||(this.h[l]=M(10-(p||0))),f=b-E,n=b+E+1,0>f&&(f=0),n>t&&(n=t);f<n;f++)f!==b&&H(l,k,e[f],a,0,10-(f<b?b-f:f-b),p)}}}this.b[g]=
1;this.u=!1}return this};h.prototype.o=function(a,b,c){if(b.constructor===Array)for(let d=0,e=b.length;d<e;d++){if(d===e-1)return this.o(a,b[d],c);this.o(a,b[d])}else{const l=this.a.index;var d=this.a.tag,e=this.a.id;let f;for(var g=0;g<e.length;g++)f=(f||b)[e[g]];if(d)for(e=0;e<d.length;e++);if("remove"===a){delete this.f["@"+f];for(let a=0,b=l.length;a<b;a++){if(a===b-1)return l[a].remove(f,c);l[a].remove(f)}}e=this.a.field;for(let k=0,h=e.length;k<h;k++){d=e[k];let m;for(g=0;g<d.length;g++)m=(m||
b)[d[g]];this.f["@"+f]=b;d=l[k];g="add"===a?d.add:d.update;if(k===h-1)return g.call(d,f,m,c);g.call(d,f,m)}}};h.prototype.update=function(a,b,c){if(this.a&&z(a))return this.o("update",a,b);this.b["@"+a]&&I(b)&&(this.remove(a),this.add(a,b,c,!0));return this};h.prototype.remove=function(a,b,c){if(this.a&&z(a))return this.o("remove",a,b);var d="@"+a;if(this.b[d]){if(!c){if(this.async&&"function"!==typeof importScripts){let c=this;d=new Promise(function(b){setTimeout(function(){c.remove(a,null,!0);c=
null;b()})});if(b)d.then(b);else return d;return this}if(b)return this.remove(a,null,!0),b(),this}for(b=0;b<10-(this.threshold||0);b++)K(this.g[b],a);this.depth&&K(this.h,a);delete this.b[d];this.u=!1}return this};let x;h.prototype.search=function(a,b,c,d){if(z(b)){if(b.constructor===Array)for(var e=0;e<b.length;e++)b[e].query=a;else b.query=a;a=b;b=0}let g=a;let f,k=[];if(z(a)&&a.constructor!==Array){(c=a.callback||b)&&(g.callback=null);var h=a.boost;var n=a.where;f=a.sort;b=a.limit;var m=a.threshold;
a=a.query}if(this.a){a=this.a.ref;m=this.a.index;if(n=g.field)g.field=null;else if(g.constructor===Array){var q=g;n=[];for(var p=0;p<g.length;p++)n[p]=g[p].field}else n=Object.keys(a);if(z(n)){n.constructor===Array||(n=[n]);p=n.length;for(var A=0;A<p;A++)q&&(g=q[A]),k[A]=m[a[n[A]]].search(g);return c?c(w(k,f)):this.async?new Promise(function(a){Promise.all(k).then(function(b){a(w(b,f))})}):w(k,f)}return m[a[n]].search(g,c)}m||(m=this.threshold||0);D(b)?(c=b,b=1E3):b||0===b||(b=1E3);if(!d){if(this.async&&
"function"!==typeof importScripts){let d=this;a=new Promise(function(a){setTimeout(function(){a(d.search(g,b,null,!0));d=null})});if(c)a.then(c);else return a;return this}if(c)return c(this.search(g,b,null,!0)),this}if(!a||!I(a))return k;g=a;if(this.cache)if(this.u){if(c=this.j.get(a))return c}else this.j.clear(),this.u=!0;g=this.encode(g);if(!g.length)return k;c=this.c;c=D(c)?c(g):g.split(O);q=c.length;d=!0;e=[];const r=u();if(1<q)if(this.depth){p=!0;var t=c[0];r[t]=1}else c.sort(U);if(!p||(A=this.h)[t]){let a=
0;h&&(m=(m||1)/h,0>h&&(a=m));for(h=p?1:0;h<q;h++){const b=c[h];if(b){if(!r[b]){const c=[];let g=!1,f=0;if(t=p?A[t]:this.g){let d;for(let e=a;e<10-m;e++)if(d=t[e][b])c[f++]=d,g=!0}if(g)e[e.length]=1<f?c.concat.apply([],c):c[0];else if(!this.s){d=!1;break}r[b]=1}t=b}}}else d=!1;d&&(k=W(e,b,this.f,this.s));n&&(k=this.where(n,null,b,k));f&&(k=w([k],f));this.cache&&this.j.set(a,k);return k};h.prototype.find=function(a,b){return this.where(a,b,1)[0]||null};h.prototype.where=function(a,b,c,d){const e=d||
this.f,g=[];let f=0;var k;let h,n;if(I(a)){if("id"===a)return[e["@"+b]];var m=1;n=[a.split(":")];h=!0}else{if(D(a)){b=d||Object.keys(e);c=b.length;for(k=0;k<c;k++)m=d?d[k]:e[b[k]],a(m)&&(g[f++]=m);return g}c||(c=b);k=Object.keys(a);m=k.length;h=!1;if(1===m&&"id"===k[0])return[e["@"+a.id]];n=Array(m);for(var q=0;q<m;q++)n[q]=k[q].split(":")}q=d||Object.keys(e);const p=q.length;for(let l=0;l<p;l++){const p=d?d[l]:e[q[l]];let t=!0;for(let c=0;c<m;c++){h||(b=a[k[c]]);const d=n[c],e=d.length;let f=p;if(1<
e)for(let a=0;a<e;a++)f=f[d[a]];else f=f[d[0]];if(f!==b){t=!1;break}}if(t&&(g[f++]=p,c&&f===c))break}return g};h.prototype.info=function(){let a;let b=0,c=0,d=0;for(var e=0;e<10-(this.threshold||0);e++){a=Object.keys(this.g[e]);for(let g=0;g<a.length;g++){var f=this.g[e][a[g]].length;b+=1*f+2*a[g].length+4;c+=f;d+=2*a[g].length}}a=Object.keys(this.b);f=a.length;for(e=0;e<f;e++)b+=2*a[e].length+2;return{id:this.id,memory:b,items:f,sequences:c,chars:d,cache:this.cache&&this.cache.i?this.cache.i.length:
!1,matcher:J.length+(this.l?this.l.length:0),worker:this.B,threshold:this.threshold,depth:this.depth,contextual:this.depth&&"strict"===this.c}};h.prototype.clear=function(){return this.destroy().init()};h.prototype.destroy=function(){this.cache&&(this.j.clear(),this.j=null);this.g=this.h=this.b=null;if(this.a){const a=this.f.index;for(let b=0;b<a.length;b++)a.destroy();this.f=null}return this};h.prototype.export=function(){if(this.a){const a=this.a.index,b=Array(a.length+1);let c=0;for(;c<a.length;c++)b[c]=
[a[c].g,a[c].h,a[c].b];b[c]=this.f;return JSON.stringify(b)}return JSON.stringify([this.g,this.h,this.b])};h.prototype.import=function(a){a=JSON.parse(a);if(this.a){const b=this.a.index,c=b.length;for(let d=0;d<c;d++){const e=b[d];e.g=a[d][0];e.h=a[d][1];e.b=a[d][2];e.f=a[c]}this.f=a[c]}else this.g=a[0],this.h=a[1],this.b=a[2],this.f=a[3]};const G={icase:function(a){return a.toLowerCase()},simple:function(){const a=[f("[\u00e0\u00e1\u00e2\u00e3\u00e4\u00e5]"),"a",f("[\u00e8\u00e9\u00ea\u00eb]"),"e",
f("[\u00ec\u00ed\u00ee\u00ef]"),"i",f("[\u00f2\u00f3\u00f4\u00f5\u00f6\u0151]"),"o",f("[\u00f9\u00fa\u00fb\u00fc\u0171]"),"u",f("[\u00fd\u0177\u00ff]"),"y",f("\u00f1"),"n",f("\u00e7"),"c",f("\u00df"),"s",f(" & ")," and ",f("[-/]")," ",f("[^a-z0-9 ]"),"",f("\\s+")," "];return function(b){b=y(b.toLowerCase(),a);return" "===b?"":b}}(),advanced:function(){const a=[f("ae"),"a",f("ai"),"ei",f("ay"),"ei",f("ey"),"ei",f("oe"),"o",f("ue"),"u",f("ie"),"i",f("sz"),"s",f("zs"),"s",f("sh"),"s",f("ck"),"k",f("cc"),
"k",f("dt"),"t",f("ph"),"f",f("pf"),"f",f("ou"),"o",f("uo"),"u"];return function(b,c){if(!b)return b;b=this.simple(b);2<b.length&&(b=y(b,a));c||1<b.length&&(b=L(b));return b}}(),extra:function(){const a=[f("p"),"b",f("z"),"s",f("[cgq]"),"k",f("n"),"m",f("d"),"t",f("[vw]"),"f",f("[aeiouy]"),""];return function(b){if(!b)return b;b=this.advanced(b,!0);if(1<b.length){b=b.split(" ");for(let c=0;c<b.length;c++){const d=b[c];1<d.length&&(b[c]=d[0]+y(d.substring(1),a))}b=b.join(" ");b=L(b)}return b}}(),balance:function(){const a=
[f("[-/]")," ",f("[^a-z0-9 ]"),"",f("\\s+")," "];return function(b){return L(y(b.toLowerCase(),a))}}()},X=function(){function a(a){this.clear();this.A=!0!==a&&a}a.prototype.clear=function(){this.cache=u();this.count=u();this.index=u();this.i=[]};a.prototype.set=function(a,c){if(this.A&&B(this.cache[a])){let b=this.i.length;if(b===this.A){b--;const a=this.i[b];delete this.cache[a];delete this.count[a];delete this.index[a]}this.index[a]=b;this.i[b]=a;this.count[a]=-1;this.cache[a]=c;this.get(a)}else this.cache[a]=
c};a.prototype.get=function(a){const b=this.cache[a];if(this.A&&b){var d=++this.count[a];const b=this.index;let c=b[a];if(0<c){const f=this.i;for(var e=c;this.count[f[--c]]<=d&&-1!==c;);c++;if(c!==e){for(d=e;d>c;d--)e=f[d-1],f[d]=e,b[e]=d;f[c]=a;b[a]=c}}}return b};return a}();return h}(!1),this);