@devisfuture/mega-collection 2.4.6 → 2.4.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (62) hide show
  1. package/dist/State.d.ts +42 -0
  2. package/dist/{chunk-BJZg_1dI.mjs → State.mjs} +2 -2
  3. package/dist/constants.d.ts +5 -0
  4. package/dist/constants.mjs +4 -0
  5. package/dist/filter/chain.d.ts +9 -0
  6. package/dist/filter/chain.mjs +22 -0
  7. package/dist/filter/constants.d.ts +2 -0
  8. package/dist/filter/criterion.d.ts +6 -0
  9. package/dist/filter/criterion.mjs +64 -0
  10. package/dist/filter/errors.d.ts +7 -0
  11. package/dist/filter/errors.mjs +17 -0
  12. package/dist/filter/filter.d.ts +89 -0
  13. package/dist/filter/filter.mjs +439 -0
  14. package/dist/filter/index.mjs +2 -891
  15. package/dist/filter/nested.d.ts +32 -0
  16. package/dist/filter/nested.mjs +246 -0
  17. package/dist/filter/types.d.ts +74 -0
  18. package/dist/filter/utils.d.ts +4 -0
  19. package/dist/filter/utils.mjs +29 -0
  20. package/dist/index.mjs +1 -1
  21. package/dist/indexer.d.ts +41 -0
  22. package/dist/indexer.mjs +101 -0
  23. package/dist/internal.d.ts +6 -0
  24. package/dist/{chunk-C5Zpu_ol.mjs → internal.mjs} +1 -1
  25. package/dist/merge/chain.d.ts +9 -0
  26. package/dist/merge/chain.mjs +23 -0
  27. package/dist/merge/constants.d.ts +6 -0
  28. package/dist/merge/constants.mjs +8 -0
  29. package/dist/merge/errors.d.ts +8 -0
  30. package/dist/merge/errors.mjs +19 -0
  31. package/dist/merge/index.mjs +1 -1
  32. package/dist/merge/merge-engines.d.ts +75 -0
  33. package/dist/{chunk-BURK5sks.mjs → merge/merge-engines.mjs} +35 -108
  34. package/dist/merge/module-adapters.d.ts +7 -0
  35. package/dist/merge/module-adapters.mjs +41 -0
  36. package/dist/merge/types.d.ts +125 -0
  37. package/dist/search/constants.d.ts +5 -0
  38. package/dist/search/index.mjs +2 -1010
  39. package/dist/search/nested.d.ts +41 -0
  40. package/dist/search/nested.mjs +210 -0
  41. package/dist/search/ngram.d.ts +23 -0
  42. package/dist/search/ngram.mjs +96 -0
  43. package/dist/search/text-search.d.ts +146 -0
  44. package/dist/search/text-search.mjs +696 -0
  45. package/dist/search/types.d.ts +93 -0
  46. package/dist/search/utils.d.ts +4 -0
  47. package/dist/search/utils.mjs +22 -0
  48. package/dist/sort/errors.d.ts +5 -0
  49. package/dist/sort/errors.mjs +11 -0
  50. package/dist/sort/index.mjs +2 -416
  51. package/dist/sort/sorter.d.ts +47 -0
  52. package/dist/sort/sorter.mjs +375 -0
  53. package/dist/sort/types.d.ts +18 -0
  54. package/dist/sort/utils.d.ts +17 -0
  55. package/dist/sort/utils.mjs +38 -0
  56. package/dist/types.d.ts +73 -0
  57. package/package.json +11 -34
  58. /package/dist/filter/{index.d.mts → index.d.ts} +0 -0
  59. /package/dist/{index.d.mts → index.d.ts} +0 -0
  60. /package/dist/merge/{index.d.mts → index.d.ts} +0 -0
  61. /package/dist/search/{index.d.mts → index.d.ts} +0 -0
  62. /package/dist/sort/{index.d.mts → index.d.ts} +0 -0
