diff --git a/README.md b/README.md index c41f629..19f73c6 100644 --- a/README.md +++ b/README.md @@ -1,57 +1,388 @@ - -FlexSearch v0.8-Preview: https://github.com/nextapps-de/flexsearch/tree/v0.8-preview +# FlexSearch v0.8 (Preview) -Let's discuss the upcoming FlexSearch v0.8 here: https://github.com/nextapps-de/flexsearch/discussions/415 -
-Store | +Add | +Search 1 | +Search N | +Replace | +Remove | +Not Found | +Scaling | +
---|---|---|---|---|---|---|---|
+ | terms per sec | +terms per sec | +terms per sec | +terms per sec | +terms per sec | +terms per sec | ++ |
IndexedDB | +123,298 | +83,823 | +62,370 | +57,410 | +171,053 | +425,744 | +No | +
Redis | +1,566,091 | +201,534 | +859,463 | +117,013 | +129,595 | +875,526 | +Yes | +
Sqlite | +269,812 | +29,627 | +129,735 | +174,445 | +1,406,553 | +122,566 | +No | +
Postgres | +354,894 | +24,329 | +76,189 | +324,546 | +3,702,647 | +50,305 | +Yes | +
MongoDB | +515,938 | +19,684 | +81,558 | +243,353 | +485,192 | +67,751 | +Yes | +
Clickhouse | +1,436,992 | +11,507 | +22,196 | +931,026 | +3,276,847 | +16,644 | +Yes | +
Encoder: | +exact |
+ default |
+ simple |
+ balance |
+ advanced |
+ extra |
+ soundex |
+
---|---|---|---|---|---|---|---|
Index Size | +3.1 Mb | +1.9 Mb | +1.8 Mb | +1.7 Mb | +1.6 Mb | +1.1 Mb | +0.7 Mb | +
Struldbrugs | +✓ | +✓ | +✓ | +✓ | +✓ | +✓ | +✓ | +
struldbrugs | ++ | ✓ | +✓ | +✓ | +✓ | +✓ | +✓ | +
strũldbrųĝgs | ++ | + | ✓ | +✓ | +✓ | +✓ | +✓ | +
strultbrooks | ++ | + | + | ✓ | +✓ | +✓ | +✓ | +
shtruhldbrohkz | ++ | + | + | + | ✓ | +✓ | +✓ | +
zdroltbrykz | ++ | + | + | + | + | ✓ | +✓ | +
struhlbrogger | ++ | + | + | + | + | + | ✓ | +
Field | -Category | -Description | +Build | +File | +CDN |
encode | -charset | -The encoder function. Has to return an array of separated words (or an empty string). | +flexsearch.bundle.debug.js | +Download | +https://rawcdn.githack.com/nextapps-de/flexsearch/0.8.0/dist/flexsearch.bundle.debug.js |
rtl | -charset | -A boolean property which indicates right-to-left encoding. | +flexsearch.bundle.min.js | +Download | +https://rawcdn.githack.com/nextapps-de/flexsearch/0.8.0/dist/flexsearch.bundle.min.js |
filter | -language | -Filter are also known as "stopwords", they completely filter out words from being indexed. | +flexsearch.bundle.module.debug.js | +Download | +https://rawcdn.githack.com/nextapps-de/flexsearch/0.8.0/dist/flexsearch.bundle.module.debug.js |
stemmer | -language | -Stemmer removes word endings and is a kind of "partial normalization". A word ending just matched when the word length is bigger than the matched partial. | +flexsearch.bundle.module.min.js | +Download | +https://rawcdn.githack.com/nextapps-de/flexsearch/0.8.0/dist/flexsearch.bundle.module.min.js |
matcher | -language | -Matcher replaces all occurrences of a given string regardless of its position and is also a kind of "partial normalization". | +flexsearch.es5.debug.js | +Download | +https://rawcdn.githack.com/nextapps-de/flexsearch/0.8.0/dist/flexsearch.es5.debug.js | +
flexsearch.es5.min.js | +Download | +https://rawcdn.githack.com/nextapps-de/flexsearch/0.8.0/dist/flexsearch.es5.min.js | +|||
flexsearch.light.debug.js | +Download | +https://rawcdn.githack.com/nextapps-de/flexsearch/0.8.0/dist/flexsearch.light.debug.js | +|||
flexsearch.light.min.js | +Download | +https://rawcdn.githack.com/nextapps-de/flexsearch/0.8.0/dist/flexsearch.light.min.js | +|||
flexsearch.light.module.debug.js | +Download | +https://rawcdn.githack.com/nextapps-de/flexsearch/0.8.0/dist/flexsearch.light.module.debug.js | +|||
flexsearch.light.module.min.js | +Download | +https://rawcdn.githack.com/nextapps-de/flexsearch/0.8.0/dist/flexsearch.light.module.min.js | +|||
Javascript Modules | +Download | +https://github.com/nextapps-de/flexsearch/tree/0.8.0/dist/module | +|||
Javascript Modules (Minified) | +Download | +https://github.com/nextapps-de/flexsearch/tree/0.8.0/dist/module-min | +|||
Javascript Modules (Debug) | +Download | +https://github.com/nextapps-de/flexsearch/tree/0.8.0/dist/module-debug | +|||
flexsearch.custom.js | +Read more about "Custom Build" |
-
-
Query | -default | -simple | -advanced | -extra | -
björn | -yes | -yes | -yes | -yes | -
björ | -yes | -yes | -yes | -yes | -
bjorn | -no | -yes | -yes | -yes | -
bjoern | -no | -no | -yes | -yes | -
philipp | -no | -no | -yes | -yes | -
filip | -no | -no | -yes | -yes | -
björnphillip | -no | -yes | -yes | -yes | -
meier | -no | -no | -yes | -yes | -
björn meier | -no | -no | -yes | -yes | -
meier fhilip | -no | -no | -yes | -yes | -
byorn mair | -no | -no | -no | -yes | -
(false positives) | -no | -no | -no | -yes | -
Modifier | -Memory Impact * | -Performance Impact ** | -Matching Impact ** | -Scoring Impact ** | -
resolution | -+1 (per level) | -+1 (per level) | -0 | -+2 (per level) | -
depth | -+4 (per level) | --1 (per level) | --10 + depth | -+10 | -
minlength | --2 (per level) | -+2 (per level) | --3 (per level) | -+2 (per level) | -
bidirectional | --2 | -0 | -+3 | --1 | -
fastupdate | -+1 | -+10 (update, remove) | -0 | -0 | -
optimize: true | --7 | --1 | -0 | --3 | -
encoder: "icase" | -0 | -0 | -0 | -0 | -
encoder: "simple" | --2 | --1 | -+2 | -0 | -
encoder: "advanced" | --3 | --2 | -+4 | -0 | -
encoder: "extra" | --5 | --5 | -+6 | -0 | -
encoder: "soundex" | --6 | --2 | -+8 | -0 | -
tokenize: "strict" | -0 | -0 | -0 | -0 | -
tokenize: "forward" | -+3 | --2 | -+5 | -0 | -
tokenize: "reverse" | -+5 | --4 | -+7 | -0 | -
tokenize: "full" | -+8 | --5 | -+10 | -0 | -
document index | -+3 (per field) | --1 (per field) | -0 | -0 | -
document tags | -+1 (per tag) | --1 (per tag) | -0 | -0 | -
store: true | -+5 (per document) | -0 | -0 | -0 | -
store: [fields] | -+1 (per field) | -0 | -0 | -0 | -
cache: true | -+10 | -+10 | -0 | -0 | -
cache: 100 | -+1 | -+9 | -0 | -0 | -
type of ids: number | -0 | -0 | -0 | -0 | -
type of ids: string | -+3 | --3 | -0 | -0 | -
p?0:1),d,k,q-1,r-1),u=this.bidirectional&&l>n;Xa(this,e,u?n:l,x,a,c,u?l:n)}}}}this.fastupdate||this.B.add(a)}else b=""}this.db&& +(b||this.X.push({del:a}),this.qa&&Ya(this));return this}; +function Xa(a,b,c,d,e,h,f){var g=f?a.M:a.map,k;if(!b[c]||!f||!(k=b[c])[f])if(f?(b=k||(b[c]=I()),b[f]=1,(k=g.get(f))?g=k:g.set(f,g=new Map)):b[c]=1,(k=g.get(c))?g=k:g.set(c,g=k=[]),g=g[d]||(g[d]=[]),!h||!g.includes(e)){if(g.length===Math.pow(2,31)-1){b=new Q(g);if(a.fastupdate)for(c=y(a.B.values()),h=c.next();!h.done;h=c.next())h=h.value,h.includes(g)&&(h[h.indexOf(g)]=b);k[d]=g=b}g.push(e);a.fastupdate&&((d=a.B.get(e))?d.push(g):a.B.set(e,[g]))}} +function Wa(a,b,c,d,e){return c&&1b?b?a.slice(c,c+b):a.slice(c):a,d?Za(a):a;for(var e=[],h=0,f=void 0,g=void 0;h=g){c-=g;continue}c b&&(f=f.slice(0,b),g=f.length),e.push(f);else{if(g>=b)return g>b&&(f=f.slice(0,b)),d?Za(f):f;e=[f]}b-=g;if(!b)break}if(!e.length)return e;e=1 a.length)return e?V(a[0],b,c,d):a[0];d=[];for(var f=0,g=I(),k=Ba(a),l=0,m;l a.length)return[];var h=[],f=0,g=I(),k=Ba(a);if(!k)return h;for(var l=0,m;l a.length)return e?V(a[0],b,c,d):a[0];b=[];c=I();d=0;for(var f;d =e)))break;if(g.length){if(f)return V(g,e,0);b.push(g);return}}return!c&&g}function X(a,b,c,d,e,h,f,g){var k;c&&(k=a.bidirectional&&b>c);if(a.db)return c?a.db.get(k?c:b,k?b:c,d,e,h,f,g):a.db.get(b,"",d,e,h,f,g);a=c?(a=a.M.get(k?b:c))&&a.get(k?c:b):a.map.get(b);return a};U.prototype.remove=function(a,b){var c=this.B.size&&(this.fastupdate?this.B.get(a):this.B.has(a));if(c){if(this.fastupdate)for(var d=0,e;d e.length)e.pop();else{var h=e.indexOf(a);h===c.length-1?e.pop():e.splice(h,1)}}else ib(this.map,a),this.depth&&ib(this.M,a);b||this.B.delete(a)}this.db&&(this.X.push({del:a}),this.qa&&Ya(this));this.cache&&this.cache.remove(a);return this}; +function ib(a,b){var c=0;if(a.constructor===Array)for(var d=0,e=void 0,h;d c||d)a=a.slice(d,d+c);e&&(a=rb.call(this,a));return a}}function rb(a){for(var b=Array(a.length),c=0,d;c =m.length)d-=m.length;else{for(var n=c?d+Math.min(m.length-d,c):m.length,p=d;p =h.length)return[];if(!b&&!c)return h;h=h.slice(c,c+b);return d?e.enrich(h):h})}; +w.enrich=function(a){"object"!==typeof a&&(a=[a]);for(var b=this.db.transaction("reg","readonly").objectStore("reg"),c=[],d=0;d m&&!h&&"string"===typeof n&&!isNaN(n)&&(m=k.indexOf(parseInt(n,10)))&&(h=1),0<=m)if(e=1,1 this.s.get(r)), l = 1); + this.m && 1 < f.length && (this.I || (this.I = new RegExp("(" + this.i + ")", "g")), f = f.replace(this.I, r => this.m.get(r)), l = 1); + f && l && (f.length < this.G || this.filter && this.filter.has(f)) && (f = ""); + if (f && (this.v || this.l && 1 < f.length)) { + e = ""; + for (let r = 0, k = "", p, q; r < f.length; r++) { + p = f.charAt(r), p === k && this.l || ((q = this.v && this.v.get(p)) || "" === q ? q === k && this.l || !(k = q) || (e += q) : e += k = p); + } + f = e; + } + if (f && this.o) { + for (e = 0; f && e < this.o.length; e += 2) { + f = f.replace(this.o[e], this.o[e + 1]); + } + } + this.cache && h.length <= this.h && (this.A.set(h, f), this.A.size > this.L && (this.A.clear(), this.h = this.h / 1.1 | 0)); + f && b.push(f); + } + this.D && (b = this.D(b) || b); + this.cache && a.length <= this.g && (this.u.set(a, b), this.u.size > this.L && (this.u.clear(), this.g = this.g / 1.1 | 0)); + return b; +}; +function E(a) { + a.C = null; + a.u.clear(); + a.A.clear(); +} +;function F(a) { + this.limit = a && !0 !== a ? a : 1000; + this.cache = new Map(); + this.g = ""; +} +F.prototype.set = function(a, c) { + this.cache.has(a) || (this.cache.set(this.g = a, c), this.limit && this.cache.size > this.limit && this.cache.delete(this.cache.keys().next().value)); +}; +F.prototype.get = function(a) { + const c = this.cache.get(a); + c && this.limit && this.g !== a && (this.cache.delete(a), this.cache.set(this.g = a, c)); + return c; +}; +F.prototype.remove = function(a) { + for (const c of this.cache) { + const b = c[0]; + c[1].includes(a) && this.cache.delete(b); + } +}; +F.prototype.clear = function() { + this.cache.clear(); + this.g = ""; +}; +function G(a = 8) { + if (!(this instanceof G)) { + return new G(a); + } + this.index = v(); + this.i = []; + this.size = 0; + 32 < a ? (this.g = H, this.h = BigInt(a)) : (this.g = I, this.h = a); +} +G.prototype.get = function(a) { + const c = this.index[this.g(a)]; + return c && c.get(a); +}; +G.prototype.set = function(a, c) { + var b = this.g(a); + let d = this.index[b]; + d ? (b = d.size, d.set(a, c), (b -= d.size) && this.size++) : (this.index[b] = d = new Map([[a, c]]), this.i.push(d)); +}; +function J(a = 8) { + if (!(this instanceof J)) { + return new J(a); + } + this.index = v(); + this.g = []; + 32 < a ? (this.i = H, this.h = BigInt(a)) : (this.i = I, this.h = a); +} +J.prototype.add = function(a) { + var c = this.i(a); + let b = this.index[c]; + b ? (c = b.size, b.add(a), (c -= b.size) && this.size++) : (this.index[c] = b = new Set([a]), this.g.push(b)); +}; +t = G.prototype; +t.has = J.prototype.has = function(a) { + const c = this.index[this.i(a)]; + return c && c.has(a); +}; +t.delete = J.prototype.delete = function(a) { + const c = this.index[this.i(a)]; + c && c.delete(a) && this.size--; +}; +t.clear = J.prototype.clear = function() { + this.index = v(); + this.g = []; + this.size = 0; +}; +t.values = J.prototype.values = function*() { + for (let a = 0; a < this.g.length; a++) { + for (let c of this.g[a].values()) { + yield c; + } + } +}; +t.keys = J.prototype.keys = function*() { + for (let a = 0; a < this.g.length; a++) { + for (let c of this.g[a].keys()) { + yield c; + } + } +}; +t.entries = J.prototype.entries = function*() { + for (let a = 0; a < this.g.length; a++) { + for (let c of this.g[a].entries()) { + yield c; + } + } +}; +function I(a) { + let c = 2 ** this.h - 1; + if ("number" == typeof a) { + return a & c; + } + let b = 0, d = this.h + 1; + for (let e = 0; e < a.length; e++) { + b = (b * d ^ a.charCodeAt(e)) & c; + } + return 32 === this.h ? b + 2 ** 31 : b; +} +function H(a) { + let c = BigInt(2) ** this.h - BigInt(1); + var b = typeof a; + if ("bigint" === b) { + return a & c; + } + if ("number" === b) { + return BigInt(a) & c; + } + b = BigInt(0); + let d = this.h + BigInt(1); + for (let e = 0; e < a.length; e++) { + b = (b * d ^ BigInt(a.charCodeAt(e))) & c; } return b; } -function y() { - return Object.create(null); -} -function z(a, b) { - return b.length - a.length; -} -;const A = /[\p{Z}\p{S}\p{P}\p{C}]+/u; -function B(a, b) { - const c = Object.keys(a), d = c.length, e = []; - let h = "", f = 0; - for (let g = 0, l, n; g < d; g++) { - l = c[g], (n = a[l]) ? (e[f++] = new RegExp(b ? "(?!\\b)" + l + "(\\b|_)" : l, "g"), e[f++] = n) : h += (h ? "|" : "") + l; - } - h && (e[f++] = new RegExp(b ? "(?!\\b)(" + h + ")(\\b|_)" : "(" + h + ")", "g"), e[f] = ""); - return e; -} -function C(a, b) { - for (let c = 0, d = b.length; c < d && (a = a.replace(b[c], b[c + 1]), a); c += 2) { - } - return a; -} -;function E(a) { - if (a = ("" + a).toLowerCase()) { - if (this.o && (a = C(a, this.o)), this.A && 1 < a.length && (a = C(a, this.A)), A || "" === A) { - const b = a.split(A); - if (this.filter) { - a = this.filter; - const c = b.length, d = []; - for (let e = 0, h = 0; e < c; e++) { - const f = b[e]; - f && !a[f] && (d[h++] = f); - } - a = d; - } else { - a = b; - } +;const K = v(), L = v(); +var aa = {normalize:function(a) { + return a.toLowerCase(); +}, l:!1}; +const M = {memory:{resolution:1}, performance:{resolution:6, fastupdate:!0, context:{depth:1, resolution:3}}, match:{tokenize:"forward"}, score:{resolution:9, context:{depth:2, resolution:9}}}; +v(); +N.prototype.add = function(a, c, b, d) { + if (c && (a || 0 === a)) { + if (!d && !b && this.j.has(a)) { + return this.update(a, c); } - } - return a; -} -;const F = {}, G = {}; -function H(a, b, c, d) { - const e = a.length; - let h = [], f, g, l = 0; - d && (d = []); - for (let n = e - 1; 0 <= n; n--) { - const m = a[n], r = m.length, p = y(); - let q = !f; - for (let k = 0; k < r; k++) { - const u = m[k], L = u.length; - if (L) { - for (let D = 0, x, w; D < L; D++) { - if (w = u[D], f) { - if (f[w]) { - if (!n) { - if (c) { - c--; - } else { - if (h[l++] = w, l === b) { - return h; - } - } - } - if (n || d) { - p[w] = 1; - } - q = !0; - } - if (d && (x = (g[w] || 0) + 1, g[w] = x, x < e)) { - const M = d[x - 2] || (d[x - 2] = []); - M[M.length] = w; - } - } else { - p[w] = 1; - } - } - } - } - if (d) { - f || (g = p); - } else if (!q) { - return []; - } - f = p; - } - if (d) { - for (let n = d.length - 1, m, r; 0 <= n; n--) { - m = d[n]; - r = m.length; - for (let p = 0, q; p < r; p++) { - if (q = m[p], !f[q]) { - if (c) { - c--; - } else { - if (h[l++] = q, l === b) { - return h; - } - } - f[q] = 1; - } - } - } - } - return h; -} -;function I(a, b) { - if (!(this instanceof I)) { - return new I(a); - } - let c; - if (a) { - var d = a.charset; - c = a.lang; - "string" === typeof d && (-1 === d.indexOf(":") && (d += ":default"), d = G[d]); - "string" === typeof c && (c = F[c]); - } else { - a = {}; - } - let e, h, f = a.context || {}; - this.encode = a.encode || d && d.encode || E; - this.register = b || y(); - this.s = e = a.resolution || 9; - this.B = b = d && d.B || a.tokenize || "strict"; - this.i = "strict" === b && f.depth; - this.j = t(f.bidirectional); - this.g = h = t(a.optimize); - this.m = t(a.fastupdate); - this.h = a.minlength || 1; - this.C = a.boost; - this.map = h ? v(e) : y(); - this.v = e = f.resolution || 1; - this.l = h ? v(e) : y(); - this.u = d && d.u || a.rtl; - this.o = (b = a.matcher || c && c.o) && B(b, !1); - this.A = (b = a.stemmer || c && c.A) && B(b, !0); - if (a = b = a.filter || c && c.filter) { - a = b; - d = y(); - for (let g = 0, l = a.length; g < l; g++) { - d[a[g]] = 1; - } - a = d; - } - this.filter = a; -} -I.prototype.append = function(a, b) { - return this.add(a, b, !0); -}; -I.prototype.add = function(a, b, c, d) { - if (b && (a || 0 === a)) { - if (!d && !c && this.register[a]) { - return this.update(a, b); - } - b = this.encode(b); - if (d = b.length) { - const n = y(), m = y(), r = this.i, p = this.s; - for (let q = 0; q < d; q++) { - let k = b[this.u ? d - 1 - q : q]; - var e = k.length; - if (k && e >= this.h && (r || !m[k])) { - var h = J(p, d, q), f = ""; - switch(this.B) { + c = this.encoder.encode(c); + if (d = c.length) { + const r = v(), k = v(), p = this.depth, q = this.resolution; + for (let m = 0; m < d; m++) { + let n = c[this.rtl ? d - 1 - m : m]; + var e = n.length; + if (e && (p || !k[n])) { + var g = this.score ? this.score(c, n, m, null, 0) : O(q, d, m), f = ""; + switch(this.tokenize) { case "full": if (2 < e) { - for (h = 0; h < e; h++) { - for (var g = e; g > h; g--) { - if (g - h >= this.h) { - var l = J(p, d, q, e, h); - f = k.substring(h, g); - K(this, m, f, l, a, c); - } + for (g = 0; g < e; g++) { + for (var h = e; h > g; h--) { + f = n.substring(g, h); + var l = this.score ? this.score(c, n, m, f, g) : O(q, d, m, e, g); + P(this, k, f, l, a, b); } } break; } case "reverse": if (1 < e) { - for (g = e - 1; 0 < g; g--) { - f = k[g] + f, f.length >= this.h && K(this, m, f, J(p, d, q, e, g), a, c); + for (h = e - 1; 0 < h; h--) { + f = n[h] + f, l = this.score ? this.score(c, n, m, f, h) : O(q, d, m, e, h), P(this, k, f, l, a, b); } f = ""; } case "forward": if (1 < e) { - for (g = 0; g < e; g++) { - f += k[g], f.length >= this.h && K(this, m, f, h, a, c); + for (h = 0; h < e; h++) { + f += n[h], P(this, k, f, g, a, b); } break; } default: - if (this.C && (h = Math.min(h / this.C(b, k, q) | 0, p - 1)), K(this, m, k, h, a, c), r && 1 < d && q < d - 1) { - for (e = y(), f = this.v, h = k, g = Math.min(r + 1, d - q), e[h] = 1, l = 1; l < g; l++) { - if ((k = b[this.u ? d - 1 - q - l : q + l]) && k.length >= this.h && !e[k]) { - e[k] = 1; - const u = this.j && k > h; - K(this, n, u ? h : k, J(f + (d / 2 > f ? 0 : 1), d, q, g - 1, l - 1), a, c, u ? k : h); + if (P(this, k, n, g, a, b), p && 1 < d && m < d - 1) { + for (e = v(), f = this.M, g = n, h = Math.min(p + 1, d - m), e[g] = 1, l = 1; l < h; l++) { + if ((n = c[this.rtl ? d - 1 - m - l : m + l]) && !e[n]) { + e[n] = 1; + const x = this.score ? this.score(c, g, m, n, l) : O(f + (d / 2 > f ? 0 : 1), d, m, h - 1, l - 1), R = this.bidirectional && n > g; + P(this, r, R ? g : n, x, a, b, R ? n : g); } } } } } } - this.m || (this.register[a] = 1); + this.fastupdate || this.j.add(a); } } return this; }; -function J(a, b, c, d, e) { - return c && 1 < a ? b + (d || 0) <= a ? c + (e || 0) : (a - 1) / (b + (d || 0)) * (c + (e || 0)) + 1 | 0 : 0; +function P(a, c, b, d, e, g, f) { + let h = f ? a.B : a.map, l; + c[b] && f && (l = c[b])[f] || (f ? (c = l || (c[b] = v()), c[f] = 1, (l = h.get(f)) ? h = l : h.set(f, h = new Map())) : c[b] = 1, (l = h.get(b)) ? h = l : h.set(b, h = []), h = h[d] || (h[d] = []), g && h.includes(e) || (h.push(e), a.fastupdate && ((c = a.j.get(e)) ? c.push(h) : a.j.set(e, [h])))); } -function K(a, b, c, d, e, h, f) { - let g = f ? a.l : a.map; - if (!b[c] || f && !b[c][f]) { - a.g && (g = g[d]), f ? (b = b[c] || (b[c] = y()), b[f] = 1, g = g[f] || (g[f] = y())) : b[c] = 1, g = g[c] || (g[c] = []), a.g || (g = g[d] || (g[d] = [])), h && g.includes(e) || (g[g.length] = e, a.m && (a = a.register[e] || (a.register[e] = []), a[a.length] = g)); - } +function O(a, c, b, d, e) { + return b && 1 < a ? c + (d || 0) <= a ? b + (e || 0) : (a - 1) / (c + (d || 0)) * (b + (e || 0)) + 1 | 0 : 0; } -I.prototype.search = function(a, b, c) { - c || (b || "object" !== typeof a ? "object" === typeof b && (c = b) : (c = a, a = c.query)); - let d = [], e; - let h, f = 0; - if (c) { - a = c.query || a; - b = c.limit; - f = c.offset || 0; - var g = c.context; - h = !1; +;function Q(a, c, b) { + if (1 === a.length) { + return a = a[0], a = b || a.length > c ? c ? a.slice(b, b + c) : a.slice(b) : a; } - if (a && (a = this.encode("" + a), e = a.length, 1 < e)) { - c = y(); - var l = []; - for (let m = 0, r = 0, p; m < e; m++) { - if ((p = a[m]) && p.length >= this.h && !c[p]) { - if (this.g || h || this.map[p]) { - l[r++] = p, c[p] = 1; - } else { - return d; + let d = []; + for (let e = 0, g, f; e < a.length; e++) { + if ((g = a[e]) && (f = g.length)) { + if (b) { + if (b >= f) { + b -= f; + continue; + } + b < f && (g = c ? g.slice(b, b + c) : g.slice(b), f = g.length, b = 0); + } + if (d.length) { + f > c && (g = g.slice(0, c), f = g.length), d.push(g); + } else { + if (f >= c) { + return f > c && (g = g.slice(0, c)), g; + } + d = [g]; + } + c -= f; + if (!c) { + break; + } + } + } + return d.length ? d = 1 < d.length ? [].concat.apply([], d) : d[0] : d; +} +;function ba(a, c, b, d) { + var e = a.length; + let g = [], f = 0, h, l, r; + d && (d = []); + for (let k = e - 1, p; 0 <= k; k--) { + r = a[k]; + e = v(); + p = !h; + for (let q = 0, m; q < r.length; q++) { + if ((m = r[q]) && m.length) { + for (let n = 0, x; n < m.length; n++) { + if (x = m[n], h) { + if (h[x]) { + if (!k) { + if (b) { + b--; + } else { + if (g[f++] = x, f === c) { + return g; + } + } + } + if (k || d) { + e[x] = 1; + } + p = !0; + } + d && !l[x] && (l[x] = 1, (d[q] || (d[q] = [])).push(x)); + } else { + e[x] = 1; + } } } } - a = l; - e = a.length; + if (d) { + h || (l = e); + } else if (!p) { + return []; + } + h = e; } - if (!e) { + if (d) { + for (let k = d.length - 1, p, q; 0 <= k; k--) { + p = d[k]; + q = p.length; + for (let m = 0, n; m < q; m++) { + if (n = p[m], !h[n]) { + if (b) { + b--; + } else { + if (g[f++] = n, f === c) { + return g; + } + } + h[n] = 1; + } + } + } + } + return g; +} +;N.prototype.search = function(a, c, b) { + b || (c || "object" !== typeof a ? "object" === typeof c && (b = c, c = 0) : (b = a, a = "")); + let d = []; + let e, g = 0; + if (b) { + a = b.query || a; + c = b.limit || c; + g = b.offset || 0; + var f = b.context; + e = b.suggest; + } + a = this.encoder.encode(a); + b = a.length; + c || (c = 100); + if (1 === b) { + return S.call(this, a[0], "", c, g); + } + f = this.depth && !1 !== f; + if (2 === b && f && !e) { + return S.call(this, a[0], a[1], c, g); + } + let h = 0, l = 0; + if (1 < b) { + var r = v(); + const p = []; + for (let q = 0, m; q < b; q++) { + if ((m = a[q]) && !r[m]) { + if (e || T(this, m)) { + p.push(m), r[m] = 1; + } else { + return d; + } + const n = m.length; + h = Math.max(h, n); + l = l ? Math.min(l, n) : n; + } + } + a = p; + b = a.length; + } + if (!b) { return d; } - b || (b = 100); - g = this.i && 1 < e && !1 !== g; - c = 0; - let n; - g ? (n = a[0], c = 1) : 1 < e && a.sort(z); - for (let m, r; c < e; c++) { - r = a[c]; - g ? (m = N(this, d, h, b, f, 2 === e, r, n), h && !1 === m && d.length || (n = r)) : m = N(this, d, h, b, f, 1 === e, r); - if (m) { - return m; + r = 0; + let k; + if (1 === b) { + return S.call(this, a[0], "", c, g); + } + if (2 === b && f && !e) { + return S.call(this, a[0], a[1], c, g); + } + 1 < b && (f ? (k = a[0], r = 1) : 9 < h && 3 < h / l && a.sort(w)); + for (let p, q; r < b; r++) { + q = a[r]; + k ? (p = T(this, q, k), p = U(p, d, e, this.M, c, g, 2 === b), e && !1 === p && d.length || (k = q)) : (p = T(this, q), p = U(p, d, e, this.resolution, c, g, 1 === b)); + if (p) { + return p; } - if (h && c === e - 1) { - l = d.length; - if (!l) { - if (g) { - g = 0; - c = -1; + if (e && r === b - 1) { + f = d.length; + if (!f) { + if (k) { + k = ""; + r = -1; continue; } return d; } - if (1 === l) { - return O(d[0], b, f); + if (1 === f) { + return Q(d[0], c, g); } } } - return H(d, b, f, h); + return ba(d, c, g, e); }; -function N(a, b, c, d, e, h, f, g) { - let l = [], n = g ? a.l : a.map; - a.g || (n = P(n, f, g, a.j)); - if (n) { - let m = 0; - const r = Math.min(n.length, g ? a.v : a.s); - for (let p = 0, q = 0, k, u; p < r; p++) { - if (k = n[p]) { - if (a.g && (k = P(k, f, g, a.j)), e && k && h && (u = k.length, u <= e ? (e -= u, k = null) : (k = k.slice(e), e = 0)), k && (l[m++] = k, h && (q += k.length, q >= d))) { +function S(a, c, b, d) { + return (a = T(this, a, c)) && a.length ? Q(a, b, d) : []; +} +function U(a, c, b, d, e, g, f) { + let h = []; + if (a) { + d = Math.min(a.length, d); + for (let l = 0, r = 0, k; l < d; l++) { + if (k = a[l]) { + if (g && k && f && (k.length <= g ? (g -= k.length, k = null) : (k = k.slice(g), g = 0)), k && (h[l] = k, f && (r += k.length, r >= e))) { break; } } } - if (m) { - if (h) { - return O(l, d, 0); + if (h.length) { + if (f) { + return Q(h, e, 0); } - b[b.length] = l; + c.push(h); return; } } - return !c && l; + return !b && h; } -function O(a, b, c) { - a = 1 === a.length ? a[0] : [].concat.apply([], a); - return c || a.length > b ? a.slice(c, c + b) : a; -} -function P(a, b, c, d) { - c ? (d = d && b > c, a = (a = a[d ? b : c]) && a[d ? c : b]) : a = a[b]; +function T(a, c, b) { + let d; + b && (d = a.bidirectional && c > b); + a = b ? (a = a.B.get(d ? c : b)) && a.get(d ? b : c) : a.map.get(c); return a; } -I.prototype.contain = function(a) { - return !!this.register[a]; -}; -I.prototype.update = function(a, b) { - return this.remove(a).add(a, b); -}; -I.prototype.remove = function(a, b) { - const c = this.register[a]; - if (c) { - if (this.m) { - for (let d = 0, e; d < c.length; d++) { - e = c[d], e.splice(e.indexOf(a), 1); +;N.prototype.remove = function(a, c) { + const b = this.j.size && (this.fastupdate ? this.j.get(a) : this.j.has(a)); + if (b) { + if (this.fastupdate) { + for (let d = 0, e; d < b.length; d++) { + if (e = b[d]) { + if (2 > e.length) { + e.pop(); + } else { + const g = e.indexOf(a); + g === b.length - 1 ? e.pop() : e.splice(g, 1); + } + } } } else { - Q(this.map, a, this.s, this.g), this.i && Q(this.l, a, this.v, this.g); + V(this.map, a), this.depth && V(this.B, a); } - b || delete this.register[a]; + c || this.j.delete(a); } + this.cache && this.cache.remove(a); return this; }; -function Q(a, b, c, d, e) { - let h = 0; +function V(a, c) { + let b = 0; if (a.constructor === Array) { - if (e) { - b = a.indexOf(b), -1 !== b ? 1 < a.length && (a.splice(b, 1), h++) : h++; - } else { - e = Math.min(a.length, c); - for (let f = 0, g; f < e; f++) { - if (g = a[f]) { - h = Q(g, b, c, d, e), d || h || delete a[f]; + for (let d = 0, e, g; d < a.length; d++) { + if ((e = a[d]) && e.length) { + if (g = e.indexOf(c), 0 <= g) { + 1 < e.length ? (e.splice(g, 1), b++) : delete a[d]; + break; + } else { + b++; } } } } else { - for (let f in a) { - (h = Q(a[f], b, c, d, e)) || delete a[f]; + for (let d of a) { + const e = d[0], g = V(d[1], c); + g ? b += g : a.delete(e); } } - return h; + return b; } -;const R = {Index:I, Document:null, Worker:null, registerCharset:function(a, b) { - G[a] = b; -}, registerLanguage:function(a, b) { - F[a] = b; -}}; -let S; -(S = self.define) && S.amd ? S([], function() { - return R; -}) : self.exports ? self.exports = R : self.FlexSearch = R; +;function N(a, c) { + if (!(this instanceof N)) { + return new N(a); + } + if (a) { + var b = "string" === typeof a ? a : a.preset; + b && (M[b] || console.warn("Preset not found: " + b), a = Object.assign({}, M[b], a)); + } else { + a = {}; + } + b = a.context || {}; + const d = a.encode || a.encoder || aa; + this.encoder = d.encode ? d : "object" === typeof d ? new D(d) : {encode:d}; + let e; + this.resolution = a.resolution || 9; + this.tokenize = e = a.tokenize || "strict"; + this.depth = "strict" === e && b.depth || 0; + this.bidirectional = !1 !== b.bidirectional; + this.fastupdate = !!a.fastupdate; + this.score = a.score || null; + this.map = (e = !1, new Map()); + this.B = e ? new G(e) : new Map(); + this.j = c || (this.fastupdate ? e ? new G(e) : new Map() : e ? new J(e) : new Set()); + this.M = b.resolution || 1; + this.rtl = d.rtl || a.rtl || !1; + this.cache = (e = a.cache || null) && new F(e); +} +t = N.prototype; +t.clear = function() { + this.map.clear(); + this.B.clear(); + this.j.clear(); + this.cache && this.cache.clear(); + return this; +}; +t.append = function(a, c) { + return this.add(a, c, !0); +}; +t.contain = function(a) { + return this.j.has(a); +}; +t.update = function(a, c) { + if (this.async) { + const b = this, d = this.remove(a); + return d.then ? d.then(() => b.add(a, c)) : this.add(a, c); + } + return this.remove(a).add(a, c); +}; +function W(a) { + let c = 0; + if (a.constructor === Array) { + for (let b = 0, d; b < a.length; b++) { + (d = a[b]) && (c += d.length); + } + } else { + for (const b of a) { + const d = b[0], e = W(b[1]); + e ? c += e : a.delete(d); + } + } + return c; +} +t.cleanup = function() { + if (!this.fastupdate) { + return console.info('Cleanup the index isn\'t required when not using "fastupdate".'), this; + } + W(this.map); + this.depth && W(this.B); + return this; +}; +t.searchCache = function(a, c, b) { + a = ("object" === typeof a ? "" + a.query : a).toLowerCase(); + let d = this.cache.get(a); + if (!d) { + d = this.search(a, c, b); + if (d instanceof Promise) { + const e = this; + d.then(function(g) { + e.cache.set(a, g); + }); + } + this.cache.set(a, d); + } + return d; +}; +const X = {Index:N, Encoder:D, Charset:L, Language:K, Document:null, Worker:null, Resolver:null, IndexedDB:null}, Y = self; +let Z; +(Z = Y.define) && Z.amd ? Z([], function() { + return X; +}) : "object" === typeof Y.exports ? Y.exports = X : Y.FlexSearch = X; }(this)); diff --git a/dist/flexsearch.light.min.js b/dist/flexsearch.light.min.js index 26454d2..f212104 100644 --- a/dist/flexsearch.light.min.js +++ b/dist/flexsearch.light.min.js @@ -1,18 +1,28 @@ /**! - * FlexSearch.js v0.7.41 (Light) + * FlexSearch.js v0.8.0 (Light) * Author and Copyright: Thomas Wilkerling * Licence: Apache-2.0 * Hosted by Nextapps GmbH * https://github.com/nextapps-de/flexsearch */ -(function(self){'use strict';function t(a){return"undefined"!==typeof a?a:!0}function v(a){const b=Array(a);for(let c=0;c=this.h&&(r||!m[k])){var h=J(p,d,q),f="";switch(this.B){case "full":if(2 h;g--)if(g-h>=this.h){var l=J(p,d,q,e,h);f=k.substring(h,g);K(this,m,f,l,a,c)}break}case "reverse":if(1 =this.h&&K(this, -m,f,J(p,d,q,e,g),a,c);f=""}case "forward":if(1 =this.h&&K(this,m,f,h,a,c);break}default:if(this.C&&(h=Math.min(h/this.C(b,k,q)|0,p-1)),K(this,m,k,h,a,c),r&&1 =this.h&&!e[k]){e[k]=1;const u=this.j&&k>h;K(this,n,u?h:k,J(f+(d/2>f?0:1),d,q,g-1,l-1),a,c,u?k:h)}}}}this.m||(this.register[a]=1)}}return this}; -function J(a,b,c,d,e){return c&&1=this.h&&!c[p])if(this.g||h||this.map[p])l[r++]=p,c[p]=1;else return d;a=l;e=a.length}if(!e)return d;b||(b=100);g=this.i&&1 =d)))break;if(m){if(h)return O(l,d,0);b[b.length]=l;return}}return!c&&l}function O(a,b,c){a=1===a.length?a[0]:[].concat.apply([],a);return c||a.length>b?a.slice(c,c+b):a} -function P(a,b,c,d){c?(d=d&&b>c,a=(a=a[d?b:c])&&a[d?c:b]):a=a[b];return a}I.prototype.contain=function(a){return!!this.register[a]};I.prototype.update=function(a,b){return this.remove(a).add(a,b)};I.prototype.remove=function(a,b){const c=this.register[a];if(c){if(this.m)for(let d=0,e;d this.s.get(r)),l=1);this.m&&1 this.m.get(r)), +l=1);f&&l&&(f.length this.L&&(this.A.clear(),this.h=this.h/1.1|0));f&&b.push(f)}this.D&&(b=this.D(b)||b);this.cache&&a.length<=this.g&& +(this.u.set(a,b),this.u.size>this.L&&(this.u.clear(),this.g=this.g/1.1|0));return b};function E(a){a.C=null;a.u.clear();a.A.clear()};function F(a){this.limit=a&&!0!==a?a:1E3;this.cache=new Map;this.g=""}F.prototype.set=function(a,c){this.cache.has(a)||(this.cache.set(this.g=a,c),this.limit&&this.cache.size>this.limit&&this.cache.delete(this.cache.keys().next().value))};F.prototype.get=function(a){const c=this.cache.get(a);c&&this.limit&&this.g!==a&&(this.cache.delete(a),this.cache.set(this.g=a,c));return c};F.prototype.remove=function(a){for(const c of this.cache){const b=c[0];c[1].includes(a)&&this.cache.delete(b)}}; +F.prototype.clear=function(){this.cache.clear();this.g=""};function G(a=8){if(!(this instanceof G))return new G(a);this.index=v();this.i=[];this.size=0;32g;h--){f=n.substring(g,h);var l=this.score?this.score(c,n,m,f,g):O(q,d,m,e,g);P(this,k,f,l,a,b)}break}case "reverse":if(1< +e){for(h=e-1;0 f?0:1),d,m,h-1,l-1),Q=this.bidirectional&&n>g;P(this,r,Q?g:n,x,a,b,Q?n:g)}}}}this.fastupdate||this.j.add(a)}}return this}; +function P(a,c,b,d,e,g,f){let h=f?a.B:a.map,l;c[b]&&f&&(l=c[b])[f]||(f?(c=l||(c[b]=v()),c[f]=1,(l=h.get(f))?h=l:h.set(f,h=new Map)):c[b]=1,(l=h.get(b))?h=l:h.set(b,h=[]),h=h[d]||(h[d]=[]),g&&h.includes(e)||(h.push(e),a.fastupdate&&((c=a.j.get(e))?c.push(h):a.j.set(e,[h]))))}function O(a,c,b,d,e){return b&&1c?c?a.slice(b,b+c):a.slice(b):a;let d=[];for(let e=0,g,f;e =f){b-=f;continue}b c&&(g=g.slice(0,c),f=g.length),d.push(g);else{if(f>=c)return f>c&&(g=g.slice(0,c)),g;d=[g]}c-=f;if(!c)break}return d.length?d=1 =e)))break;if(h.length){if(f)return R(h,e,0);c.push(h);return}}return!b&&h}function T(a,c,b){let d;b&&(d=a.bidirectional&&c>b);a=b?(a=a.B.get(d?c:b))&&a.get(d?b:c):a.map.get(c);return a};N.prototype.remove=function(a,c){const b=this.j.size&&(this.fastupdate?this.j.get(a):this.j.has(a));if(b){if(this.fastupdate)for(let d=0,e;d e.length)e.pop();else{const g=e.indexOf(a);g===b.length-1?e.pop():e.splice(g,1)}}else V(this.map,a),this.depth&&V(this.B,a);c||this.j.delete(a)}this.cache&&this.cache.remove(a);return this}; +function V(a,c){let b=0;if(a.constructor===Array)for(let d=0,e,g;d b.add(a,c)):this.add(a,c)}return this.remove(a).add(a,c)};function W(a){let c=0;if(a.constructor===Array)for(let b=0,d;b this.s.get(r)), l = 1); + this.m && 1 < f.length && (this.I || (this.I = new RegExp("(" + this.i + ")", "g")), f = f.replace(this.I, r => this.m.get(r)), l = 1); + f && l && (f.length < this.G || this.filter && this.filter.has(f)) && (f = ""); + if (f && (this.v || this.l && 1 < f.length)) { + e = ""; + for (let r = 0, k = "", p, q; r < f.length; r++) { + p = f.charAt(r), p === k && this.l || ((q = this.v && this.v.get(p)) || "" === q ? q === k && this.l || !(k = q) || (e += q) : e += k = p); + } + f = e; + } + if (f && this.o) { + for (e = 0; f && e < this.o.length; e += 2) { + f = f.replace(this.o[e], this.o[e + 1]); + } + } + this.cache && h.length <= this.h && (this.A.set(h, f), this.A.size > this.L && (this.A.clear(), this.h = this.h / 1.1 | 0)); + f && b.push(f); + } + this.D && (b = this.D(b) || b); + this.cache && a.length <= this.g && (this.u.set(a, b), this.u.size > this.L && (this.u.clear(), this.g = this.g / 1.1 | 0)); + return b; +}; +function E(a) { + a.C = null; + a.u.clear(); + a.A.clear(); +} +;function F(a) { + this.limit = a && !0 !== a ? a : 1000; + this.cache = new Map(); + this.g = ""; +} +F.prototype.set = function(a, c) { + this.cache.has(a) || (this.cache.set(this.g = a, c), this.limit && this.cache.size > this.limit && this.cache.delete(this.cache.keys().next().value)); +}; +F.prototype.get = function(a) { + const c = this.cache.get(a); + c && this.limit && this.g !== a && (this.cache.delete(a), this.cache.set(this.g = a, c)); + return c; +}; +F.prototype.remove = function(a) { + for (const c of this.cache) { + const b = c[0]; + c[1].includes(a) && this.cache.delete(b); + } +}; +F.prototype.clear = function() { + this.cache.clear(); + this.g = ""; +}; +function G(a = 8) { + if (!(this instanceof G)) { + return new G(a); + } + this.index = v(); + this.i = []; + this.size = 0; + 32 < a ? (this.g = H, this.h = BigInt(a)) : (this.g = I, this.h = a); +} +G.prototype.get = function(a) { + const c = this.index[this.g(a)]; + return c && c.get(a); +}; +G.prototype.set = function(a, c) { + var b = this.g(a); + let d = this.index[b]; + d ? (b = d.size, d.set(a, c), (b -= d.size) && this.size++) : (this.index[b] = d = new Map([[a, c]]), this.i.push(d)); +}; +function J(a = 8) { + if (!(this instanceof J)) { + return new J(a); + } + this.index = v(); + this.g = []; + 32 < a ? (this.i = H, this.h = BigInt(a)) : (this.i = I, this.h = a); +} +J.prototype.add = function(a) { + var c = this.i(a); + let b = this.index[c]; + b ? (c = b.size, b.add(a), (c -= b.size) && this.size++) : (this.index[c] = b = new Set([a]), this.g.push(b)); +}; +t = G.prototype; +t.has = J.prototype.has = function(a) { + const c = this.index[this.i(a)]; + return c && c.has(a); +}; +t.delete = J.prototype.delete = function(a) { + const c = this.index[this.i(a)]; + c && c.delete(a) && this.size--; +}; +t.clear = J.prototype.clear = function() { + this.index = v(); + this.g = []; + this.size = 0; +}; +t.values = J.prototype.values = function*() { + for (let a = 0; a < this.g.length; a++) { + for (let c of this.g[a].values()) { + yield c; + } + } +}; +t.keys = J.prototype.keys = function*() { + for (let a = 0; a < this.g.length; a++) { + for (let c of this.g[a].keys()) { + yield c; + } + } +}; +t.entries = J.prototype.entries = function*() { + for (let a = 0; a < this.g.length; a++) { + for (let c of this.g[a].entries()) { + yield c; + } + } +}; +function I(a) { + let c = 2 ** this.h - 1; + if ("number" == typeof a) { + return a & c; + } + let b = 0, d = this.h + 1; + for (let e = 0; e < a.length; e++) { + b = (b * d ^ a.charCodeAt(e)) & c; + } + return 32 === this.h ? b + 2 ** 31 : b; +} +function H(a) { + let c = BigInt(2) ** this.h - BigInt(1); + var b = typeof a; + if ("bigint" === b) { + return a & c; + } + if ("number" === b) { + return BigInt(a) & c; + } + b = BigInt(0); + let d = this.h + BigInt(1); + for (let e = 0; e < a.length; e++) { + b = (b * d ^ BigInt(a.charCodeAt(e))) & c; } return b; } -function y() { - return Object.create(null); -} -function z(a, b) { - return b.length - a.length; -} -;const A = /[\p{Z}\p{S}\p{P}\p{C}]+/u; -function B(a, b) { - const c = Object.keys(a), d = c.length, e = []; - let h = "", f = 0; - for (let g = 0, l, n; g < d; g++) { - l = c[g], (n = a[l]) ? (e[f++] = new RegExp(b ? "(?!\\b)" + l + "(\\b|_)" : l, "g"), e[f++] = n) : h += (h ? "|" : "") + l; - } - h && (e[f++] = new RegExp(b ? "(?!\\b)(" + h + ")(\\b|_)" : "(" + h + ")", "g"), e[f] = ""); - return e; -} -function C(a, b) { - for (let c = 0, d = b.length; c < d && (a = a.replace(b[c], b[c + 1]), a); c += 2) { - } - return a; -} -;function E(a) { - if (a = ("" + a).toLowerCase()) { - if (this.o && (a = C(a, this.o)), this.A && 1 < a.length && (a = C(a, this.A)), A || "" === A) { - const b = a.split(A); - if (this.filter) { - a = this.filter; - const c = b.length, d = []; - for (let e = 0, h = 0; e < c; e++) { - const f = b[e]; - f && !a[f] && (d[h++] = f); - } - a = d; - } else { - a = b; - } +;const K = v(), L = v(); +var M = {normalize:function(a) { + return a.toLowerCase(); +}, l:!1}; +const N = {memory:{resolution:1}, performance:{resolution:6, fastupdate:!0, context:{depth:1, resolution:3}}, match:{tokenize:"forward"}, score:{resolution:9, context:{depth:2, resolution:9}}}; +v(); +P.prototype.add = function(a, c, b, d) { + if (c && (a || 0 === a)) { + if (!d && !b && this.j.has(a)) { + return this.update(a, c); } - } - return a; -} -;const F = {}, G = {}; -function H(a, b, c, d) { - const e = a.length; - let h = [], f, g, l = 0; - d && (d = []); - for (let n = e - 1; 0 <= n; n--) { - const m = a[n], r = m.length, p = y(); - let q = !f; - for (let k = 0; k < r; k++) { - const u = m[k], J = u.length; - if (J) { - for (let D = 0, x, w; D < J; D++) { - if (w = u[D], f) { - if (f[w]) { - if (!n) { - if (c) { - c--; - } else { - if (h[l++] = w, l === b) { - return h; - } - } - } - if (n || d) { - p[w] = 1; - } - q = !0; - } - if (d && (x = (g[w] || 0) + 1, g[w] = x, x < e)) { - const K = d[x - 2] || (d[x - 2] = []); - K[K.length] = w; - } - } else { - p[w] = 1; - } - } - } - } - if (d) { - f || (g = p); - } else if (!q) { - return []; - } - f = p; - } - if (d) { - for (let n = d.length - 1, m, r; 0 <= n; n--) { - m = d[n]; - r = m.length; - for (let p = 0, q; p < r; p++) { - if (q = m[p], !f[q]) { - if (c) { - c--; - } else { - if (h[l++] = q, l === b) { - return h; - } - } - f[q] = 1; - } - } - } - } - return h; -} -;function I(a, b) { - if (!(this instanceof I)) { - return new I(a); - } - let c; - if (a) { - var d = a.charset; - c = a.lang; - "string" === typeof d && (-1 === d.indexOf(":") && (d += ":default"), d = G[d]); - "string" === typeof c && (c = F[c]); - } else { - a = {}; - } - let e, h, f = a.context || {}; - this.encode = a.encode || d && d.encode || E; - this.register = b || y(); - this.s = e = a.resolution || 9; - this.B = b = d && d.B || a.tokenize || "strict"; - this.i = "strict" === b && f.depth; - this.j = t(f.bidirectional); - this.g = h = t(a.optimize); - this.m = t(a.fastupdate); - this.h = a.minlength || 1; - this.C = a.boost; - this.map = h ? v(e) : y(); - this.v = e = f.resolution || 1; - this.l = h ? v(e) : y(); - this.u = d && d.u || a.rtl; - this.o = (b = a.matcher || c && c.o) && B(b, !1); - this.A = (b = a.stemmer || c && c.A) && B(b, !0); - if (a = b = a.filter || c && c.filter) { - a = b; - d = y(); - for (let g = 0, l = a.length; g < l; g++) { - d[a[g]] = 1; - } - a = d; - } - this.filter = a; -} -I.prototype.append = function(a, b) { - return this.add(a, b, !0); -}; -I.prototype.add = function(a, b, c, d) { - if (b && (a || 0 === a)) { - if (!d && !c && this.register[a]) { - return this.update(a, b); - } - b = this.encode(b); - if (d = b.length) { - const n = y(), m = y(), r = this.i, p = this.s; - for (let q = 0; q < d; q++) { - let k = b[this.u ? d - 1 - q : q]; - var e = k.length; - if (k && e >= this.h && (r || !m[k])) { - var h = L(p, d, q), f = ""; - switch(this.B) { + c = this.encoder.encode(c); + if (d = c.length) { + const r = v(), k = v(), p = this.depth, q = this.resolution; + for (let m = 0; m < d; m++) { + let n = c[this.rtl ? d - 1 - m : m]; + var e = n.length; + if (e && (p || !k[n])) { + var g = this.score ? this.score(c, n, m, null, 0) : Q(q, d, m), f = ""; + switch(this.tokenize) { case "full": if (2 < e) { - for (h = 0; h < e; h++) { - for (var g = e; g > h; g--) { - if (g - h >= this.h) { - var l = L(p, d, q, e, h); - f = k.substring(h, g); - M(this, m, f, l, a, c); - } + for (g = 0; g < e; g++) { + for (var h = e; h > g; h--) { + f = n.substring(g, h); + var l = this.score ? this.score(c, n, m, f, g) : Q(q, d, m, e, g); + R(this, k, f, l, a, b); } } break; } case "reverse": if (1 < e) { - for (g = e - 1; 0 < g; g--) { - f = k[g] + f, f.length >= this.h && M(this, m, f, L(p, d, q, e, g), a, c); + for (h = e - 1; 0 < h; h--) { + f = n[h] + f, l = this.score ? this.score(c, n, m, f, h) : Q(q, d, m, e, h), R(this, k, f, l, a, b); } f = ""; } case "forward": if (1 < e) { - for (g = 0; g < e; g++) { - f += k[g], f.length >= this.h && M(this, m, f, h, a, c); + for (h = 0; h < e; h++) { + f += n[h], R(this, k, f, g, a, b); } break; } default: - if (this.C && (h = Math.min(h / this.C(b, k, q) | 0, p - 1)), M(this, m, k, h, a, c), r && 1 < d && q < d - 1) { - for (e = y(), f = this.v, h = k, g = Math.min(r + 1, d - q), e[h] = 1, l = 1; l < g; l++) { - if ((k = b[this.u ? d - 1 - q - l : q + l]) && k.length >= this.h && !e[k]) { - e[k] = 1; - const u = this.j && k > h; - M(this, n, u ? h : k, L(f + (d / 2 > f ? 0 : 1), d, q, g - 1, l - 1), a, c, u ? k : h); + if (R(this, k, n, g, a, b), p && 1 < d && m < d - 1) { + for (e = v(), f = this.M, g = n, h = Math.min(p + 1, d - m), e[g] = 1, l = 1; l < h; l++) { + if ((n = c[this.rtl ? d - 1 - m - l : m + l]) && !e[n]) { + e[n] = 1; + const x = this.score ? this.score(c, g, m, n, l) : Q(f + (d / 2 > f ? 0 : 1), d, m, h - 1, l - 1), O = this.bidirectional && n > g; + R(this, r, O ? g : n, x, a, b, O ? n : g); } } } } } } - this.m || (this.register[a] = 1); + this.fastupdate || this.j.add(a); } } return this; }; -function L(a, b, c, d, e) { - return c && 1 < a ? b + (d || 0) <= a ? c + (e || 0) : (a - 1) / (b + (d || 0)) * (c + (e || 0)) + 1 | 0 : 0; +function R(a, c, b, d, e, g, f) { + let h = f ? a.B : a.map, l; + c[b] && f && (l = c[b])[f] || (f ? (c = l || (c[b] = v()), c[f] = 1, (l = h.get(f)) ? h = l : h.set(f, h = new Map())) : c[b] = 1, (l = h.get(b)) ? h = l : h.set(b, h = []), h = h[d] || (h[d] = []), g && h.includes(e) || (h.push(e), a.fastupdate && ((c = a.j.get(e)) ? c.push(h) : a.j.set(e, [h])))); } -function M(a, b, c, d, e, h, f) { - let g = f ? a.l : a.map; - if (!b[c] || f && !b[c][f]) { - a.g && (g = g[d]), f ? (b = b[c] || (b[c] = y()), b[f] = 1, g = g[f] || (g[f] = y())) : b[c] = 1, g = g[c] || (g[c] = []), a.g || (g = g[d] || (g[d] = [])), h && g.includes(e) || (g[g.length] = e, a.m && (a = a.register[e] || (a.register[e] = []), a[a.length] = g)); - } +function Q(a, c, b, d, e) { + return b && 1 < a ? c + (d || 0) <= a ? b + (e || 0) : (a - 1) / (c + (d || 0)) * (b + (e || 0)) + 1 | 0 : 0; } -I.prototype.search = function(a, b, c) { - c || (b || "object" !== typeof a ? "object" === typeof b && (c = b) : (c = a, a = c.query)); - let d = [], e; - let h, f = 0; - if (c) { - a = c.query || a; - b = c.limit; - f = c.offset || 0; - var g = c.context; - h = !1; +;function S(a, c, b) { + if (1 === a.length) { + return a = a[0], a = b || a.length > c ? c ? a.slice(b, b + c) : a.slice(b) : a; } - if (a && (a = this.encode("" + a), e = a.length, 1 < e)) { - c = y(); - var l = []; - for (let m = 0, r = 0, p; m < e; m++) { - if ((p = a[m]) && p.length >= this.h && !c[p]) { - if (this.g || h || this.map[p]) { - l[r++] = p, c[p] = 1; - } else { - return d; + let d = []; + for (let e = 0, g, f; e < a.length; e++) { + if ((g = a[e]) && (f = g.length)) { + if (b) { + if (b >= f) { + b -= f; + continue; + } + b < f && (g = c ? g.slice(b, b + c) : g.slice(b), f = g.length, b = 0); + } + if (d.length) { + f > c && (g = g.slice(0, c), f = g.length), d.push(g); + } else { + if (f >= c) { + return f > c && (g = g.slice(0, c)), g; + } + d = [g]; + } + c -= f; + if (!c) { + break; + } + } + } + return d.length ? d = 1 < d.length ? [].concat.apply([], d) : d[0] : d; +} +;function T(a, c, b, d) { + var e = a.length; + let g = [], f = 0, h, l, r; + d && (d = []); + for (let k = e - 1, p; 0 <= k; k--) { + r = a[k]; + e = v(); + p = !h; + for (let q = 0, m; q < r.length; q++) { + if ((m = r[q]) && m.length) { + for (let n = 0, x; n < m.length; n++) { + if (x = m[n], h) { + if (h[x]) { + if (!k) { + if (b) { + b--; + } else { + if (g[f++] = x, f === c) { + return g; + } + } + } + if (k || d) { + e[x] = 1; + } + p = !0; + } + d && !l[x] && (l[x] = 1, (d[q] || (d[q] = [])).push(x)); + } else { + e[x] = 1; + } } } } - a = l; - e = a.length; + if (d) { + h || (l = e); + } else if (!p) { + return []; + } + h = e; } - if (!e) { + if (d) { + for (let k = d.length - 1, p, q; 0 <= k; k--) { + p = d[k]; + q = p.length; + for (let m = 0, n; m < q; m++) { + if (n = p[m], !h[n]) { + if (b) { + b--; + } else { + if (g[f++] = n, f === c) { + return g; + } + } + h[n] = 1; + } + } + } + } + return g; +} +;P.prototype.search = function(a, c, b) { + b || (c || "object" !== typeof a ? "object" === typeof c && (b = c, c = 0) : (b = a, a = "")); + let d = []; + let e, g = 0; + if (b) { + a = b.query || a; + c = b.limit || c; + g = b.offset || 0; + var f = b.context; + e = !1; + } + a = this.encoder.encode(a); + b = a.length; + c || (c = 100); + if (1 === b) { + return U.call(this, a[0], "", c, g); + } + f = this.depth && !1 !== f; + if (2 === b && f && !e) { + return U.call(this, a[0], a[1], c, g); + } + let h = 0, l = 0; + if (1 < b) { + var r = v(); + const p = []; + for (let q = 0, m; q < b; q++) { + if ((m = a[q]) && !r[m]) { + if (e || V(this, m)) { + p.push(m), r[m] = 1; + } else { + return d; + } + const n = m.length; + h = Math.max(h, n); + l = l ? Math.min(l, n) : n; + } + } + a = p; + b = a.length; + } + if (!b) { return d; } - b || (b = 100); - g = this.i && 1 < e && !1 !== g; - c = 0; - let n; - g ? (n = a[0], c = 1) : 1 < e && a.sort(z); - for (let m, r; c < e; c++) { - r = a[c]; - g ? (m = N(this, d, h, b, f, 2 === e, r, n), h && !1 === m && d.length || (n = r)) : m = N(this, d, h, b, f, 1 === e, r); - if (m) { - return m; + r = 0; + let k; + if (1 === b) { + return U.call(this, a[0], "", c, g); + } + if (2 === b && f && !e) { + return U.call(this, a[0], a[1], c, g); + } + 1 < b && (f ? (k = a[0], r = 1) : 9 < h && 3 < h / l && a.sort(w)); + for (let p, q; r < b; r++) { + q = a[r]; + k ? (p = V(this, q, k), p = W(p, d, e, this.M, c, g, 2 === b), e && !1 === p && d.length || (k = q)) : (p = V(this, q), p = W(p, d, e, this.resolution, c, g, 1 === b)); + if (p) { + return p; } - if (h && c === e - 1) { - l = d.length; - if (!l) { - if (g) { - g = 0; - c = -1; + if (e && r === b - 1) { + f = d.length; + if (!f) { + if (k) { + k = ""; + r = -1; continue; } return d; } - if (1 === l) { - return O(d[0], b, f); + if (1 === f) { + return S(d[0], c, g); } } } - return H(d, b, f, h); + return T(d, c, g, e); }; -function N(a, b, c, d, e, h, f, g) { - let l = [], n = g ? a.l : a.map; - a.g || (n = P(n, f, g, a.j)); - if (n) { - let m = 0; - const r = Math.min(n.length, g ? a.v : a.s); - for (let p = 0, q = 0, k, u; p < r; p++) { - if (k = n[p]) { - if (a.g && (k = P(k, f, g, a.j)), e && k && h && (u = k.length, u <= e ? (e -= u, k = null) : (k = k.slice(e), e = 0)), k && (l[m++] = k, h && (q += k.length, q >= d))) { +function U(a, c, b, d) { + return (a = V(this, a, c)) && a.length ? S(a, b, d) : []; +} +function W(a, c, b, d, e, g, f) { + let h = []; + if (a) { + d = Math.min(a.length, d); + for (let l = 0, r = 0, k; l < d; l++) { + if (k = a[l]) { + if (g && k && f && (k.length <= g ? (g -= k.length, k = null) : (k = k.slice(g), g = 0)), k && (h[l] = k, f && (r += k.length, r >= e))) { break; } } } - if (m) { - if (h) { - return O(l, d, 0); + if (h.length) { + if (f) { + return S(h, e, 0); } - b[b.length] = l; + c.push(h); return; } } - return !c && l; + return !b && h; } -function O(a, b, c) { - a = 1 === a.length ? a[0] : [].concat.apply([], a); - return c || a.length > b ? a.slice(c, c + b) : a; -} -function P(a, b, c, d) { - c ? (d = d && b > c, a = (a = a[d ? b : c]) && a[d ? c : b]) : a = a[b]; +function V(a, c, b) { + let d; + b && (d = a.bidirectional && c > b); + a = b ? (a = a.B.get(d ? c : b)) && a.get(d ? b : c) : a.map.get(c); return a; } -I.prototype.contain = function(a) { - return !!this.register[a]; -}; -I.prototype.update = function(a, b) { - return this.remove(a).add(a, b); -}; -I.prototype.remove = function(a, b) { - const c = this.register[a]; - if (c) { - if (this.m) { - for (let d = 0, e; d < c.length; d++) { - e = c[d], e.splice(e.indexOf(a), 1); +;P.prototype.remove = function(a, c) { + const b = this.j.size && (this.fastupdate ? this.j.get(a) : this.j.has(a)); + if (b) { + if (this.fastupdate) { + for (let d = 0, e; d < b.length; d++) { + if (e = b[d]) { + if (2 > e.length) { + e.pop(); + } else { + const g = e.indexOf(a); + g === b.length - 1 ? e.pop() : e.splice(g, 1); + } + } } } else { - Q(this.map, a, this.s, this.g), this.i && Q(this.l, a, this.v, this.g); + X(this.map, a), this.depth && X(this.B, a); } - b || delete this.register[a]; + c || this.j.delete(a); } + this.cache && this.cache.remove(a); return this; }; -function Q(a, b, c, d, e) { - let h = 0; +function X(a, c) { + let b = 0; if (a.constructor === Array) { - if (e) { - b = a.indexOf(b), -1 !== b ? 1 < a.length && (a.splice(b, 1), h++) : h++; - } else { - e = Math.min(a.length, c); - for (let f = 0, g; f < e; f++) { - if (g = a[f]) { - h = Q(g, b, c, d, e), d || h || delete a[f]; + for (let d = 0, e, g; d < a.length; d++) { + if ((e = a[d]) && e.length) { + if (g = e.indexOf(c), 0 <= g) { + 1 < e.length ? (e.splice(g, 1), b++) : delete a[d]; + break; + } else { + b++; } } } } else { - for (let f in a) { - (h = Q(a[f], b, c, d, e)) || delete a[f]; + for (let d of a) { + const e = d[0], g = X(d[1], c); + g ? b += g : a.delete(e); } } - return h; + return b; } -;export default {Index:I, Document:null, Worker:null, registerCharset:function(a, b) { - G[a] = b; -}, registerLanguage:function(a, b) { - F[a] = b; -}}; +;function P(a, c) { + if (!(this instanceof P)) { + return new P(a); + } + if (a) { + var b = "string" === typeof a ? a : a.preset; + b && (N[b] || console.warn("Preset not found: " + b), a = Object.assign({}, N[b], a)); + } else { + a = {}; + } + b = a.context || {}; + const d = a.encode || a.encoder || M; + this.encoder = d.encode ? d : "object" === typeof d ? new D(d) : {encode:d}; + let e; + this.resolution = a.resolution || 9; + this.tokenize = e = a.tokenize || "strict"; + this.depth = "strict" === e && b.depth || 0; + this.bidirectional = !1 !== b.bidirectional; + this.fastupdate = !!a.fastupdate; + this.score = a.score || null; + this.map = (e = !1, new Map()); + this.B = e ? new G(e) : new Map(); + this.j = c || (this.fastupdate ? e ? new G(e) : new Map() : e ? new J(e) : new Set()); + this.M = b.resolution || 1; + this.rtl = d.rtl || a.rtl || !1; + this.cache = (e = a.cache || null) && new F(e); +} +t = P.prototype; +t.clear = function() { + this.map.clear(); + this.B.clear(); + this.j.clear(); + this.cache && this.cache.clear(); + return this; +}; +t.append = function(a, c) { + return this.add(a, c, !0); +}; +t.contain = function(a) { + return this.j.has(a); +}; +t.update = function(a, c) { + if (this.async) { + const b = this, d = this.remove(a); + return d.then ? d.then(() => b.add(a, c)) : this.add(a, c); + } + return this.remove(a).add(a, c); +}; +function Y(a) { + let c = 0; + if (a.constructor === Array) { + for (let b = 0, d; b < a.length; b++) { + (d = a[b]) && (c += d.length); + } + } else { + for (const b of a) { + const d = b[0], e = Y(b[1]); + e ? c += e : a.delete(d); + } + } + return c; +} +t.cleanup = function() { + if (!this.fastupdate) { + return console.info('Cleanup the index isn\'t required when not using "fastupdate".'), this; + } + Y(this.map); + this.depth && Y(this.B); + return this; +}; +t.searchCache = function(a, c, b) { + a = ("object" === typeof a ? "" + a.query : a).toLowerCase(); + let d = this.cache.get(a); + if (!d) { + d = this.search(a, c, b); + if (d instanceof Promise) { + const e = this; + d.then(function(g) { + e.cache.set(a, g); + }); + } + this.cache.set(a, d); + } + return d; +}; +export default {Index:P, Encoder:D, Charset:L, Language:K, Document:null, Worker:null, Resolver:null, IndexedDB:null}; +export const Index=P;export const Encoder=D;export const Charset=L;export const Language=K;export const Document=null;export const Worker=null;export const Resolver=null;export const IndexedDB=null; \ No newline at end of file diff --git a/dist/flexsearch.light.module.min.js b/dist/flexsearch.light.module.min.js index fe7705e..df51872 100644 --- a/dist/flexsearch.light.module.min.js +++ b/dist/flexsearch.light.module.min.js @@ -1,18 +1,29 @@ /**! - * FlexSearch.js v0.7.41 (Light.module) + * FlexSearch.js v0.8.0 (Bundle) * Author and Copyright: Thomas Wilkerling * Licence: Apache-2.0 * Hosted by Nextapps GmbH * https://github.com/nextapps-de/flexsearch */ -function t(a){return"undefined"!==typeof a?a:!0}function v(a){const b=Array(a);for(let c=0;c=this.h&&(r||!m[k])){var h=L(p,d,q),f="";switch(this.B){case "full":if(2 h;g--)if(g-h>=this.h){var l=L(p,d,q,e,h);f=k.substring(h,g);M(this,m,f,l,a,c)}break}case "reverse":if(1 =this.h&&M(this, -m,f,L(p,d,q,e,g),a,c);f=""}case "forward":if(1 =this.h&&M(this,m,f,h,a,c);break}default:if(this.C&&(h=Math.min(h/this.C(b,k,q)|0,p-1)),M(this,m,k,h,a,c),r&&1 =this.h&&!e[k]){e[k]=1;const u=this.j&&k>h;M(this,n,u?h:k,L(f+(d/2>f?0:1),d,q,g-1,l-1),a,c,u?k:h)}}}}this.m||(this.register[a]=1)}}return this}; -function L(a,b,c,d,e){return c&&1=this.h&&!c[p])if(this.g||h||this.map[p])l[r++]=p,c[p]=1;else return d;a=l;e=a.length}if(!e)return d;b||(b=100);g=this.i&&1 =d)))break;if(m){if(h)return O(l,d,0);b[b.length]=l;return}}return!c&&l}function O(a,b,c){a=1===a.length?a[0]:[].concat.apply([],a);return c||a.length>b?a.slice(c,c+b):a} -function P(a,b,c,d){c?(d=d&&b>c,a=(a=a[d?b:c])&&a[d?c:b]):a=a[b];return a}I.prototype.contain=function(a){return!!this.register[a]};I.prototype.update=function(a,b){return this.remove(a).add(a,b)};I.prototype.remove=function(a,b){const c=this.register[a];if(c){if(this.m)for(let d=0,e;d this.s.get(r)),l=1);this.m&&1 this.m.get(r)), +l=1);f&&l&&(f.length this.L&&(this.A.clear(),this.h=this.h/1.1|0));f&&b.push(f)}this.D&&(b=this.D(b)||b);this.cache&&a.length<=this.g&& +(this.u.set(a,b),this.u.size>this.L&&(this.u.clear(),this.g=this.g/1.1|0));return b};function E(a){a.C=null;a.u.clear();a.A.clear()};function F(a){this.limit=a&&!0!==a?a:1E3;this.cache=new Map;this.g=""}F.prototype.set=function(a,c){this.cache.has(a)||(this.cache.set(this.g=a,c),this.limit&&this.cache.size>this.limit&&this.cache.delete(this.cache.keys().next().value))};F.prototype.get=function(a){const c=this.cache.get(a);c&&this.limit&&this.g!==a&&(this.cache.delete(a),this.cache.set(this.g=a,c));return c};F.prototype.remove=function(a){for(const c of this.cache){const b=c[0];c[1].includes(a)&&this.cache.delete(b)}}; +F.prototype.clear=function(){this.cache.clear();this.g=""};function G(a=8){if(!(this instanceof G))return new G(a);this.index=v();this.i=[];this.size=0;32g;h--){f=n.substring(g,h);var l=this.score?this.score(c,n,m,f,g):Q(q,d,m,e,g);R(this,k,f,l,a,b)}break}case "reverse":if(1< +e){for(h=e-1;0 f?0:1),d,m,h-1,l-1),N=this.bidirectional&&n>g;R(this,r,N?g:n,x,a,b,N?n:g)}}}}this.fastupdate||this.j.add(a)}}return this}; +function R(a,c,b,d,e,g,f){let h=f?a.B:a.map,l;c[b]&&f&&(l=c[b])[f]||(f?(c=l||(c[b]=v()),c[f]=1,(l=h.get(f))?h=l:h.set(f,h=new Map)):c[b]=1,(l=h.get(b))?h=l:h.set(b,h=[]),h=h[d]||(h[d]=[]),g&&h.includes(e)||(h.push(e),a.fastupdate&&((c=a.j.get(e))?c.push(h):a.j.set(e,[h]))))}function Q(a,c,b,d,e){return b&&1c?c?a.slice(b,b+c):a.slice(b):a;let d=[];for(let e=0,g,f;e =f){b-=f;continue}b c&&(g=g.slice(0,c),f=g.length),d.push(g);else{if(f>=c)return f>c&&(g=g.slice(0,c)),g;d=[g]}c-=f;if(!c)break}return d.length?d=1 =e)))break;if(h.length){if(f)return S(h,e,0);c.push(h);return}}return!b&&h}function V(a,c,b){let d;b&&(d=a.bidirectional&&c>b);a=b?(a=a.B.get(d?c:b))&&a.get(d?b:c):a.map.get(c);return a};P.prototype.remove=function(a,c){const b=this.j.size&&(this.fastupdate?this.j.get(a):this.j.has(a));if(b){if(this.fastupdate)for(let d=0,e;d e.length)e.pop();else{const g=e.indexOf(a);g===b.length-1?e.pop():e.splice(g,1)}}else X(this.map,a),this.depth&&X(this.B,a);c||this.j.delete(a)}this.cache&&this.cache.remove(a);return this}; +function X(a,c){let b=0;if(a.constructor===Array)for(let d=0,e,g;d b.add(a,c)):this.add(a,c)}return this.remove(a).add(a,c)};function Y(a){let c=0;if(a.constructor===Array)for(let b=0,d;b setTimeout(tick, 0, resolve)) + // ); + // + // // apply different performance budgets + // if(key === "update" || key === "remove" && this.fastupdate === false){ + // budget += 1000 * this.resolution; + // if(this.depth) + // budget += 1000 * this.resolution_ctx; + // } + // else if(key === "search"){ + // budget++; + // } + // else{ + // budget += 20 * this.resolution; + // if(this.depth) + // budget += 20 * this.resolution_ctx; + // } + // + // // wait for the event loop cycle + // if(budget >= 1e6){ + // await cycle; + // } + + const args = /*[].slice.call*/arguments, arg = args[args.length - 1]; let callback; - if (is_function(arg)) { - + if ("function" == typeof arg) { callback = arg; delete args[args.length - 1]; } - const promise = new Promise(function (resolve) { - - setTimeout(function () { - - self.async = !0; - const res = self[key].apply(self, args); - self.async = !1; - resolve(res); - }); - }); - - if (callback) { - - promise.then(callback); - return this; - } else { - - return promise; - } + this.async = !0; + const res = this[key].apply(this, args); + this.async = !1; + res.then ? res.then(callback) : callback(res); + return res; }; } \ No newline at end of file diff --git a/dist/module-debug/cache.js b/dist/module-debug/cache.js index 43262f7..a21e83a 100644 --- a/dist/module-debug/cache.js +++ b/dist/module-debug/cache.js @@ -1,168 +1,77 @@ -import { IndexInterface, DocumentInterface } from "./type.js"; -import { create_object, is_object } from "./common.js"; +import Index from "./index.js"; +import Document from "./document.js"; + +/** + * @param {string|Object} query + * @param {number|Object=} limit + * @param {Object=} options + * @this {Index|Document} + * @returns {Array |Promise} + */ + +export function searchCache(query, limit, options) { + + query = ("object" == typeof query ? "" + query.query : "" + query).toLowerCase(); + + //let encoded = this.encoder.encode(query).join(" "); + let cache = this.cache.get(query); + if (!cache) { + cache = this.search(query, limit, options); + if (cache instanceof Promise) { + const self = this; + cache.then(function (cache) { + self.cache.set(query, cache); + }); + } + this.cache.set(query, cache); + } + return cache; +} /** * @param {boolean|number=} limit * @constructor */ -function CacheClass(limit) { - +export default function CacheClass(limit) { /** @private */ - this.limit = !0 !== limit && limit; - + this.limit = !limit || !0 === limit ? 1000 : limit; /** @private */ - this.cache = create_object(); - + this.cache = new Map(); /** @private */ - this.queue = []; - - //this.clear(); + this.last = ""; } -export default CacheClass; - -/** - * @param {string|Object} query - * @param {number|Object=} limit - * @param {Object=} options - * @this {IndexInterface} - * @returns {Array } - */ - -export function searchCache(query, limit, options) { - - if (is_object(query)) { - - query = query.query; - } - - let cache = this.cache.get(query); - - if (!cache) { - - cache = this.search(query, limit, options); - this.cache.set(query, cache); - } - - return cache; -} - -// CacheClass.prototype.clear = function(){ -// -// /** @private */ -// this.cache = create_object(); -// -// /** @private */ -// this.queue = []; -// }; - CacheClass.prototype.set = function (key, value) { - - if (!this.cache[key]) { - - // it is just a shame that native function array.shift() performs so bad - - // const length = this.queue.length; - // - // this.queue[length] = key; - // - // if(length === this.limit){ - // - // delete this.cache[this.queue.shift()]; - // } - - // the same bad performance - - // this.queue.unshift(key); - // - // if(this.queue.length === this.limit){ - // - // this.queue.pop(); - // } - - // fast implementation variant - - // let length = this.queue.length; - // - // if(length === this.limit){ - // - // length--; - // - // delete this.cache[this.queue[0]]; - // - // for(let x = 0; x < length; x++){ - // - // this.queue[x] = this.queue[x + 1]; - // } - // } - // - // this.queue[length] = key; - - // current fastest implementation variant - // theoretically that should not perform better compared to the example above - - let length = this.queue.length; - - if (length === this.limit) { - - delete this.cache[this.queue[length - 1]]; - } else { - - length++; + if (!this.cache.has(key)) { + this.cache.set(this.last = key, value); + if (this.limit && this.cache.size > this.limit) { + this.cache.delete(this.cache.keys().next().value); } - - for (let x = length - 1; 0 < x; x--) { - - this.queue[x] = this.queue[x - 1]; - } - - this.queue[0] = key; } - - this.cache[key] = value; }; CacheClass.prototype.get = function (key) { - - const cache = this.cache[key]; - - if (this.limit && cache) { - - // probably the indexOf() method performs faster when matched content is on front (left-to-right) - // using lastIndexOf() does not help, it performs almost slower - - const pos = this.queue.indexOf(key); - - // if(pos < this.queue.length - 1){ - // - // const tmp = this.queue[pos]; - // this.queue[pos] = this.queue[pos + 1]; - // this.queue[pos + 1] = tmp; - // } - - if (pos) { - - const tmp = this.queue[pos - 1]; - this.queue[pos - 1] = this.queue[pos]; - this.queue[pos] = tmp; - } + const cache = this.cache.get(key); + if (cache && this.limit && this.last !== key) { + this.cache.delete(key); + this.cache.set(this.last = key, cache); } - return cache; }; -CacheClass.prototype.del = function (id) { +CacheClass.prototype.remove = function (id) { + for (const item of this.cache) { + const key = item[0], + value = item[1]; - for (let i = 0, item, key; i < this.queue.length; i++) { - - key = this.queue[i]; - item = this.cache[key]; - - if (item.includes(id)) { - - this.queue.splice(i--, 1); - delete this.cache[key]; + if (value.includes(id)) { + this.cache.delete(key); } } +}; + +CacheClass.prototype.clear = function () { + this.cache.clear(); + this.last = ""; }; \ No newline at end of file diff --git a/dist/module-debug/common.js b/dist/module-debug/common.js index 95e4ace..b733012 100644 --- a/dist/module-debug/common.js +++ b/dist/module-debug/common.js @@ -1,6 +1,65 @@ -export function parse_option(value, default_value) { +/** + * @param {*} value + * @param {*} default_value + * @param {*=} merge_value + * @return {*} + */ - return "undefined" != typeof value ? value : default_value; +export function parse_option(value, default_value, merge_value) { + const type_merge = typeof merge_value, + type_value = typeof value; + + + if ("undefined" != type_merge) { + if ("undefined" != type_value) { + + if (merge_value) { + if ("function" == type_value && type_merge == type_value) { + return function (str) { + return (/** @type Function */value( + /** @type Function */merge_value(str)) + ); + }; + } + + const constructor_value = value.constructor, + constructor_merge = merge_value.constructor; + + + if (constructor_value === constructor_merge) { + + if (constructor_value === Array) { + return merge_value.concat(value); + } + + if (constructor_value === Map) { + const map = new Map( /** @type !Map */merge_value); + for (const item of /** @type !Map */value) { + const key = item[0], + val = item[1]; + + map.set(key, val); + } + return map; + } + + if (constructor_value === Set) { + const set = new Set( /** @type !Set */merge_value); + for (const val of /** @type !Set */value.values()) { + set.add(val); + } + return set; + } + } + } + + return value; + } else { + return merge_value; + } + } + + return "undefined" == type_value ? default_value : value; } /** @@ -13,19 +72,33 @@ export function create_object_array(count) { const array = Array(count); for (let i = 0; i < count; i++) { - array[i] = create_object(); } return array; } +/** + * @param {!number} count + * @returns {Array