1
0
mirror of https://github.com/nextapps-de/flexsearch.git synced 2025-09-25 04:51:29 +02:00
Files
flexsearch/dist/flexsearch.light.debug.js
2025-03-17 01:13:35 +01:00

615 lines
17 KiB
JavaScript

/**!
* FlexSearch.js v0.8.0 (Light/Debug)
* 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, c, b) {
const d = typeof b, e = typeof a;
if ("undefined" !== d) {
if ("undefined" !== e) {
if (b) {
if ("function" === e && d === e) {
return function(h) {
return a(b(h));
};
}
c = a.constructor;
if (c === b.constructor) {
if (c === Array) {
return b.concat(a);
}
if (c === Map) {
var g = new Map(b);
for (var f of a) {
g.set(f[0], f[1]);
}
return g;
}
if (c === Set) {
f = new Set(b);
for (g of a.values()) {
f.add(g);
}
return f;
}
}
}
return a;
}
return b;
}
return "undefined" === e ? c : a;
}
function u() {
return Object.create(null);
}
function v(a, c) {
return c.length - a.length;
}
;const w = /[^\p{L}\p{N}]+/u, x = /(\d{3})/g, y = /(\D)(\d{3})/g, z = /(\d{3})(\D)/g, B = "".normalize && /[\u0300-\u036f]/g;
function C(a) {
if (!this) {
return new C(...arguments);
}
for (let c = 0; c < arguments.length; c++) {
this.assign(arguments[c]);
}
}
C.prototype.assign = function(a) {
this.normalize = t(a.normalize, !0, this.normalize);
let c = a.include, b = c || a.exclude || a.split;
if ("object" === typeof b) {
let d = !c, e = "";
a.include || (e += "\\p{Z}");
b.letter && (e += "\\p{L}");
b.number && (e += "\\p{N}", d = !!c);
b.symbol && (e += "\\p{S}");
b.punctuation && (e += "\\p{P}");
b.control && (e += "\\p{C}");
if (b = b.char) {
e += "object" === typeof b ? b.join("") : b;
}
this.split = new RegExp("[" + (c ? "^" : "") + e + "]+", "u");
this.numeric = d;
} else {
this.split = t(b, w, this.split), this.numeric = t(this.numeric, !0);
}
this.prepare = t(a.prepare, null, this.prepare);
this.finalize = t(a.finalize, null, this.finalize);
this.rtl = a.rtl || !1;
this.dedupe = t(a.dedupe, !0, this.dedupe);
this.filter = t((b = a.filter) && new Set(b), null, this.filter);
this.matcher = t((b = a.matcher) && new Map(b), null, this.matcher);
this.mapper = t((b = a.mapper) && new Map(b), null, this.mapper);
this.stemmer = t((b = a.stemmer) && new Map(b), null, this.stemmer);
this.replacer = t(a.replacer, null, this.replacer);
this.minlength = t(a.minlength, 1, this.minlength);
this.maxlength = t(a.maxlength, 0, this.maxlength);
if (this.cache = b = t(a.cache, !0, this.cache)) {
this.m = null, this.B = "number" === typeof b ? b : 2e5, this.i = new Map(), this.j = new Map(), this.o = this.h = 128;
}
this.s = "";
this.v = null;
this.u = "";
this.A = null;
if (this.matcher) {
for (const d of this.matcher.keys()) {
this.s += (this.s ? "|" : "") + d;
}
}
if (this.stemmer) {
for (const d of this.stemmer.keys()) {
this.u += (this.u ? "|" : "") + d;
}
}
return this;
};
C.prototype.encode = function(a) {
if (this.cache && a.length <= this.h) {
if (this.m) {
if (this.i.has(a)) {
return this.i.get(a);
}
} else {
this.m = setTimeout(D, 0, this);
}
}
this.normalize && (a = "function" === typeof this.normalize ? this.normalize(a) : B ? a.normalize("NFKD").replace(B, "").toLowerCase() : a.toLowerCase());
this.prepare && (a = this.prepare(a));
this.numeric && 3 < a.length && (a = a.replace(y, "$1 $2").replace(z, "$1 $2").replace(x, "$1 "));
const c = !(this.dedupe || this.mapper || this.filter || this.matcher || this.stemmer || this.replacer);
let b = [], d = this.split || "" === this.split ? a.split(this.split) : a;
for (let g = 0, f, h; g < d.length; g++) {
if (!(f = h = d[g])) {
continue;
}
if (f.length < this.minlength) {
continue;
}
if (c) {
b.push(f);
continue;
}
if (this.filter && this.filter.has(f)) {
continue;
}
if (this.cache && f.length <= this.o) {
if (this.m) {
var e = this.j.get(f);
if (e || "" === e) {
e && b.push(e);
continue;
}
} else {
this.m = setTimeout(D, 0, this);
}
}
let k;
this.stemmer && 2 < f.length && (this.A || (this.A = new RegExp("(?!^)(" + this.u + ")$")), f = f.replace(this.A, p => this.stemmer.get(p)), k = 1);
this.matcher && 1 < f.length && (this.v || (this.v = new RegExp("(" + this.s + ")", "g")), f = f.replace(this.v, p => this.matcher.get(p)), k = 1);
f && k && (f.length < this.minlength || this.filter && this.filter.has(f)) && (f = "");
if (f && (this.mapper || this.dedupe && 1 < f.length)) {
e = "";
for (let p = 0, m = "", q, r; p < f.length; p++) {
q = f.charAt(p), q === m && this.dedupe || ((r = this.mapper && this.mapper.get(q)) || "" === r ? r === m && this.dedupe || !(m = r) || (e += r) : e += m = q);
}
f = e;
}
if (f && this.replacer) {
for (e = 0; f && e < this.replacer.length; e += 2) {
f = f.replace(this.replacer[e], this.replacer[e + 1]);
}
}
this.cache && h.length <= this.o && (this.j.set(h, f), this.j.size > this.B && (this.j.clear(), this.o = this.o / 1.1 | 0));
f && b.push(f);
}
this.finalize && (b = this.finalize(b) || b);
this.cache && a.length <= this.h && (this.i.set(a, b), this.i.size > this.B && (this.i.clear(), this.h = this.h / 1.1 | 0));
return b;
};
function D(a) {
a.m = null;
a.i.clear();
a.j.clear();
}
;function E(a) {
this.limit = a && !0 !== a ? a : 1000;
this.cache = new Map();
this.h = "";
}
E.prototype.set = function(a, c) {
this.cache.set(this.h = a, c);
this.cache.size > this.limit && this.cache.delete(this.cache.keys().next().value);
};
E.prototype.get = function(a) {
const c = this.cache.get(a);
c && this.h !== a && (this.cache.delete(a), this.cache.set(this.h = a, c));
return c;
};
E.prototype.remove = function(a) {
for (const c of this.cache) {
const b = c[0];
c[1].includes(a) && this.cache.delete(b);
}
};
E.prototype.clear = function() {
this.cache.clear();
this.h = "";
};
const F = {normalize:function(a) {
return a.toLowerCase();
}, dedupe:!1};
const G = {memory:{resolution:1}, performance:{resolution:6, fastupdate:!0, context:{depth:1, resolution:3}}, match:{tokenize:"forward"}, score:{resolution:9, context:{depth:2, resolution:9}}};
u();
H.prototype.add = function(a, c, b, d) {
if (c && (a || 0 === a)) {
if (!d && !b && this.g.has(a)) {
return this.update(a, c);
}
c = this.encoder.encode(c);
if (d = c.length) {
const p = u(), m = u(), q = this.depth, r = this.resolution;
for (let l = 0; l < d; l++) {
let n = c[this.rtl ? d - 1 - l : l];
var e = n.length;
if (e && (q || !m[n])) {
var g = this.score ? this.score(c, n, l, null, 0) : I(r, d, l), f = "";
switch(this.tokenize) {
case "full":
if (2 < e) {
for (g = 0; g < e; g++) {
for (var h = e; h > g; h--) {
f = n.substring(g, h);
var k = this.score ? this.score(c, n, l, f, g) : I(r, d, l, e, g);
J(this, m, f, k, a, b);
}
}
break;
}
case "reverse":
if (1 < e) {
for (h = e - 1; 0 < h; h--) {
f = n[h] + f, k = this.score ? this.score(c, n, l, f, h) : I(r, d, l, e, h), J(this, m, f, k, a, b);
}
f = "";
}
case "forward":
if (1 < e) {
for (h = 0; h < e; h++) {
f += n[h], J(this, m, f, g, a, b);
}
break;
}
default:
if (J(this, m, n, g, a, b), q && 1 < d && l < d - 1) {
for (e = u(), f = this.C, g = n, h = Math.min(q + 1, d - l), e[g] = 1, k = 1; k < h; k++) {
if ((n = c[this.rtl ? d - 1 - l - k : l + k]) && !e[n]) {
e[n] = 1;
const A = this.score ? this.score(c, g, l, n, k) : I(f + (d / 2 > f ? 0 : 1), d, l, h - 1, k - 1), M = this.bidirectional && n > g;
J(this, p, M ? g : n, A, a, b, M ? n : g);
}
}
}
}
}
}
this.fastupdate || this.g.add(a);
}
}
return this;
};
function J(a, c, b, d, e, g, f) {
let h = f ? a.l : a.map, k;
c[b] && f && (k = c[b])[f] || (f ? (c = k || (c[b] = u()), c[f] = 1, (k = h.get(f)) ? h = k : h.set(f, h = new Map())) : c[b] = 1, (k = h.get(b)) ? h = k : h.set(b, h = []), h = h[d] || (h[d] = []), g && h.includes(e) || (h.push(e), a.fastupdate && ((c = a.g.get(e)) ? c.push(h) : a.g.set(e, [h]))));
}
function I(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;
}
;function K(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;
}
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;
}
;H.prototype.search = function(a, c, b) {
b || (c || "object" !== typeof a ? "object" === typeof c && (b = c, c = 0) : (b = a, a = ""));
var d = [], e = 0;
if (b) {
a = b.query || a;
c = b.limit || c;
e = b.offset || 0;
var g = b.context;
var f = b.suggest;
}
a = this.encoder.encode(a);
b = a.length;
c || (c = 100);
if (1 === b) {
return L.call(this, a[0], "", c, e);
}
g = this.depth && !1 !== g;
if (2 === b && g && !f) {
return L.call(this, a[0], a[1], c, e);
}
var h = 0, k = 0;
if (1 < b) {
var p = u();
const q = [];
for (let r = 0, l; r < b; r++) {
if ((l = a[r]) && !p[l]) {
if (f || N(this, l)) {
q.push(l), p[l] = 1;
} else {
return d;
}
const n = l.length;
h = Math.max(h, n);
k = k ? Math.min(k, n) : n;
}
}
a = q;
b = a.length;
}
if (!b) {
return d;
}
p = 0;
if (1 === b) {
return L.call(this, a[0], "", c, e);
}
if (2 === b && g && !f) {
return L.call(this, a[0], a[1], c, e);
}
if (1 < b) {
if (g) {
var m = a[0];
p = 1;
} else {
9 < h && 3 < h / k && a.sort(v);
}
}
for (let q, r; p < b; p++) {
r = a[p];
m ? (q = N(this, r, m), q = O(q, d, f, this.C, c, e, 2 === b), f && !1 === q && d.length || (m = r)) : (q = N(this, r), q = O(q, d, f, this.resolution, c, e, 1 === b));
if (q) {
return q;
}
if (f && p === b - 1) {
g = d.length;
if (!g) {
if (m) {
m = "";
p = -1;
continue;
}
return d;
}
if (1 === g) {
return K(d[0], c, e);
}
}
}
a: {
a = d;
d = this.resolution;
m = f;
b = a.length;
f = [];
g = u();
for (let q = 0, r, l, n, A; q < d; q++) {
for (k = 0; k < b; k++) {
if (n = a[k], q < n.length && (r = n[q])) {
for (p = 0; p < r.length; p++) {
l = r[p], (h = g[l]) ? g[l]++ : (h = 0, g[l] = 1), A = f[h] || (f[h] = []), A.push(l);
}
}
}
}
if (a = f.length) {
if (m) {
d = [];
for (let q = a - 1, r = 0, l, n; 0 <= q; q--) {
if (l = f[q], n = l.length, e >= n) {
e -= n;
} else {
if (n + r > c || e) {
l = l.slice(e, c - r + e), n = l.length;
}
d.push(l);
r += n;
if (c === r) {
break;
}
}
}
if (1 < d.length) {
f = d;
a = [];
d = u();
g = f.length;
for (k = 0; k < g; k++) {
for (m = f[k], h = m.length, p = 0; p < h; p++) {
if (b = m[p], !d[b]) {
if (d[b] = 1, e) {
e--;
} else {
if (a.push(b), a.length === c) {
break;
}
}
}
}
}
c = a;
} else {
c = d[0];
}
f = c;
} else {
if (a < b) {
d = [];
break a;
}
f = f[a - 1];
if (f.length > c || e) {
f = f.slice(e, c + e);
}
}
}
d = f;
}
return d;
};
function L(a, c, b, d) {
return (a = N(this, a, c)) && a.length ? K(a, b, d) : [];
}
function O(a, c, b, d, e, g, f) {
let h = [];
if (a) {
d = Math.min(a.length, d);
for (let k = 0, p = 0, m; k < d; k++) {
if (m = a[k]) {
if (g && m && f && (m.length <= g ? (g -= m.length, m = null) : (m = m.slice(g), g = 0)), m && (h[k] = m, f && (p += m.length, p >= e))) {
break;
}
}
}
if (h.length) {
if (f) {
return K(h, e, 0);
}
c.push(h);
return;
}
}
return !b && h;
}
function N(a, c, b) {
let d;
b && (d = a.bidirectional && c > b);
a = b ? (a = a.l.get(d ? c : b)) && a.get(d ? b : c) : a.map.get(c);
return a;
}
;H.prototype.remove = function(a, c) {
const b = this.g.size && (this.fastupdate ? this.g.get(a) : this.g.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 {
P(this.map, a), this.depth && P(this.l, a);
}
c || this.g.delete(a);
}
this.cache && this.cache.remove(a);
return this;
};
function P(a, c) {
let b = 0;
if (a.constructor === Array) {
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 d of a) {
const e = d[0], g = P(d[1], c);
g ? b += g : a.delete(e);
}
}
return b;
}
;function H(a, c) {
if (!this) {
return new H(a);
}
if (a) {
var b = "string" === typeof a ? a : a.preset;
b && (G[b] || console.warn("Preset not found: " + b), a = Object.assign({}, G[b], a));
} else {
a = {};
}
b = a.context || {};
const d = a.encode || a.encoder || F;
this.encoder = d.encode ? d : "object" === typeof d ? new C(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;
e = !1;
this.map = new Map();
this.l = new Map();
this.g = c || (this.fastupdate ? new Map() : new Set());
this.C = b.resolution || 1;
this.rtl = d.rtl || a.rtl || !1;
this.cache = (e = a.cache || null) && new E(e);
}
H.prototype.clear = function() {
this.map.clear();
this.l.clear();
this.g.clear();
this.cache && this.cache.clear();
return this;
};
H.prototype.append = function(a, c) {
return this.add(a, c, !0);
};
H.prototype.contain = function(a) {
return this.g.has(a);
};
H.prototype.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 Q(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 = Q(b[1]);
e ? c += e : a.delete(d);
}
}
return c;
}
H.prototype.cleanup = function() {
if (!this.fastupdate) {
return console.info('Cleanup the index isn\'t required when not using "fastupdate".'), this;
}
Q(this.map);
this.depth && Q(this.l);
return this;
};
H.prototype.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.then) {
const e = this;
d.then(function(g) {
e.cache.set(a, g);
return g;
});
}
this.cache.set(a, d);
}
return d;
};
const R = {Index:H, Charset:null, Encoder:C, Document:null, Worker:null, Resolver:null, IndexedDB:null, Language:{}}, S = self;
let T;
(T = S.define) && T.amd ? T([], function() {
return R;
}) : "object" === typeof S.exports ? S.exports = R : S.FlexSearch = R;
}(this));