@@ -1,1010 +1,2 @@
1
- import { a as e, i as t, n } from "../chunk-BJZg_1dI.mjs";
2
- import { n as r } from "../chunk-C5Zpu_ol.mjs";
3
- //#region src/search/ngram.ts
4
- function i(e) {
5
- for (let t = 0; t < e.length - 1; t++) {
6
- let n = t;
7
- for (let r = t + 1; r < e.length; r++) e[r].size < e[n].size && (n = r);
8
- if (n !== t) {
9
- let r = e[t];
10
- e[t] = e[n], e[n] = r;
11
- }
12
- }
13
- }
14
- function a(e, t) {
15
- let n = [];
16
- for (let r of t) {
17
- let t = e.get(r);
18
- if (!t) return null;
19
- n.push(t);
20
- }
21
- return i(n), n;
22
- }
23
- function o(e, t) {
24
- for (let n = 0; n < t.length; n++) if (!t[n].has(e)) return !1;
25
- return !0;
26
- }
27
- function s(e, t, n, r) {
28
- let i = a(e, t);
29
- if (i === null) return null;
30
- let s = i[0];
31
- return {
32
- smallestPostingList: s,
33
- matches: (e) => s.has(e) && o(e, i) && !!n[e]?.includes(r)
34
- };
35
- }
36
- function c(e) {
37
- let t = Math.min(3, Math.max(2, e.length)), n = e.length - t + 1, r = Array(n);
38
- for (let i = 0; i < n; i++) r[i] = e.substring(i, i + t);
39
- return r;
40
- }
41
- function l(e, t) {
42
- let n = e.get(t);
43
- if (n) return n;
44
- let r = /* @__PURE__ */ new Set();
45
- return e.set(t, r), r;
46
- }
47
- function u(e) {
48
- let t = c(e);
49
- if (t.length <= 12) return new Set(t);
50
- let n = /* @__PURE__ */ new Set(), r = t.length - 1;
51
- for (let e = 0; e <= 11; e++) {
52
- let i = Math.round(e * r / 11);
53
- n.add(t[i]);
54
- }
55
- return n;
56
- }
57
- function d(e, t, n) {
58
- let r = Math.min(3, t.length);
59
- for (let i = 2; i <= r; i++) {
60
- let r = t.length - i;
61
- for (let a = 0; a <= r; a++) l(e, t.substring(a, a + i)).add(n);
62
- }
63
- }
64
- function f(e, t, n, r, i = {}) {
65
- let { restrictionLookup: a = null, take: o = Infinity } = i, c = s(e, t, n, r);
66
- if (c === null) return [];
67
- let l = [];
68
- for (let e of c.smallestPostingList) if (!(a !== null && !a[e]) && c.matches(e) && (l.push(e), l.length >= o)) break;
69
- return l;
70
- }
71
- function p(e, t, n, r, i) {
72
- let { candidateIndices: a, restrictionLookup: o = null, take: c = Infinity } = i;
73
- if (a.length === 0) return [];
74
- let l = s(e, t, n, r);
75
- if (l === null) return [];
76
- let u = [];
77
- if (o === null || a.length <= l.smallestPostingList.size) {
78
- for (let e = 0; e < a.length; e++) {
79
- let t = a[e];
80
- if (l.matches(t) && (u.push(t), u.length >= c)) break;
81
- }
82
- return u;
83
- }
84
- for (let e of l.smallestPostingList) if (o[e] && l.matches(e) && (u.push(e), u.length >= c)) break;
85
- return u;
86
- }
87
- function m(e, t, n) {
88
- let r = Math.min(3, t.length);
89
- for (let i = 2; i <= r; i++) {
90
- let r = t.length - i;
91
- for (let a = 0; a <= r; a++) {
92
- let r = t.substring(a, a + i), o = e.get(r);
93
- o && (o.delete(n), o.size === 0 && e.delete(r));
94
- }
95
- }
96
- }
97
- //#endregion
98
- //#region src/search/nested.ts
99
- var h = class {
100
- constructor(e = {
101
- ngramIndexes: /* @__PURE__ */ new Map(),
102
- normalizedFieldValues: /* @__PURE__ */ new Map()
103
- }) {
104
- this.storage = e, this.registeredFields = /* @__PURE__ */ new Set(), this.fieldDescriptors = /* @__PURE__ */ new Map();
105
- }
106
- registerFields(e) {
107
- if (e?.length) for (let t of e) this.registerField(t);
108
- }
109
- hasRegisteredFields() {
110
- return this.registeredFields.size > 0;
111
- }
112
- hasField(e) {
113
- return this.registeredFields.has(e);
114
- }
115
- hasIndexes() {
116
- return this.storage.ngramIndexes.size > 0;
117
- }
118
- clearIndexes() {
119
- this.storage.ngramIndexes.clear(), this.storage.normalizedFieldValues.clear();
120
- }
121
- buildIndexes(e) {
122
- this.clearIndexes();
123
- for (let t of this.registeredFields) this.buildIndex(e, t);
124
- }
125
- addItems(e, t) {
126
- if (!(e.length === 0 || this.storage.ngramIndexes.size === 0)) for (let n of this.storage.ngramIndexes.keys()) this.addItemsToField(n, e, t);
127
- }
128
- updateItem(e, t, n) {
129
- if (this.storage.ngramIndexes.size !== 0) for (let r of this.storage.ngramIndexes.keys()) this.updateItemInField(r, e, t, n);
130
- }
131
- removeItem(e, t) {
132
- if (this.storage.ngramIndexes.size !== 0) for (let n of this.storage.ngramIndexes.keys()) this.removeItemFromField(n, e, t);
133
- }
134
- moveItem(e, t, n) {
135
- if (!(this.storage.ngramIndexes.size === 0 || t === n)) for (let r of this.storage.ngramIndexes.keys()) this.moveItemForField(r, e, t, n);
136
- }
137
- searchAllIndexedFieldIndices(e, t, n, r) {
138
- let i = /* @__PURE__ */ new Set(), a = [];
139
- for (let o of this.storage.ngramIndexes.keys()) for (let s of this.searchIndexedFieldIndices(o, e, t, n, r)) i.has(s) || (i.add(s), a.push(s));
140
- return a;
141
- }
142
- searchIndexedField(e, t, n, r, i, a, o = Infinity) {
143
- let s = this.searchIndexedFieldIndices(t, n, r, i, a, o), c = [];
144
- for (let t = 0; t < s.length; t++) {
145
- let n = e[s[t]];
146
- n && c.push(n);
147
- }
148
- return c;
149
- }
150
- searchIndexedFieldIndices(e, t, n, r, i, a = Infinity) {
151
- let o = this.storage.ngramIndexes.get(e);
152
- if (!o) return [];
153
- let s = this.storage.normalizedFieldValues.get(e) ?? [];
154
- return i == null ? f(o, n, s, t, {
155
- restrictionLookup: r,
156
- take: a
157
- }) : p(o, n, s, t, {
158
- candidateIndices: i,
159
- restrictionLookup: r,
160
- take: a
161
- });
162
- }
163
- searchFieldLinear(e, t, n) {
164
- let r = this.searchFieldLinearIndices(e, t, n), i = [];
165
- for (let t = 0; t < r.length; t++) {
166
- let n = e[r[t]];
167
- n && i.push(n);
168
- }
169
- return i;
170
- }
171
- searchFieldLinearIndices(e, t, n, r) {
172
- let i = this.fieldDescriptors.get(t);
173
- if (!i) return [];
174
- let { collectionKey: a, nestedKey: o } = i, s = [];
175
- if (r) {
176
- for (let t = 0; t < r.length; t++) {
177
- let i = r[t], c = e[i][a];
178
- if (!Array.isArray(c)) continue;
179
- let l = !1;
180
- for (let e = 0; e < c.length; e++) {
181
- let t = c[e][o];
182
- if (typeof t == "string" && t.toLowerCase().includes(n)) {
183
- l = !0;
184
- break;
185
- }
186
- }
187
- l && s.push(i);
188
- }
189
- return s;
190
- }
191
- for (let t = 0; t < e.length; t++) {
192
- let r = e[t][a];
193
- if (!Array.isArray(r)) continue;
194
- let i = !1;
195
- for (let e = 0; e < r.length; e++) {
196
- let t = r[e][o];
197
- if (typeof t == "string" && t.toLowerCase().includes(n)) {
198
- i = !0;
199
- break;
200
- }
201
- }
202
- i && s.push(t);
203
- }
204
- return s;
205
- }
206
- matchesAnyField(e, t) {
207
- for (let n of this.registeredFields) {
208
- let r = this.fieldDescriptors.get(n);
209
- if (!r) continue;
210
- let { collectionKey: i, nestedKey: a } = r, o = e[i];
211
- if (Array.isArray(o)) for (let e = 0; e < o.length; e++) {
212
- let n = o[e][a];
213
- if (typeof n == "string" && n.toLowerCase().includes(t)) return !0;
214
- }
215
- }
216
- return !1;
217
- }
218
- registerField(e) {
219
- let t = r(e);
220
- t && (this.registeredFields.add(e), this.fieldDescriptors.set(e, t));
221
- }
222
- getNormalizedValues(e) {
223
- let t = this.storage.normalizedFieldValues.get(e);
224
- if (t) return t;
225
- let n = [];
226
- return this.storage.normalizedFieldValues.set(e, n), n;
227
- }
228
- buildIndex(e, t) {
229
- let n = this.fieldDescriptors.get(t);
230
- if (!n) return;
231
- let { collectionKey: r, nestedKey: i } = n, a = /* @__PURE__ */ new Map(), o = Array(e.length);
232
- for (let t = 0, n = e.length; t < n; t++) {
233
- let n = e[t][r];
234
- if (!Array.isArray(n)) continue;
235
- let s = [];
236
- for (let e = 0; e < n.length; e++) {
237
- let t = n[e][i];
238
- typeof t == "string" && s.push(t.toLowerCase());
239
- }
240
- if (s.length === 0) continue;
241
- let c = s.join("\n");
242
- o[t] = c, d(a, c, t);
243
- }
244
- this.storage.ngramIndexes.set(t, a), this.storage.normalizedFieldValues.set(t, o);
245
- }
246
- addItemsToField(e, t, n) {
247
- let r = this.fieldDescriptors.get(e), i = this.storage.ngramIndexes.get(e);
248
- if (!r || !i) return;
249
- let a = this.getNormalizedValues(e), { collectionKey: o, nestedKey: s } = r;
250
- for (let e = 0; e < t.length; e++) {
251
- let r = t[e][o];
252
- if (!Array.isArray(r)) continue;
253
- let c = [], l = n + e;
254
- for (let e = 0; e < r.length; e++) {
255
- let t = r[e][s];
256
- typeof t == "string" && c.push(t.toLowerCase());
257
- }
258
- if (c.length === 0) continue;
259
- let u = c.join("\n");
260
- a[l] = u, d(i, u, l);
261
- }
262
- }
263
- updateItemInField(e, t, n, r) {
264
- let i = this.storage.ngramIndexes.get(e);
265
- if (!i) return;
266
- let a = this.getNormalizedValues(e), o = this.getNormalizedItemValue(e, n), s = this.getNormalizedItemValue(e, t);
267
- if (o !== s) {
268
- if (o && m(i, o, r), !s) {
269
- delete a[r];
270
- return;
271
- }
272
- a[r] = s, d(i, s, r);
273
- }
274
- }
275
- removeItemFromField(e, t, n) {
276
- let r = this.storage.ngramIndexes.get(e);
277
- if (!r) return;
278
- let i = this.getNormalizedValues(e), a = i[n] ?? this.getNormalizedItemValue(e, t);
279
- a && m(r, a, n), delete i[n];
280
- }
281
- moveItemForField(e, t, n, r) {
282
- let i = this.storage.ngramIndexes.get(e);
283
- if (!i) return;
284
- let a = this.getNormalizedValues(e), o = a[n] ?? this.getNormalizedItemValue(e, t);
285
- if (!o) {
286
- delete a[n];
287
- return;
288
- }
289
- m(i, o, n), d(i, o, r), a[r] = o, delete a[n];
290
- }
291
- getNormalizedItemValue(e, t) {
292
- let n = this.fieldDescriptors.get(e);
293
- if (!n) return null;
294
- let { collectionKey: r, nestedKey: i } = n, a = t[r];
295
- if (!Array.isArray(a)) return null;
296
- let o = [];
297
- for (let e = 0; e < a.length; e++) {
298
- let t = a[e][i];
299
- typeof t == "string" && o.push(t.toLowerCase());
300
- }
301
- return o.length === 0 ? null : o.join("\n");
302
- }
303
- }, g = () => ({
304
- indexedFields: /* @__PURE__ */ new Set(),
305
- flatIndexes: /* @__PURE__ */ new Map(),
306
- nestedStorage: {
307
- ngramIndexes: /* @__PURE__ */ new Map(),
308
- normalizedFieldValues: /* @__PURE__ */ new Map()
309
- },
310
- deferredMutationVersion: null,
311
- filterByPreviousResult: !1,
312
- previousResultIndices: null,
313
- previousResultLookup: null,
314
- previousQuery: null,
315
- stats: {
316
- totalQueries: 0,
317
- indexedQueries: 0,
318
- fallbackQueries: 0,
319
- fallbackFields: /* @__PURE__ */ new Map()
320
- }
321
- }), _ = class {
322
- constructor(t = {}) {
323
- this.cachedIndexedFieldsList = null, this.cachedLinearSearchFieldsList = null, this.emittedWarningKeys = /* @__PURE__ */ new Set(), this.warnings = [], this.normalizedValuesCache = /* @__PURE__ */ new Map(), this.combinedNormalizedValuesCache = null, this.minQueryLength = t.minQueryLength ?? 1, this.silent = t.silent ?? !1, this.state = t.state ?? new e(t.data ?? []), this.namespace = this.state.createNamespace("search"), this.nestedCollection = new h(this.runtime.nestedStorage), this.nestedCollection.registerFields(t.nestedFields), this.state.subscribe((e) => this.handleStateMutation(e)), t.filterByPreviousResult && (this.runtime.filterByPreviousResult = !0);
324
- let n = t.fields?.length, r = this.nestedCollection.hasRegisteredFields();
325
- if (n) for (let e of t.fields) this.indexedFields.add(e);
326
- this.dataset.length > 0 && (n || r) && this.rebuildConfiguredIndexes();
327
- }
328
- get dataset() {
329
- return this.state.getOriginData();
330
- }
331
- get runtime() {
332
- return this.state.getOrCreateScopedValue(this.namespace, "runtime", g);
333
- }
334
- get flatIndexes() {
335
- return this.runtime.flatIndexes;
336
- }
337
- get indexedFields() {
338
- return this.runtime.indexedFields;
339
- }
340
- shouldDeferMutationIndexUpdates() {
341
- return this.state.getScopedValue(t, n) === !0;
342
- }
343
- markDeferredMutationState() {
344
- this.runtime.deferredMutationVersion = this.state.getMutationVersion(), this.flatIndexes.clear(), this.nestedCollection.clearIndexes(), this.cachedIndexedFieldsList = null, this.cachedLinearSearchFieldsList = null, this.normalizedValuesCache.clear(), this.combinedNormalizedValuesCache = null, this.clearPreviousSearchState();
345
- }
346
- ensureConfiguredIndexesReady() {
347
- this.runtime.deferredMutationVersion !== null && (this.runtime.deferredMutationVersion = null, this.dataset.length > 0 && (this.indexedFields.size > 0 || this.nestedCollection.hasRegisteredFields()) && this.rebuildConfiguredIndexes());
348
- }
349
- rebuildConfiguredIndexes() {
350
- this.flatIndexes.clear(), this.nestedCollection.clearIndexes(), this.cachedIndexedFieldsList = null, this.cachedLinearSearchFieldsList = null, this.normalizedValuesCache.clear(), this.combinedNormalizedValuesCache = null;
351
- for (let e of this.indexedFields) this.buildIndexFromData(this.dataset, e);
352
- this.nestedCollection.hasRegisteredFields() && this.nestedCollection.buildIndexes(this.dataset);
353
- }
354
- buildIndexFromData(e, t) {
355
- let n = this.state.getMutationVersion(), r = /* @__PURE__ */ new Map(), i = Array(e.length);
356
- for (let n = 0, a = e.length; n < a; n++) {
357
- let a = e[n][t];
358
- if (typeof a != "string") continue;
359
- let o = a.toLowerCase();
360
- i[n] = o, d(r, o, n);
361
- }
362
- this.flatIndexes.set(t, {
363
- ngramMap: r,
364
- normalizedValues: i,
365
- version: n
366
- });
367
- }
368
- search(e, t, n) {
369
- return typeof t == "string" ? this.searchField(e, t, n) : this.searchAll(e, t);
370
- }
371
- searchAll(e, t) {
372
- return this.searchAllFields(e, t);
373
- }
374
- normalizeQuery(e) {
375
- return e.trim().toLowerCase();
376
- }
377
- normalizeSearchWindow(e) {
378
- let t = Math.max(0, Math.trunc(e?.offset ?? 0)), n = e?.limit, r = n === void 0 ? Infinity : Math.max(0, Math.trunc(n));
379
- return {
380
- offset: t,
381
- limit: r,
382
- take: Number.isFinite(r) ? t + r : Infinity,
383
- hasWindow: t > 0 || Number.isFinite(r)
384
- };
385
- }
386
- shouldTrackPreviousResult(e) {
387
- return !e.hasWindow;
388
- }
389
- hasReachedWindowLimit(e, t) {
390
- return Number.isFinite(e.limit) && t >= e.limit;
391
- }
392
- sliceItems(e, t) {
393
- return t.hasWindow ? e.slice(t.offset, t.take) : e;
394
- }
395
- collectItemsFromIndices(e, t) {
396
- let n = t.hasWindow && e.length > 0 ? e.slice(t.offset, t.take) : e, r = [];
397
- for (let e = 0; e < n.length; e++) {
398
- let t = this.dataset[n[e]];
399
- t && r.push(t);
400
- }
401
- return {
402
- items: r,
403
- indices: n
404
- };
405
- }
406
- createLookup(e) {
407
- let t = new Uint8Array(this.dataset.length);
408
- for (let n = 0; n < e.length; n++) t[e[n]] = 1;
409
- return t;
410
- }
411
- getRestrictionLookup(e) {
412
- let t = this.runtime.previousResultLookup;
413
- if (t !== null && this.runtime.previousResultIndices === e && t.length === this.dataset.length) return t;
414
- let n = this.createLookup(e);
415
- return this.runtime.previousResultLookup = n, n;
416
- }
417
- getSearchSource(e) {
418
- let { runtime: t } = this;
419
- return t.filterByPreviousResult ? t.previousQuery !== null && t.previousResultIndices !== null && e.includes(t.previousQuery) ? {
420
- indices: t.previousResultIndices,
421
- lookup: this.getRestrictionLookup(t.previousResultIndices)
422
- } : (t.previousResultIndices = null, t.previousResultLookup = null, t.previousQuery = null, {
423
- indices: null,
424
- lookup: null
425
- }) : {
426
- indices: null,
427
- lookup: null
428
- };
429
- }
430
- saveSearchResult(e, t) {
431
- let { runtime: n } = this;
432
- n.filterByPreviousResult && (n.previousResultIndices = e, n.previousResultLookup = null, n.previousQuery = t);
433
- }
434
- persistSearchResult(e, t, n) {
435
- n && this.saveSearchResult(e.indices, t);
436
- }
437
- resetSearchState() {
438
- return this.clearPreviousSearchState(), this;
439
- }
440
- getWarnings() {
441
- return [...this.warnings];
442
- }
443
- getStats() {
444
- let { totalQueries: e, indexedQueries: t, fallbackQueries: n, fallbackFields: r } = this.runtime.stats;
445
- return {
446
- totalQueries: e,
447
- indexedQueries: t,
448
- fallbackQueries: n,
449
- fallbackRate: e === 0 ? 0 : n / e,
450
- fallbackFields: Object.fromEntries(r)
451
- };
452
- }
453
- resetStats() {
454
- let { stats: e } = this.runtime;
455
- return e.totalQueries = 0, e.indexedQueries = 0, e.fallbackQueries = 0, e.fallbackFields.clear(), this;
456
- }
457
- clearPreviousSearchState() {
458
- let { runtime: e } = this;
459
- e.previousResultIndices = null, e.previousResultLookup = null, e.previousQuery = null;
460
- }
461
- recordIndexedQuery() {
462
- let { stats: e } = this.runtime;
463
- e.totalQueries += 1, e.indexedQueries += 1;
464
- }
465
- recordFallbackQuery(e) {
466
- let { stats: t } = this.runtime;
467
- t.totalQueries += 1, t.fallbackQueries += 1, t.fallbackFields.set(e, (t.fallbackFields.get(e) ?? 0) + 1);
468
- }
469
- shouldEmitFallbackWarning(e) {
470
- return !this.silent && e.length >= 2;
471
- }
472
- warnAboutFallback(e, t, n) {
473
- if (!this.shouldEmitFallbackWarning(t)) return;
474
- let r = `${e}\u0000${t}\u0000${n}`;
475
- if (this.emittedWarningKeys.has(r)) return;
476
- this.emittedWarningKeys.add(r);
477
- let i = `[TextSearchEngine] warn: query "${t}" on ${e} used linear fallback. ${n}. Add the field(s) to the index schema to enable indexed search.`;
478
- this.warnings.push(i), typeof process < "u" && process.env.NODE_ENV !== "production" && process.env.NODE_ENV !== "test" && console.warn(i);
479
- }
480
- getResolvedFlatIndex(e) {
481
- let t = this.state.getMutationVersion(), n = this.flatIndexes.get(e);
482
- return n && n.version !== t && this.dataset.length > 0 && (this.buildIndexFromData(this.dataset, e), n = this.flatIndexes.get(e)), n;
483
- }
484
- getQueryGrams(e) {
485
- let t = u(e);
486
- return t.size > 0 ? t : null;
487
- }
488
- searchAllFields(e, t) {
489
- let n = this.normalizeQuery(e), r = this.normalizeSearchWindow(t), i = "all fields";
490
- if (r.limit === 0) return [];
491
- if (!n || n.length < this.minQueryLength) return this.sliceItems(this.dataset, r);
492
- n.length >= 2 && this.ensureConfiguredIndexesReady();
493
- let a = this.shouldTrackPreviousResult(r), { indices: o, lookup: s } = this.getSearchSource(n), c, l = () => (c === void 0 && (c = n.length >= 2 ? this.getQueryGrams(n) : null), c);
494
- if (o !== null) {
495
- let e = l();
496
- if (e !== null && (this.flatIndexes.size > 0 || this.nestedCollection.hasIndexes())) {
497
- this.recordIndexedQuery();
498
- let t = this.searchAllFieldsIndexed(n, e, r, s, o);
499
- return this.persistSearchResult(t, n, a), t.items;
500
- }
501
- this.recordFallbackQuery(i);
502
- let t = this.searchLinearAllFields(this.dataset, n, o, r);
503
- return this.persistSearchResult(t, n, a), t.items;
504
- }
505
- if (!this.flatIndexes.size && !this.nestedCollection.hasIndexes()) {
506
- this.warnAboutFallback(i, n, this.indexedFields.size > 0 ? "configured indexes are not currently built" : "no indexed fields are configured"), this.recordFallbackQuery(i);
507
- let e = this.searchLinearAllFields(this.dataset, n, null, r);
508
- return this.persistSearchResult(e, n, a), e.items;
509
- }
510
- let u = l();
511
- if (u === null) {
512
- this.recordFallbackQuery(i);
513
- let e = this.searchLinearAllFields(this.dataset, n, null, r);
514
- return this.persistSearchResult(e, n, a), e.items;
515
- }
516
- this.recordIndexedQuery();
517
- let d = this.searchAllFieldsIndexed(n, u, r, null);
518
- return this.persistSearchResult(d, n, a), d.items;
519
- }
520
- searchAllFieldsIndexed(e, t, n, r, i = null) {
521
- if (i !== null && !this.nestedCollection.hasRegisteredFields() && this.flatIndexes.size > 0) return this.searchAllFieldsIndexedInCandidates(e, t, n, r, i);
522
- let a = this.dataset, o = new Uint8Array(a.length), s = [], c = 0;
523
- for (let a of this.flatIndexes.keys()) {
524
- let l = this.searchFieldWithPreparedQueryIndices(a, e, t, r, i);
525
- for (let e = 0; e < l.length; e++) {
526
- let t = l[e];
527
- if (!o[t]) {
528
- if (o[t] = 1, c < n.offset) {
529
- c += 1;
530
- continue;
531
- }
532
- if (s.push(t), c += 1, this.hasReachedWindowLimit(n, s.length)) return this.materializeIndicesResult(s);
533
- }
534
- }
535
- }
536
- for (let a of this.nestedCollection.searchAllIndexedFieldIndices(e, t, r, i)) if (!o[a]) {
537
- if (o[a] = 1, c < n.offset) {
538
- c += 1;
539
- continue;
540
- }
541
- if (s.push(a), c += 1, this.hasReachedWindowLimit(n, s.length)) return this.materializeIndicesResult(s);
542
- }
543
- return this.materializeIndicesResult(s);
544
- }
545
- searchAllFieldsIndexedInCandidates(e, t, n, r, i) {
546
- let a = [];
547
- for (let n of this.flatIndexes.keys()) {
548
- let r = this.getResolvedFlatIndex(n);
549
- if (!r) continue;
550
- let i = s(r.ngramMap, t, r.normalizedValues, e);
551
- i !== null && a.push(i.matches);
552
- }
553
- if (a.length === 0) return {
554
- items: [],
555
- indices: []
556
- };
557
- let o = [], c = 0;
558
- for (let e = 0; e < i.length; e++) {
559
- let t = i[e];
560
- if (r !== null && !r[t]) continue;
561
- let s = !1;
562
- for (let e = 0; e < a.length; e++) if (a[e](t)) {
563
- s = !0;
564
- break;
565
- }
566
- if (s) {
567
- if (c < n.offset) {
568
- c += 1;
569
- continue;
570
- }
571
- if (o.push(t), c += 1, this.hasReachedWindowLimit(n, o.length)) break;
572
- }
573
- }
574
- return this.materializeIndicesResult(o);
575
- }
576
- searchField(e, t, n) {
577
- let r = this.normalizeQuery(t), i = this.normalizeSearchWindow(n), a = `field "${e}"`;
578
- if (i.limit === 0) return [];
579
- if (!r || r.length < this.minQueryLength) return this.sliceItems(this.dataset, i);
580
- r.length >= 2 && this.ensureConfiguredIndexesReady();
581
- let o = this.shouldTrackPreviousResult(i), { indices: s, lookup: c } = this.getSearchSource(r), l = this.nestedCollection.hasField(e), u, d = () => (u === void 0 && (u = r.length >= 2 ? this.getQueryGrams(r) : null), u);
582
- if (s !== null) {
583
- let t = d();
584
- if (l) {
585
- if (t !== null && this.nestedCollection.hasIndexes()) {
586
- this.recordIndexedQuery();
587
- let n = this.nestedCollection.searchIndexedFieldIndices(e, r, t, c, s, i.take), a = this.collectItemsFromIndices(n, i);
588
- return this.persistSearchResult(a, r, o), a.items;
589
- }
590
- this.recordFallbackQuery(a);
591
- let n = this.searchLinearSingleField(this.dataset, e, r, s, i);
592
- return this.persistSearchResult(n, r, o), n.items;
593
- }
594
- if (t !== null && this.flatIndexes.has(e)) {
595
- this.recordIndexedQuery();
596
- let n = this.searchFieldWithPreparedQuery(e, r, t, i, c, s);
597
- return this.persistSearchResult(n, r, o), n.items;
598
- }
599
- this.recordFallbackQuery(a);
600
- let n = this.searchLinearSingleField(this.dataset, e, r, s, i);
601
- return this.persistSearchResult(n, r, o), n.items;
602
- }
603
- if (l) {
604
- if (r.length >= 2 && this.nestedCollection.hasIndexes()) {
605
- let t = this.getQueryGrams(r);
606
- if (t === null) return [];
607
- this.recordIndexedQuery();
608
- let n = this.nestedCollection.searchIndexedFieldIndices(e, r, t, null, null, i.take), a = this.collectItemsFromIndices(n, i);
609
- return this.persistSearchResult(a, r, o), a.items;
610
- }
611
- this.recordFallbackQuery(a);
612
- let t = this.searchLinearSingleField(this.dataset, e, r, null, i);
613
- return this.persistSearchResult(t, r, o), t.items;
614
- }
615
- this.flatIndexes.size || this.warnAboutFallback(a, r, this.indexedFields.size > 0 ? `field "${e}" is not backed by an active index` : "no indexed fields are configured");
616
- let f = d();
617
- if (this.flatIndexes.size > 0 && f !== null) {
618
- if (!this.flatIndexes.has(e)) return [];
619
- this.recordIndexedQuery();
620
- let t = this.searchFieldWithPreparedQuery(e, r, f, i);
621
- return this.persistSearchResult(t, r, o), t.items;
622
- }
623
- this.recordFallbackQuery(a);
624
- let p = this.searchLinearSingleField(this.dataset, e, r, null, i);
625
- return this.persistSearchResult(p, r, o), p.items;
626
- }
627
- searchFieldWithPreparedQuery(e, t, n, r, i = null, a = null) {
628
- let o = this.searchFieldWithPreparedQueryIndices(e, t, n, i, a, r.take);
629
- return this.collectItemsFromIndices(o, r);
630
- }
631
- searchFieldWithPreparedQueryIndices(e, t, n, r = null, i = null, a = Infinity) {
632
- let o = this.getResolvedFlatIndex(e);
633
- if (!o) return [];
634
- let { ngramMap: s, normalizedValues: c } = o;
635
- return i === null ? f(s, n, c, t, {
636
- restrictionLookup: r,
637
- take: a
638
- }) : p(s, n, c, t, {
639
- candidateIndices: i,
640
- restrictionLookup: r,
641
- take: a
642
- });
643
- }
644
- getIndexedFieldsList() {
645
- return this.cachedIndexedFieldsList === null && (this.cachedIndexedFieldsList = Array.from(this.indexedFields)), this.cachedIndexedFieldsList;
646
- }
647
- materializeIndicesResult(e) {
648
- let t = [];
649
- for (let n = 0; n < e.length; n++) {
650
- let r = this.dataset[e[n]];
651
- r && t.push(r);
652
- }
653
- return {
654
- items: t,
655
- indices: e
656
- };
657
- }
658
- getLinearSearchFields(e) {
659
- let t = this.getIndexedFieldsList();
660
- return t.length > 0 ? t : (this.cachedLinearSearchFieldsList === null && (this.cachedLinearSearchFieldsList = e.length ? Object.keys(e[0]).filter((t) => typeof e[0][t] == "string") : []), this.cachedLinearSearchFieldsList);
661
- }
662
- buildNormalizedValuesOnly(e, t) {
663
- let n = Array(e.length);
664
- for (let r = 0, i = e.length; r < i; r++) {
665
- let i = e[r][t];
666
- typeof i == "string" && (n[r] = i.toLowerCase());
667
- }
668
- return this.normalizedValuesCache.set(t, n), n;
669
- }
670
- buildCombinedNormalizedValues(e, t) {
671
- let n = Array(e.length);
672
- for (let r = 0; r < e.length; r++) n[r] = this.buildCombinedNormalizedValue(e[r], t);
673
- return this.combinedNormalizedValuesCache = {
674
- fieldsKey: t.join("\0"),
675
- values: n
676
- }, n;
677
- }
678
- buildCombinedNormalizedValue(e, t) {
679
- let n = "";
680
- for (let r = 0; r < t.length; r++) {
681
- let i = e[t[r]];
682
- typeof i == "string" && (n && (n += "\n"), n += i.toLowerCase());
683
- }
684
- return n;
685
- }
686
- getCombinedNormalizedValues(e, t) {
687
- if (e !== this.dataset) return null;
688
- let n = t.join("\0");
689
- return this.combinedNormalizedValuesCache?.fieldsKey === n ? this.combinedNormalizedValuesCache.values : this.buildCombinedNormalizedValues(e, t);
690
- }
691
- invalidateNormalizedValuesCacheEntry(e, t) {
692
- for (let [n, r] of this.normalizedValuesCache) {
693
- let i = t[n];
694
- r[e] = typeof i == "string" ? i.toLowerCase() : "";
695
- }
696
- if (this.combinedNormalizedValuesCache !== null) {
697
- let n = this.combinedNormalizedValuesCache.fieldsKey.split("\0");
698
- this.combinedNormalizedValuesCache.values[e] = this.buildCombinedNormalizedValue(t, n);
699
- }
700
- }
701
- searchLinearAllFields(e, t, n, r) {
702
- if (!e.length) return {
703
- items: [],
704
- indices: []
705
- };
706
- let i = this.getLinearSearchFields(e), a = e === this.dataset, o = i.length, s = Array(o);
707
- for (let t = 0; t < o; t++) {
708
- let n = i[t], r = this.flatIndexes.get(n);
709
- if (r) s[t] = r.normalizedValues;
710
- else {
711
- let r = this.normalizedValuesCache.get(n);
712
- r ? s[t] = r : a ? s[t] = this.buildNormalizedValuesOnly(e, n) : s[t] = null;
713
- }
714
- }
715
- let c = this.nestedCollection.hasRegisteredFields(), l = c ? null : this.getCombinedNormalizedValues(e, i), u = !c && s.every((e) => e !== null), d = [], f = 0;
716
- if (n !== null) {
717
- let e = this.dataset;
718
- if (l !== null) {
719
- for (let e = 0; e < n.length; e++) {
720
- let i = n[e];
721
- if (l[i]?.includes(t)) {
722
- if (f < r.offset) {
723
- f += 1;
724
- continue;
725
- }
726
- if (d.push(i), f += 1, this.hasReachedWindowLimit(r, d.length)) break;
727
- }
728
- }
729
- return this.materializeIndicesResult(d);
730
- }
731
- if (u) {
732
- for (let e = 0; e < n.length; e++) {
733
- let i = n[e], a = !1;
734
- for (let e = 0; e < o; e++) {
735
- let n = s[e][i];
736
- if (n && n.includes(t)) {
737
- a = !0;
738
- break;
739
- }
740
- }
741
- if (a) {
742
- if (f < r.offset) {
743
- f += 1;
744
- continue;
745
- }
746
- if (d.push(i), f += 1, this.hasReachedWindowLimit(r, d.length)) break;
747
- }
748
- }
749
- return this.materializeIndicesResult(d);
750
- }
751
- for (let a = 0; a < n.length; a++) {
752
- let l = n[a], u = e[l], p = !1;
753
- for (let e = 0; e < o; e++) {
754
- let n = s[e];
755
- if (n) {
756
- let e = n[l];
757
- if (e && e.includes(t)) {
758
- p = !0;
759
- break;
760
- }
761
- } else {
762
- let n = u[i[e]];
763
- if (typeof n == "string" && n.toLowerCase().includes(t)) {
764
- p = !0;
765
- break;
766
- }
767
- }
768
- }
769
- if (!p && c && (p = this.nestedCollection.matchesAnyField(u, t)), p) {
770
- if (f < r.offset) {
771
- f += 1;
772
- continue;
773
- }
774
- if (d.push(l), f += 1, this.hasReachedWindowLimit(r, d.length)) break;
775
- }
776
- }
777
- return this.materializeIndicesResult(d);
778
- }
779
- if (l !== null) {
780
- for (let n = 0; n < e.length; n++) if (l[n]?.includes(t)) {
781
- if (f < r.offset) {
782
- f += 1;
783
- continue;
784
- }
785
- if (d.push(n), f += 1, this.hasReachedWindowLimit(r, d.length)) break;
786
- }
787
- return this.materializeIndicesResult(d);
788
- }
789
- if (u) {
790
- for (let n = 0; n < e.length; n++) {
791
- let e = !1;
792
- for (let r = 0; r < o; r++) {
793
- let i = s[r][n];
794
- if (i && i.includes(t)) {
795
- e = !0;
796
- break;
797
- }
798
- }
799
- if (e) {
800
- if (f < r.offset) {
801
- f += 1;
802
- continue;
803
- }
804
- if (d.push(n), f += 1, this.hasReachedWindowLimit(r, d.length)) break;
805
- }
806
- }
807
- return this.materializeIndicesResult(d);
808
- }
809
- for (let n = 0; n < e.length; n++) {
810
- let a = e[n], l = !1;
811
- for (let e = 0; e < o; e++) {
812
- let r = s[e];
813
- if (r) {
814
- let e = r[n];
815
- if (e && e.includes(t)) {
816
- l = !0;
817
- break;
818
- }
819
- } else {
820
- let n = a[i[e]];
821
- if (typeof n == "string" && n.toLowerCase().includes(t)) {
822
- l = !0;
823
- break;
824
- }
825
- }
826
- }
827
- if (!l && c && (l = this.nestedCollection.matchesAnyField(a, t)), l) {
828
- if (f < r.offset) {
829
- f += 1;
830
- continue;
831
- }
832
- if (d.push(n), f += 1, this.hasReachedWindowLimit(r, d.length)) break;
833
- }
834
- }
835
- return this.materializeIndicesResult(d);
836
- }
837
- searchLinearSingleField(e, t, n, r, i) {
838
- if (!e.length) return {
839
- items: [],
840
- indices: []
841
- };
842
- if (this.nestedCollection.hasField(t)) {
843
- let a = this.nestedCollection.searchFieldLinearIndices(e, t, n, r ?? void 0);
844
- return this.collectItemsFromIndices(a, i);
845
- }
846
- let a = [], o = this.flatIndexes.get(t), s = o ? o.normalizedValues : this.normalizedValuesCache.get(t) ?? (e === this.dataset ? this.buildNormalizedValuesOnly(e, t) : null), c = 0;
847
- if (r !== null) {
848
- let e = this.dataset;
849
- if (s) {
850
- for (let e = 0; e < r.length; e++) {
851
- let t = r[e];
852
- if (s[t]?.includes(n)) {
853
- if (c < i.offset) {
854
- c += 1;
855
- continue;
856
- }
857
- if (a.push(t), c += 1, this.hasReachedWindowLimit(i, a.length)) break;
858
- }
859
- }
860
- return this.materializeIndicesResult(a);
861
- }
862
- for (let o = 0; o < r.length; o++) {
863
- let s = r[o], l = e[s];
864
- if (typeof l[t] == "string" && l[t].toLowerCase().includes(n)) {
865
- if (c < i.offset) {
866
- c += 1;
867
- continue;
868
- }
869
- if (a.push(s), c += 1, this.hasReachedWindowLimit(i, a.length)) break;
870
- }
871
- }
872
- return this.materializeIndicesResult(a);
873
- }
874
- if (s) {
875
- for (let t = 0; t < e.length; t++) if (s[t]?.includes(n)) {
876
- if (c < i.offset) {
877
- c += 1;
878
- continue;
879
- }
880
- if (a.push(t), c += 1, this.hasReachedWindowLimit(i, a.length)) break;
881
- }
882
- return this.materializeIndicesResult(a);
883
- }
884
- for (let r = 0; r < e.length; r++) if (typeof e[r][t] == "string" && e[r][t].toLowerCase().includes(n)) {
885
- if (c < i.offset) {
886
- c += 1;
887
- continue;
888
- }
889
- if (a.push(r), c += 1, this.hasReachedWindowLimit(i, a.length)) break;
890
- }
891
- return this.materializeIndicesResult(a);
892
- }
893
- clearIndexes() {
894
- return this.flatIndexes.clear(), this.nestedCollection.clearIndexes(), this.normalizedValuesCache.clear(), this.combinedNormalizedValuesCache = null, this;
895
- }
896
- getOriginData() {
897
- return this.state.getOriginData();
898
- }
899
- data(e) {
900
- return this.state.data(e), this;
901
- }
902
- add(e) {
903
- return this.state.add(e), this;
904
- }
905
- update(e) {
906
- return this.state.update(e), this;
907
- }
908
- applyAddedItems(e, t) {
909
- if (e.length === 0) return this;
910
- for (let n of this.indexedFields) this.addItemsToField(n, e, t);
911
- return this.nestedCollection.addItems(e, t), this;
912
- }
913
- clearData() {
914
- return this.state.clearData(), this;
915
- }
916
- handleStateMutation(e) {
917
- if (this.shouldDeferMutationIndexUpdates() && (e.type === "add" || e.type === "update")) {
918
- this.markDeferredMutationState();
919
- return;
920
- }
921
- switch (e.type) {
922
- case "add":
923
- this.applyAddedItems(e.items, e.startIndex);
924
- for (let [t, n] of this.normalizedValuesCache) for (let r = 0; r < e.items.length; r++) {
925
- let i = e.items[r][t];
926
- n[e.startIndex + r] = typeof i == "string" ? i.toLowerCase() : "";
927
- }
928
- this.combinedNormalizedValuesCache = null, this.clearPreviousSearchState();
929
- return;
930
- case "update":
931
- this.applyUpdatedItem(e.index, e.previousItem, e.nextItem), this.invalidateNormalizedValuesCacheEntry(e.index, e.nextItem), this.clearPreviousSearchState();
932
- return;
933
- case "data":
934
- this.runtime.deferredMutationVersion = null, this.rebuildConfiguredIndexes(), this.combinedNormalizedValuesCache = null, this.clearPreviousSearchState();
935
- return;
936
- case "clearData":
937
- this.runtime.deferredMutationVersion = null, this.flatIndexes.clear(), this.nestedCollection.clearIndexes(), this.cachedLinearSearchFieldsList = null, this.normalizedValuesCache.clear(), this.combinedNormalizedValuesCache = null, this.clearPreviousSearchState();
938
- return;
939
- case "remove":
940
- this.applyRemovedItem(e.removedItem, e.removedIndex, e.movedItem, e.movedFromIndex), this.cachedLinearSearchFieldsList = null, this.normalizedValuesCache.clear(), this.combinedNormalizedValuesCache = null, this.clearPreviousSearchState();
941
- return;
942
- case "removeMany":
943
- for (let t = 0; t < e.entries.length; t++) {
944
- let n = e.entries[t];
945
- this.applyRemovedItem(n.removedItem, n.removedIndex, n.movedItem, n.movedFromIndex);
946
- }
947
- this.cachedLinearSearchFieldsList = null, this.normalizedValuesCache.clear(), this.combinedNormalizedValuesCache = null, this.clearPreviousSearchState();
948
- return;
949
- }
950
- }
951
- addItemsToField(e, t, n) {
952
- let r = this.flatIndexes.get(e);
953
- if (!r) return;
954
- let { ngramMap: i, normalizedValues: a } = r;
955
- for (let r = 0; r < t.length; r++) {
956
- let o = t[r][e];
957
- if (typeof o != "string") continue;
958
- let s = o.toLowerCase(), c = n + r;
959
- a[c] = s, d(i, s, c);
960
- }
961
- r.version = this.state.getMutationVersion();
962
- }
963
- applyUpdatedItem(e, t, n) {
964
- for (let r of this.indexedFields) this.updateIndexedField(r, e, t, n);
965
- this.nestedCollection.updateItem(n, t, e);
966
- }
967
- applyRemovedItem(e, t, n, r) {
968
- for (let i of this.indexedFields) this.removeIndexedFieldValue(i, e, t), n !== null && r !== null && this.moveIndexedFieldValue(i, n, r, t);
969
- this.nestedCollection.removeItem(e, t), n !== null && r !== null && this.nestedCollection.moveItem(n, r, t);
970
- }
971
- updateIndexedField(e, t, n, r) {
972
- let i = this.flatIndexes.get(e);
973
- if (!i) return;
974
- if (n[e] === r[e]) {
975
- i.version = this.state.getMutationVersion();
976
- return;
977
- }
978
- let { ngramMap: a, normalizedValues: o } = i, s = this.getNormalizedFieldValue(n, e);
979
- s && m(a, s, t);
980
- let c = this.getNormalizedFieldValue(r, e);
981
- if (!c) {
982
- delete o[t], i.version = this.state.getMutationVersion();
983
- return;
984
- }
985
- o[t] = c, d(a, c, t), i.version = this.state.getMutationVersion();
986
- }
987
- removeIndexedFieldValue(e, t, n) {
988
- let r = this.flatIndexes.get(e);
989
- if (!r) return;
990
- let { ngramMap: i, normalizedValues: a } = r, o = this.getNormalizedFieldValue(t, e);
991
- o && m(i, o, n), delete a[n], r.version = this.state.getMutationVersion();
992
- }
993
- moveIndexedFieldValue(e, t, n, r) {
994
- if (n === r) return;
995
- let i = this.flatIndexes.get(e);
996
- if (!i) return;
997
- let { ngramMap: a, normalizedValues: o } = i, s = o[n] ?? this.getNormalizedFieldValue(t, e);
998
- if (!s) {
999
- delete o[n], i.version = this.state.getMutationVersion();
1000
- return;
1001
- }
1002
- m(a, s, n), d(a, s, r), o[r] = s, delete o[n], i.version = this.state.getMutationVersion();
1003
- }
1004
- getNormalizedFieldValue(e, t) {
1005
- let n = e[t];
1006
- return typeof n == "string" ? n.toLowerCase() : null;
1007
- }
1008
- };
1009
- //#endregion
1010
- export { _ as TextSearchEngine };
1
+ import { TextSearchEngine as e } from "./text-search.mjs";
2
+ export { e as TextSearchEngine };