@devisfuture/mega-collection 2.4.5 → 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 (63) hide show
  1. package/README.md +2 -2
  2. package/dist/State.d.ts +42 -0
  3. package/dist/{chunk-BJZg_1dI.mjs → State.mjs} +2 -2
  4. package/dist/constants.d.ts +5 -0
  5. package/dist/constants.mjs +4 -0
  6. package/dist/filter/chain.d.ts +9 -0
  7. package/dist/filter/chain.mjs +22 -0
  8. package/dist/filter/constants.d.ts +2 -0
  9. package/dist/filter/criterion.d.ts +6 -0
  10. package/dist/filter/criterion.mjs +64 -0
  11. package/dist/filter/errors.d.ts +7 -0
  12. package/dist/filter/errors.mjs +17 -0
  13. package/dist/filter/filter.d.ts +89 -0
  14. package/dist/filter/filter.mjs +439 -0
  15. package/dist/filter/index.mjs +2 -891
  16. package/dist/filter/nested.d.ts +32 -0
  17. package/dist/filter/nested.mjs +246 -0
  18. package/dist/filter/types.d.ts +74 -0
  19. package/dist/filter/utils.d.ts +4 -0
  20. package/dist/filter/utils.mjs +29 -0
  21. package/dist/index.mjs +1 -1
  22. package/dist/indexer.d.ts +41 -0
  23. package/dist/indexer.mjs +101 -0
  24. package/dist/internal.d.ts +6 -0
  25. package/dist/{chunk-C5Zpu_ol.mjs → internal.mjs} +1 -1
  26. package/dist/merge/chain.d.ts +9 -0
  27. package/dist/merge/chain.mjs +23 -0
  28. package/dist/merge/constants.d.ts +6 -0
  29. package/dist/merge/constants.mjs +8 -0
  30. package/dist/merge/errors.d.ts +8 -0
  31. package/dist/merge/errors.mjs +19 -0
  32. package/dist/merge/index.mjs +1 -1
  33. package/dist/merge/merge-engines.d.ts +75 -0
  34. package/dist/{chunk-BURK5sks.mjs → merge/merge-engines.mjs} +35 -108
  35. package/dist/merge/module-adapters.d.ts +7 -0
  36. package/dist/merge/module-adapters.mjs +41 -0
  37. package/dist/merge/types.d.ts +125 -0
  38. package/dist/search/constants.d.ts +5 -0
  39. package/dist/search/index.mjs +2 -1010
  40. package/dist/search/nested.d.ts +41 -0
  41. package/dist/search/nested.mjs +210 -0
  42. package/dist/search/ngram.d.ts +23 -0
  43. package/dist/search/ngram.mjs +96 -0
  44. package/dist/search/text-search.d.ts +146 -0
  45. package/dist/search/text-search.mjs +696 -0
  46. package/dist/search/types.d.ts +93 -0
  47. package/dist/search/utils.d.ts +4 -0
  48. package/dist/search/utils.mjs +22 -0
  49. package/dist/sort/errors.d.ts +5 -0
  50. package/dist/sort/errors.mjs +11 -0
  51. package/dist/sort/index.mjs +2 -416
  52. package/dist/sort/sorter.d.ts +47 -0
  53. package/dist/sort/sorter.mjs +375 -0
  54. package/dist/sort/types.d.ts +18 -0
  55. package/dist/sort/utils.d.ts +17 -0
  56. package/dist/sort/utils.mjs +38 -0
  57. package/dist/types.d.ts +73 -0
  58. package/package.json +11 -34
  59. /package/dist/filter/{index.d.mts → index.d.ts} +0 -0
  60. /package/dist/{index.d.mts → index.d.ts} +0 -0
  61. /package/dist/merge/{index.d.mts → index.d.ts} +0 -0
  62. /package/dist/search/{index.d.mts → index.d.ts} +0 -0
  63. /package/dist/sort/{index.d.mts → index.d.ts} +0 -0
@@ -1,891 +1,2 @@
1
- import { a as e, i as t, t as n } from "../chunk-BJZg_1dI.mjs";
2
- import { n as r, t as i } from "../chunk-C5Zpu_ol.mjs";
3
- //#region src/indexer.ts
4
- var a = class {
5
- constructor(e = {
6
- indexes: /* @__PURE__ */ new Map(),
7
- itemPositions: /* @__PURE__ */ new Map()
8
- }) {
9
- this.storage = e;
10
- }
11
- addItems(e) {
12
- if (!(e.length === 0 || this.storage.indexes.size === 0)) for (let t = 0; t < e.length; t++) this.addItem(e[t]);
13
- }
14
- buildIndex(e, t) {
15
- let n = /* @__PURE__ */ new Map(), r = /* @__PURE__ */ new Map();
16
- for (let i = 0, a = e.length; i < a; i++) {
17
- let a = e[i], o = a[t];
18
- if (o == null) continue;
19
- let s = n.get(o);
20
- if (s) s.push(a), r.get(o).set(a, s.length - 1);
21
- else {
22
- n.set(o, [a]);
23
- let e = /* @__PURE__ */ new WeakMap();
24
- e.set(a, 0), r.set(o, e);
25
- }
26
- }
27
- this.storage.indexes.set(t, n), this.storage.itemPositions.set(t, r);
28
- }
29
- getByValue(e, t) {
30
- let n = this.storage.indexes.get(e);
31
- return n ? n.get(t) ?? [] : [];
32
- }
33
- getByValues(e, t) {
34
- let n = this.storage.indexes.get(e);
35
- if (!n) return [];
36
- if (t.length === 1) return n.get(t[0]) ?? [];
37
- if (t.length === 2) {
38
- let e = n.get(t[0]), r = n.get(t[1]);
39
- return e ? r ? e.concat(r) : e : r ?? [];
40
- }
41
- let r = [];
42
- for (let e = 0; e < t.length; e++) {
43
- let i = n.get(t[e]);
44
- if (i !== void 0) for (let e = 0; e < i.length; e++) r.push(i[e]);
45
- }
46
- return r;
47
- }
48
- hasIndex(e) {
49
- return this.storage.indexes.has(e);
50
- }
51
- addItem(e) {
52
- for (let t of this.storage.indexes.keys()) this.addItemToField(t, e);
53
- }
54
- updateItem(e, t) {
55
- for (let n of this.storage.indexes.keys()) this.updateItemInField(n, e, t);
56
- }
57
- removeItem(e) {
58
- for (let t of this.storage.indexes.keys()) this.removeItemFromField(t, e);
59
- }
60
- clear() {
61
- this.storage.indexes.clear(), this.storage.itemPositions.clear();
62
- }
63
- getIndexMap(e) {
64
- return this.storage.indexes.get(e);
65
- }
66
- removeItemFromField(e, t) {
67
- let n = t[e];
68
- if (n == null) return;
69
- let r = this.storage.indexes.get(e), i = this.storage.itemPositions.get(e), a = r?.get(n), o = i?.get(n), s = o?.get(t);
70
- if (!r || !i || !a || !o || s === void 0) return;
71
- let c = a.length - 1, l = a[c];
72
- s !== c && (a[s] = l, o.set(l, s)), a.pop(), o.delete(t), a.length === 0 && (r.delete(n), i.delete(n));
73
- }
74
- updateItemInField(e, t, n) {
75
- let r = t[e];
76
- if (r === n[e]) {
77
- if (r == null) return;
78
- this.replaceItemReferenceInField(e, r, t, n);
79
- return;
80
- }
81
- this.removeItemFromField(e, t), this.addItemToField(e, n);
82
- }
83
- replaceItemReferenceInField(e, t, n, r) {
84
- let i = this.storage.indexes.get(e), a = this.storage.itemPositions.get(e), o = i?.get(t), s = a?.get(t), c = s?.get(n);
85
- !o || !s || c === void 0 || (o[c] = r, s.delete(n), s.set(r, c));
86
- }
87
- addItemToField(e, t) {
88
- let n = t[e];
89
- if (n == null) return;
90
- let r = this.storage.indexes.get(e), i = this.storage.itemPositions.get(e);
91
- if (!r || !i) return;
92
- let a = r.get(n), o = i.get(n);
93
- if (a && o) {
94
- a.push(t), o.set(t, a.length - 1);
95
- return;
96
- }
97
- r.set(n, [t]);
98
- let s = /* @__PURE__ */ new WeakMap();
99
- s.set(t, 0), i.set(n, s);
100
- }
101
- }, o = class {
102
- constructor(e) {
103
- this.callbacks = e;
104
- }
105
- create(e) {
106
- let t = e;
107
- return Object.defineProperties(t, {
108
- filter: i((t, n) => n === void 0 ? this.callbacks.filter(e, t) : this.callbacks.filter(t, n)),
109
- add: i((e) => this.callbacks.add(e)),
110
- update: i((e) => this.callbacks.update(e)),
111
- clearIndexes: i(() => this.callbacks.clearIndexes()),
112
- data: i((e) => this.callbacks.data(e)),
113
- getOriginData: i(() => this.callbacks.getOriginData()),
114
- clearData: i(() => this.callbacks.clearData()),
115
- resetFilterState: i(() => this.callbacks.resetFilterState())
116
- }), t;
117
- }
118
- }, s = class e extends Error {
119
- constructor(e) {
120
- super(e), this.name = "FilterEngineError";
121
- }
122
- static missingDatasetForBuildIndex() {
123
- return new e("FilterEngine: no dataset in memory. Call data() or add() before buildIndex().");
124
- }
125
- static missingDatasetForFilter() {
126
- return new e("FilterEngine: no dataset in memory. Call data() or add() before filter().");
127
- }
128
- static duplicateMutableExcludeField(t) {
129
- return new e(`FilterEngine: cannot use mutable exclude on field \`${t}\` because it contains duplicate values.`);
130
- }
131
- };
132
- //#endregion
133
- //#region src/filter/criterion.ts
134
- function c(e) {
135
- let t = /* @__PURE__ */ new Map();
136
- for (let n = 0; n < e.length; n++) {
137
- let r = e[n], i = Array.isArray(r.values), a = d(r.exclude), o = a !== null, s = i ? f(r.values, a) : null;
138
- if (!i && !o) continue;
139
- let c = r.field, l = t.get(c);
140
- if (!l) {
141
- t.set(c, {
142
- field: r.field,
143
- values: [],
144
- exclude: [],
145
- hasValues: i,
146
- hasExclude: o,
147
- includedValues: s ? new Set(s) : null,
148
- excludedValues: a ? new Set(a) : null,
149
- cacheKeySegment: ""
150
- });
151
- continue;
152
- }
153
- if (i) if (!l.hasValues) l.hasValues = !0, l.includedValues = new Set(s);
154
- else for (let e of l.includedValues) s.has(e) || l.includedValues.delete(e);
155
- if (o) if (!l.hasExclude) l.hasExclude = !0, l.excludedValues = new Set(a);
156
- else for (let e of a) l.excludedValues.add(e);
157
- }
158
- let n = [...t.values()].sort((e, t) => e.field.localeCompare(t.field));
159
- for (let e = 0; e < n.length; e++) p(n[e]);
160
- return n;
161
- }
162
- function l(e, t) {
163
- return !(e.hasValues && !e.includedValues.has(t) || e.hasExclude && e.excludedValues.has(t));
164
- }
165
- function u(e) {
166
- return e.hasValues && e.values.length === 0;
167
- }
168
- function d(e) {
169
- return !Array.isArray(e) || e.length === 0 ? null : new Set(e);
170
- }
171
- function f(e, t) {
172
- let n = /* @__PURE__ */ new Set();
173
- for (let r = 0; r < e.length; r++) {
174
- let i = e[r];
175
- t?.has(i) || n.add(i);
176
- }
177
- return n;
178
- }
179
- function p(e) {
180
- if (e.hasValues && e.hasExclude) for (let t of e.excludedValues) e.includedValues.delete(t);
181
- e.values = e.includedValues ? [...e.includedValues] : [], e.exclude = e.excludedValues ? [...e.excludedValues] : [], e.cacheKeySegment = m(e);
182
- }
183
- function m(e) {
184
- let t = e.field;
185
- return t += `|hv:${e.hasValues ? "1" : "0"}|v:`, e.hasValues && (t += h(e.values)), t += `|hx:${e.hasExclude ? "1" : "0"}|x:`, e.hasExclude && (t += h(e.exclude)), t;
186
- }
187
- function h(e) {
188
- return e.length === 0 ? "" : e.map((e) => g(e)).sort().join(",");
189
- }
190
- function g(e) {
191
- if (e === null) return "null:null";
192
- let t = typeof e;
193
- return t === "object" ? `object:${JSON.stringify(e)}` : `${t}:${String(e)}`;
194
- }
195
- //#endregion
196
- //#region src/filter/nested.ts
197
- var _ = class {
198
- constructor(e = {
199
- indexes: /* @__PURE__ */ new Map(),
200
- itemPositions: /* @__PURE__ */ new Map()
201
- }) {
202
- this.storage = e, this.registeredFields = /* @__PURE__ */ new Set(), this.fieldDescriptors = /* @__PURE__ */ new Map();
203
- }
204
- registerFields(e) {
205
- if (e?.length) for (let t = 0; t < e.length; t++) this.registerField(e[t]);
206
- }
207
- hasRegisteredFields() {
208
- return this.registeredFields.size > 0;
209
- }
210
- hasField(e) {
211
- return this.registeredFields.has(e);
212
- }
213
- clearIndexes() {
214
- this.storage.indexes.clear(), this.storage.itemPositions.clear();
215
- }
216
- buildIndexes(e) {
217
- this.storage.indexes.clear(), this.storage.itemPositions.clear();
218
- for (let t of this.registeredFields) this.buildIndex(e, t);
219
- }
220
- addItems(e) {
221
- if (!(e.length === 0 || this.storage.indexes.size === 0)) for (let t = 0; t < e.length; t++) this.addItem(e[t]);
222
- }
223
- removeItem(e) {
224
- for (let t of this.storage.indexes.keys()) this.removeItemFromIndex(t, e);
225
- }
226
- updateItem(e, t) {
227
- for (let n of this.storage.indexes.keys()) this.updateItemInIndex(n, e, t);
228
- }
229
- filter(e, t, n) {
230
- let r = this.resolveCriteria(t);
231
- if (e.length === 0 || r.length === 0) return e;
232
- let i = [], a = [];
233
- for (let e = 0; e < r.length; e++) {
234
- let t = r[e];
235
- if (this.storage.indexes.has(t.field)) {
236
- i.push(t);
237
- continue;
238
- }
239
- a.push(t);
240
- }
241
- let o = e;
242
- if (i.length > 0 && (o = this.filterByIndexes(i, e, n), o.length === 0)) return o;
243
- for (let e = 0; e < a.length; e++) if (o = this.filterLinearly(o, a[e]), o.length === 0) return o;
244
- return o;
245
- }
246
- resolveCriteria(e) {
247
- if (e.length === 0) return [];
248
- let t = e[0];
249
- return "hasValues" in t && "hasExclude" in t && "includedValues" in t ? e : c(e);
250
- }
251
- registerField(e) {
252
- let t = r(e);
253
- t && (this.registeredFields.add(e), this.fieldDescriptors.set(e, t));
254
- }
255
- buildIndex(e, t) {
256
- let n = this.fieldDescriptors.get(t);
257
- if (!n) return;
258
- let { collectionKey: r, nestedKey: i } = n, a = /* @__PURE__ */ new Map(), o = /* @__PURE__ */ new Map();
259
- for (let t = 0, n = e.length; t < n; t++) {
260
- let n = e[t], s = n[r];
261
- if (Array.isArray(s)) for (let e = 0; e < s.length; e++) {
262
- let t = s[e][i];
263
- if (t == null) continue;
264
- let r = a.get(t);
265
- if (r) {
266
- r[r.length - 1] !== n && (r.push(n), o.get(t).set(n, r.length - 1));
267
- continue;
268
- }
269
- a.set(t, [n]);
270
- let c = /* @__PURE__ */ new WeakMap();
271
- c.set(n, 0), o.set(t, c);
272
- }
273
- }
274
- this.storage.indexes.set(t, a), this.storage.itemPositions.set(t, o);
275
- }
276
- removeItemFromIndex(e, t) {
277
- let n = this.fieldDescriptors.get(e), r = this.storage.indexes.get(e), i = this.storage.itemPositions.get(e);
278
- if (!n || !r || !i) return;
279
- let { collectionKey: a, nestedKey: o } = n, s = t[a];
280
- if (!Array.isArray(s) || s.length === 0) return;
281
- let c = /* @__PURE__ */ new Set();
282
- for (let e = 0; e < s.length; e++) {
283
- let t = s[e][o];
284
- t != null && c.add(t);
285
- }
286
- for (let e of c) {
287
- let n = r.get(e), a = i.get(e), o = a?.get(t);
288
- if (!n || !a || o === void 0) continue;
289
- let s = n.length - 1, c = n[s];
290
- o !== s && (n[o] = c, a.set(c, o)), n.pop(), a.delete(t), n.length === 0 && (r.delete(e), i.delete(e));
291
- }
292
- }
293
- addItem(e) {
294
- for (let t of this.storage.indexes.keys()) this.addItemToIndex(t, e);
295
- }
296
- addItemToIndex(e, t) {
297
- let n = this.fieldDescriptors.get(e), r = this.storage.indexes.get(e), i = this.storage.itemPositions.get(e);
298
- if (!n || !r || !i) return;
299
- let { collectionKey: a, nestedKey: o } = n, s = t[a];
300
- if (!Array.isArray(s) || s.length === 0) return;
301
- let c = /* @__PURE__ */ new Set();
302
- for (let e = 0; e < s.length; e++) {
303
- let t = s[e][o];
304
- t != null && c.add(t);
305
- }
306
- for (let e of c) {
307
- let n = r.get(e), a = i.get(e);
308
- if (n && a) {
309
- n.push(t), a.set(t, n.length - 1);
310
- continue;
311
- }
312
- r.set(e, [t]);
313
- let o = /* @__PURE__ */ new WeakMap();
314
- o.set(t, 0), i.set(e, o);
315
- }
316
- }
317
- updateItemInIndex(e, t, n) {
318
- let r = this.fieldDescriptors.get(e), i = this.storage.indexes.get(e), a = this.storage.itemPositions.get(e);
319
- if (!r || !i || !a) return;
320
- let o = this.collectUniqueValues(n, r), s = this.collectUniqueValues(t, r);
321
- if (this.areValueSetsEqual(o, s)) {
322
- for (let e of o) {
323
- let r = i.get(e), o = a.get(e), s = o?.get(n);
324
- !r || !o || s === void 0 || (r[s] = t, o.delete(n), o.set(t, s));
325
- }
326
- return;
327
- }
328
- this.removeItemFromIndex(e, n), this.addItemToIndex(e, t);
329
- }
330
- collectUniqueValues(e, t) {
331
- let { collectionKey: n, nestedKey: r } = t, i = e[n], a = /* @__PURE__ */ new Set();
332
- if (!Array.isArray(i) || i.length === 0) return a;
333
- for (let e = 0; e < i.length; e++) {
334
- let t = i[e][r];
335
- t != null && a.add(t);
336
- }
337
- return a;
338
- }
339
- areValueSetsEqual(e, t) {
340
- if (e.size !== t.size) return !1;
341
- for (let n of e) if (!t.has(n)) return !1;
342
- return !0;
343
- }
344
- filterByIndexes(e, t, n) {
345
- let r = e.filter((e) => e.hasValues), i = e.filter((e) => e.hasExclude), a = t === n ? null : new Set(t);
346
- if (r.length === 0) return this.applyIndexedExclusions(t, i);
347
- if (r.length === 1) {
348
- let e = this.storage.indexes.get(r[0].field);
349
- if (!e) return [];
350
- let t = this.getItemsByValues(e, r[0].values);
351
- if (t.length === 0) return [];
352
- let n = [];
353
- for (let e = 0; e < t.length; e++) {
354
- let r = t[e];
355
- a && !a.has(r) || n.push(r);
356
- }
357
- return this.applyIndexedExclusions(n, i);
358
- }
359
- let o = r.map((e) => ({
360
- criterion: e,
361
- size: this.estimateIndexSize(e)
362
- })).sort((e, t) => e.size - t.size), s = a, c = [];
363
- for (let e = 0; e < o.length; e++) {
364
- let { criterion: t } = o[e], n = this.storage.indexes.get(t.field);
365
- if (!n) return [];
366
- let r = this.getItemsByValues(n, t.values);
367
- if (r.length === 0) return [];
368
- if (s === null) c = r;
369
- else {
370
- c = [];
371
- for (let e = 0; e < r.length; e++) {
372
- let t = r[e];
373
- s.has(t) && c.push(t);
374
- }
375
- }
376
- if (c.length === 0) return [];
377
- s = new Set(c);
378
- }
379
- return this.applyIndexedExclusions(c, i);
380
- }
381
- getItemsByValues(e, t) {
382
- if (t.length === 1) return e.get(t[0]) ?? [];
383
- let n = /* @__PURE__ */ new Set(), r = [];
384
- for (let i = 0; i < t.length; i++) {
385
- let a = e.get(t[i]);
386
- if (a) for (let e = 0; e < a.length; e++) {
387
- let t = a[e];
388
- n.has(t) || (n.add(t), r.push(t));
389
- }
390
- }
391
- return r;
392
- }
393
- estimateIndexSize(e) {
394
- let t = this.storage.indexes.get(e.field);
395
- return t ? e.values.reduce((e, n) => {
396
- let r = t.get(n);
397
- return r ? e + r.length : e;
398
- }, 0) : Infinity;
399
- }
400
- filterLinearly(e, t) {
401
- let n = this.fieldDescriptors.get(t.field);
402
- if (!n) return e;
403
- let { collectionKey: r, nestedKey: i } = n, a = [];
404
- for (let n = 0; n < e.length; n++) {
405
- let o = e[n], s = o[r];
406
- if (!Array.isArray(s)) continue;
407
- let c = !t.hasValues, l = !1;
408
- for (let e = 0; e < s.length; e++) {
409
- let n = s[e][i];
410
- if (t.hasExclude && t.excludedValues.has(n)) {
411
- l = !0;
412
- break;
413
- }
414
- t.hasValues && t.includedValues.has(n) && (c = !0);
415
- }
416
- !l && c && a.push(o);
417
- }
418
- return a;
419
- }
420
- applyIndexedExclusions(e, t) {
421
- if (t.length === 0 || e.length === 0) return e;
422
- let n = /* @__PURE__ */ new Set();
423
- for (let e = 0; e < t.length; e++) {
424
- let r = t[e], i = this.storage.indexes.get(r.field);
425
- if (!i) continue;
426
- let a = this.getItemsByValues(i, r.exclude);
427
- for (let e = 0; e < a.length; e++) n.add(a[e]);
428
- }
429
- if (n.size === 0) return e;
430
- let r = [];
431
- for (let t = 0; t < e.length; t++) {
432
- let i = e[t];
433
- n.has(i) || r.push(i);
434
- }
435
- return r;
436
- }
437
- }, v = () => ({
438
- indexedFields: /* @__PURE__ */ new Set(),
439
- indexerStorage: {
440
- indexes: /* @__PURE__ */ new Map(),
441
- itemPositions: /* @__PURE__ */ new Map()
442
- },
443
- nestedStorage: {
444
- indexes: /* @__PURE__ */ new Map(),
445
- itemPositions: /* @__PURE__ */ new Map()
446
- },
447
- deferredMutationVersion: null,
448
- mutableExclude: {
449
- datasetPositions: /* @__PURE__ */ new Map(),
450
- valueCounts: /* @__PURE__ */ new Map(),
451
- duplicateValueCount: 0,
452
- hasDuplicateValues: !1
453
- },
454
- sequentialCache: {
455
- previousResult: null,
456
- previousCriteria: null,
457
- previousCriteriaKey: null,
458
- previousBaseData: null,
459
- previousResultsByCriteria: /* @__PURE__ */ new Map(),
460
- previousResultSet: null
461
- }
462
- }), y = class {
463
- constructor(t = {}) {
464
- this.chainBuilder = new o({
465
- filter: (e, t) => t === void 0 ? this.filter(e) : this.filter(e, t),
466
- getOriginData: () => this.getOriginData(),
467
- add: (e) => this.add(e),
468
- update: (e) => this.update(e),
469
- data: (e) => this.data(e),
470
- clearIndexes: () => this.clearIndexes(),
471
- clearData: () => this.clearData(),
472
- resetFilterState: () => this.resetFilterState()
473
- }), this.mutableExcludeField = t.mutableExcludeField ?? null, this.state = t.state ?? new e(t.data ?? [], { filterByPreviousResult: t.filterByPreviousResult ?? !1 }), t.state && t.filterByPreviousResult && this.state.setFilterByPreviousResult(!0), this.filterByPreviousResult = this.state.isFilterByPreviousResultEnabled(), this.namespace = this.state.createNamespace("filter"), this.indexer = new a(this.runtime.indexerStorage), this.nestedCollection = new _(this.runtime.nestedStorage), this.nestedCollection.registerFields(t.nestedFields), this.state.subscribe((e) => this.handleStateMutation(e)), this.rebuildMutableExcludeState();
474
- let n = t.fields?.length, r = this.nestedCollection.hasRegisteredFields();
475
- if (n) for (let e of t.fields) this.indexedFields.add(e);
476
- this.dataset.length > 0 && (n || r) && this.rebuildConfiguredIndexes();
477
- }
478
- get dataset() {
479
- return this.state.getOriginData();
480
- }
481
- get runtime() {
482
- return this.state.getOrCreateScopedValue(this.namespace, "runtime", v);
483
- }
484
- get mutableExcludeState() {
485
- return this.runtime.mutableExclude;
486
- }
487
- get indexedFields() {
488
- return this.runtime.indexedFields;
489
- }
490
- get sequentialCache() {
491
- return this.runtime.sequentialCache;
492
- }
493
- shouldDeferMutationIndexUpdates() {
494
- return this.state.getScopedValue(t, n) === !0;
495
- }
496
- markDeferredMutationState() {
497
- this.runtime.deferredMutationVersion = this.state.getMutationVersion(), this.indexer.clear(), this.nestedCollection.clearIndexes(), this.clearMutableExcludeState(), this.resetFilterState();
498
- }
499
- ensureRuntimeReady() {
500
- this.runtime.deferredMutationVersion !== null && (this.runtime.deferredMutationVersion = null, this.rebuildMutableExcludeState(), this.dataset.length > 0 && (this.indexedFields.size > 0 || this.nestedCollection.hasRegisteredFields()) && this.rebuildConfiguredIndexes());
501
- }
502
- rebuildConfiguredIndexes() {
503
- this.indexer.clear(), this.nestedCollection.clearIndexes();
504
- for (let e of this.indexedFields) this.buildIndex(this.dataset, e);
505
- this.nestedCollection.hasRegisteredFields() && this.nestedCollection.buildIndexes(this.dataset);
506
- }
507
- buildIndex(e, t) {
508
- if (!Array.isArray(e)) {
509
- if (!this.dataset.length) throw s.missingDatasetForBuildIndex();
510
- return this.indexer.buildIndex(this.dataset, e), this;
511
- }
512
- return this.resetFilterState(), this.rebuildMutableExcludeState(), this.indexer.buildIndex(e, t), this;
513
- }
514
- clearIndexes() {
515
- return this.indexer.clear(), this.nestedCollection.clearIndexes(), this;
516
- }
517
- resetFilterState() {
518
- return this.sequentialCache.previousResult = null, this.sequentialCache.previousCriteria = null, this.sequentialCache.previousCriteriaKey = null, this.sequentialCache.previousBaseData = null, this.sequentialCache.previousResultsByCriteria.clear(), this.sequentialCache.previousResultSet = null, this.state.clearPreviousResult(), this;
519
- }
520
- clearData() {
521
- return this.state.clearData(), this;
522
- }
523
- data(e) {
524
- return this.state.data(e), this;
525
- }
526
- add(e) {
527
- return this.state.add(e), this;
528
- }
529
- update(e) {
530
- return this.state.update(e), this;
531
- }
532
- applyAddedItems(e, t) {
533
- return e.length === 0 ? this : (this.updateMutableExcludeStateForAddedItems(e, t), this.resetFilterState(), this.indexer.addItems(e), this.nestedCollection.addItems(e), this);
534
- }
535
- getOriginData() {
536
- return this.state.getOriginData();
537
- }
538
- filter(e, t) {
539
- return t === void 0 ? this.withChain(this.rawFilter(e)) : this.withChain(this.rawFilter(e, t));
540
- }
541
- rawFilter(e, t) {
542
- this.ensureRuntimeReady();
543
- let n = t === void 0;
544
- if (n && !this.dataset.length) throw s.missingDatasetForFilter();
545
- let r = c(n ? e : t), i = n ? this.dataset : e;
546
- if (n) {
547
- let e = this.applyMutableExclude(r);
548
- if (e !== void 0) return e;
549
- }
550
- let a = i, o = r;
551
- if (this.filterByPreviousResult) {
552
- let e = this.resolveWithSequentialCache(i, n, r);
553
- if (e.isFromCache) return e.result;
554
- a = e.sourceData, o = e.executionCriteria;
555
- }
556
- if (r.length === 0) return this.filterByPreviousResult && this.resetFilterState(), i;
557
- for (let e = 0; e < o.length; e++) if (u(o[e])) return this.createEmptyResult(n, i, r);
558
- let l = [], d = [];
559
- for (let e = 0; e < o.length; e++) {
560
- let t = o[e];
561
- this.nestedCollection.hasField(t.field) ? l.push(t) : d.push(t);
562
- }
563
- if (l.length > 0 && (a = this.nestedCollection.filter(a, l, this.dataset), a.length === 0)) return this.createEmptyResult(n, i, r);
564
- if (d.length === 0) return this.storePreviousResult(a, n, i, r), a;
565
- let f = [], p = [];
566
- for (let e = 0; e < d.length; e++) {
567
- let t = d[e];
568
- this.indexer.hasIndex(t.field) ? f.push(t) : p.push(t);
569
- }
570
- let m;
571
- if (f.length > 0 && p.length === 0) return m = this.filterViaIndex(f, a), this.storePreviousResult(m, n, i, r), m;
572
- if (f.length > 0 && p.length > 0) {
573
- let e = this.filterViaIndex(f, a);
574
- return m = this.linearFilter(e, p), this.storePreviousResult(m, n, i, r), m;
575
- }
576
- return m = this.linearFilter(a, d), this.storePreviousResult(m, n, i, r), m;
577
- }
578
- resolveWithSequentialCache(e, t, n) {
579
- let { previousResult: r, previousCriteria: i, previousCriteriaKey: a, previousBaseData: o, previousResultsByCriteria: s } = this.sequentialCache;
580
- if (r === null || i === null || o !== e) return {
581
- isFromCache: !1,
582
- sourceData: e,
583
- executionCriteria: n
584
- };
585
- let c = this.createCriteriaCacheKey(n), l = s.get(c);
586
- if (c === a) return {
587
- isFromCache: !0,
588
- result: r
589
- };
590
- if (l !== void 0) return this.storePreviousResult(l.result, t, e, n, c, l.resultSet), {
591
- isFromCache: !0,
592
- result: l.result
593
- };
594
- let u = this.hasCriteriaBacktrack(i, n) || r.length === 0 && !this.canApplySequentiallyToEmptyResult(i, n);
595
- return {
596
- isFromCache: !1,
597
- sourceData: u ? e : r,
598
- executionCriteria: u ? n : this.createSequentialExecutionCriteria(i, n)
599
- };
600
- }
601
- withChain(e) {
602
- return this.chainBuilder.create(e);
603
- }
604
- handleStateMutation(e) {
605
- if (this.shouldDeferMutationIndexUpdates() && (e.type === "add" || e.type === "update")) {
606
- this.markDeferredMutationState();
607
- return;
608
- }
609
- switch (e.type) {
610
- case "add":
611
- this.applyAddedItems(e.items, e.startIndex);
612
- return;
613
- case "update":
614
- this.applyUpdatedItem(e.index, e.previousItem, e.nextItem);
615
- return;
616
- case "data":
617
- this.runtime.deferredMutationVersion = null, this.rebuildMutableExcludeState(), this.resetFilterState(), this.rebuildConfiguredIndexes();
618
- return;
619
- case "clearData":
620
- this.runtime.deferredMutationVersion = null, this.clearMutableExcludeState(), this.indexer.clear(), this.nestedCollection.clearIndexes(), this.resetFilterState();
621
- return;
622
- case "remove":
623
- this.applyRemovedItem(e.field, e.value, e.removedItem, e.removedIndex, e.movedItem);
624
- return;
625
- case "removeMany":
626
- this.applyRemovedItems(e.field, e.entries);
627
- return;
628
- }
629
- }
630
- rebuildMutableExcludeState() {
631
- if (this.clearMutableExcludeState(), this.mutableExcludeField !== null) for (let e = 0; e < this.dataset.length; e++) {
632
- let t = this.dataset[e][this.mutableExcludeField];
633
- t != null && this.registerMutableExcludeValue(t, e);
634
- }
635
- }
636
- updateMutableExcludeStateForAddedItems(e, t) {
637
- if (this.mutableExcludeField !== null) for (let n = 0; n < e.length; n++) {
638
- let r = e[n][this.mutableExcludeField];
639
- r != null && this.registerMutableExcludeValue(r, t + n);
640
- }
641
- }
642
- applyMutableExclude(e) {
643
- if (this.mutableExcludeField === null || e.length !== 1) return;
644
- let t = e[0];
645
- if (!(t.field !== this.mutableExcludeField || t.hasValues || !t.hasExclude)) {
646
- if (this.mutableExcludeState.hasDuplicateValues) throw s.duplicateMutableExcludeField(this.mutableExcludeField);
647
- return this.state.removeByFieldValues(this.mutableExcludeField, t.exclude), this.resetFilterState(), this.dataset;
648
- }
649
- }
650
- applyUpdatedItem(e, t, n) {
651
- this.updateMutableExcludeStateForUpdatedItem(e, t, n), this.resetFilterState(), this.indexer.updateItem(t, n), this.nestedCollection.updateItem(n, t);
652
- }
653
- applyRemovedItem(e, t, n, r, i) {
654
- if (e === this.mutableExcludeField && (this.unregisterMutableExcludeValue(t), this.mutableExcludeState.datasetPositions.delete(t), i !== null && this.mutableExcludeField !== null)) {
655
- let e = i[this.mutableExcludeField];
656
- e != null && this.mutableExcludeState.datasetPositions.set(e, r);
657
- }
658
- this.resetFilterState(), this.indexer.removeItem(n), this.nestedCollection.removeItem(n);
659
- }
660
- applyRemovedItems(e, t) {
661
- for (let n = 0; n < t.length; n++) {
662
- let r = t[n];
663
- if (e === this.mutableExcludeField && (this.unregisterMutableExcludeValue(r.value), this.mutableExcludeState.datasetPositions.delete(r.value), r.movedItem !== null && this.mutableExcludeField !== null)) {
664
- let e = r.movedItem[this.mutableExcludeField];
665
- e != null && this.mutableExcludeState.datasetPositions.set(e, r.removedIndex);
666
- }
667
- this.indexer.removeItem(r.removedItem), this.nestedCollection.removeItem(r.removedItem);
668
- }
669
- this.resetFilterState();
670
- }
671
- updateMutableExcludeStateForUpdatedItem(e, t, n) {
672
- if (this.mutableExcludeField === null) return;
673
- let r = t[this.mutableExcludeField], i = n[this.mutableExcludeField];
674
- if (r === i) {
675
- i != null && this.mutableExcludeState.datasetPositions.set(i, e);
676
- return;
677
- }
678
- r != null && (this.unregisterMutableExcludeValue(r), this.mutableExcludeState.datasetPositions.get(r) === e && this.mutableExcludeState.datasetPositions.delete(r)), i != null && this.registerMutableExcludeValue(i, e);
679
- }
680
- clearMutableExcludeState() {
681
- this.mutableExcludeState.datasetPositions.clear(), this.mutableExcludeState.valueCounts.clear(), this.mutableExcludeState.duplicateValueCount = 0, this.mutableExcludeState.hasDuplicateValues = !1;
682
- }
683
- registerMutableExcludeValue(e, t) {
684
- let n = (this.mutableExcludeState.valueCounts.get(e) ?? 0) + 1;
685
- n === 2 && this.mutableExcludeState.duplicateValueCount++, this.mutableExcludeState.valueCounts.set(e, n), this.mutableExcludeState.datasetPositions.set(e, t), this.mutableExcludeState.hasDuplicateValues = this.mutableExcludeState.duplicateValueCount > 0;
686
- }
687
- unregisterMutableExcludeValue(e) {
688
- let t = this.mutableExcludeState.valueCounts.get(e);
689
- t !== void 0 && (t === 2 && this.mutableExcludeState.duplicateValueCount--, t <= 1 ? this.mutableExcludeState.valueCounts.delete(e) : this.mutableExcludeState.valueCounts.set(e, t - 1), this.mutableExcludeState.hasDuplicateValues = this.mutableExcludeState.duplicateValueCount > 0);
690
- }
691
- linearFilter(e, t) {
692
- let n = [];
693
- for (let r = 0; r < e.length; r++) {
694
- let i = e[r], a = !0;
695
- for (let e = 0; e < t.length; e++) {
696
- let n = t[e];
697
- if (!l(n, i[n.field])) {
698
- a = !1;
699
- break;
700
- }
701
- }
702
- a && n.push(i);
703
- }
704
- return n;
705
- }
706
- filterViaIndex(e, t) {
707
- let n = t !== this.dataset, r = null;
708
- if (n) {
709
- if (t === this.sequentialCache.previousResult && this.sequentialCache.previousResultSet !== null) r = this.sequentialCache.previousResultSet;
710
- else if (r = new Set(t), t === this.sequentialCache.previousResult) {
711
- this.sequentialCache.previousResultSet = r;
712
- let e = this.sequentialCache.previousCriteriaKey;
713
- if (e !== null) {
714
- let t = this.sequentialCache.previousResultsByCriteria.get(e);
715
- t && (t.resultSet = r);
716
- }
717
- }
718
- }
719
- let i = [], a = [];
720
- for (let t = 0; t < e.length; t++) {
721
- let n = e[t];
722
- n.hasValues && i.push(n), n.hasExclude && a.push(n);
723
- }
724
- if (i.length === 0) return this.applyIndexedExclusions(t, a);
725
- if (i.length === 1) {
726
- let e = i[0], t = !1;
727
- for (let n = 0; n < a.length; n++) if (a[n].field !== e.field) {
728
- t = !0;
729
- break;
730
- }
731
- if (r === null && !t) return e.values.length === 1 ? this.indexer.getByValue(e.field, e.values[0]).slice() : this.indexer.getByValues(e.field, e.values);
732
- let n = this.indexer.getByValues(e.field, e.values), o = [];
733
- for (let e = 0; e < n.length; e++) {
734
- let t = n[e];
735
- r && !r.has(t) || o.push(t);
736
- }
737
- return this.applyIndexedExclusions(o, a);
738
- }
739
- let o = i[0], s = this.estimateIndexSize(o);
740
- for (let e = 1; e < i.length; e++) {
741
- let t = i[e], n = this.estimateIndexSize(t);
742
- n < s && (o = t, s = n);
743
- }
744
- let c = this.indexer.getByValues(o.field, o.values);
745
- if (c.length === 0) return [];
746
- let u = [];
747
- for (let e = 0; e < c.length; e++) {
748
- let t = c[e], n = !0;
749
- if (!(r && !r.has(t))) {
750
- for (let e = 0; e < i.length; e++) {
751
- let r = i[e];
752
- if (r !== o && !l(r, t[r.field])) {
753
- n = !1;
754
- break;
755
- }
756
- }
757
- n && u.push(t);
758
- }
759
- }
760
- return this.applyIndexedExclusions(u, a);
761
- }
762
- estimateIndexSize(e) {
763
- let t = this.indexer.getIndexMap(e.field);
764
- if (!t) return Infinity;
765
- let n = 0;
766
- for (let r = 0; r < e.values.length; r++) {
767
- let i = t.get(e.values[r]);
768
- i && (n += i.length);
769
- }
770
- return n;
771
- }
772
- applyIndexedExclusions(e, t) {
773
- if (t.length === 0 || e.length === 0) return e;
774
- let n = /* @__PURE__ */ new Set();
775
- for (let e = 0; e < t.length; e++) {
776
- let r = t[e], i = this.indexer.getIndexMap(r.field);
777
- if (i) for (let e = 0; e < r.exclude.length; e++) {
778
- let t = i.get(r.exclude[e]);
779
- if (t) for (let e = 0; e < t.length; e++) n.add(t[e]);
780
- }
781
- }
782
- if (n.size === 0) return e;
783
- let r = [];
784
- for (let t = 0; t < e.length; t++) {
785
- let i = e[t];
786
- n.has(i) || r.push(i);
787
- }
788
- return r;
789
- }
790
- createEmptyResult(e, t, n) {
791
- let r = [];
792
- return this.storePreviousResult(r, e, t, n), r;
793
- }
794
- storePreviousResult(e, t, n, r, i = this.createCriteriaCacheKey(r), a) {
795
- if (!this.filterByPreviousResult) return;
796
- let o = a === void 0 ? null : a;
797
- this.sequentialCache.previousResult = e, this.sequentialCache.previousCriteria = r, this.sequentialCache.previousCriteriaKey = i, this.sequentialCache.previousBaseData = t ? this.dataset : n, this.sequentialCache.previousResultSet = o, this.state.setPreviousResult(e, t ? this.dataset : n), this.sequentialCache.previousResultsByCriteria.set(i, this.createSequentialCacheEntry(e, o));
798
- }
799
- createCriteriaCacheKey(e) {
800
- let t = "";
801
- for (let n = 0; n < e.length; n++) t += e[n].cacheKeySegment, t += ";";
802
- return t;
803
- }
804
- createSequentialCacheEntry(e, t) {
805
- return {
806
- result: e,
807
- resultSet: t
808
- };
809
- }
810
- createSequentialExecutionCriteria(e, t) {
811
- let n = /* @__PURE__ */ new Map();
812
- for (let t = 0; t < e.length; t++) {
813
- let r = e[t];
814
- n.set(r.field, r);
815
- }
816
- return t.map((e) => {
817
- let t = n.get(e.field);
818
- if (!t || !t.hasValues || !e.hasValues || t.hasExclude || e.hasExclude || e.includedValues.size <= t.includedValues.size) return e;
819
- for (let n of t.includedValues) if (!e.includedValues.has(n)) return e;
820
- let r = [];
821
- for (let n of e.includedValues) t.includedValues.has(n) || r.push(n);
822
- return r.length === 0 ? e : {
823
- ...e,
824
- values: r,
825
- includedValues: new Set(r)
826
- };
827
- });
828
- }
829
- hasCriteriaBacktrack(e, t) {
830
- let n = this.createCriteriaStateMap(e), r = this.createCriteriaStateMap(t);
831
- for (let [e, t] of n) {
832
- let n = r.get(e);
833
- if (!n || this.isCriterionBacktracked(t, n)) return !0;
834
- }
835
- return !1;
836
- }
837
- canApplySequentiallyToEmptyResult(e, t) {
838
- let n = this.createCriteriaStateMap(e), r = this.createCriteriaStateMap(t);
839
- for (let [e, t] of r) {
840
- let r = n.get(e);
841
- if (r) {
842
- if (r.hasValues !== t.hasValues && (r.hasValues || !t.hasValues)) return !1;
843
- if (r.hasValues && t.hasValues) {
844
- let e = r.includedValues, n = t.includedValues;
845
- if (!this.areSetsEqual(e, n) && !this.isSubset(e, n)) return !1;
846
- }
847
- if (r.hasExclude !== t.hasExclude && (r.hasExclude || !t.hasExclude)) return !1;
848
- if (r.hasExclude && t.hasExclude) {
849
- let e = r.excludedValues, n = t.excludedValues;
850
- if (!this.areSetsEqual(e, n) && !this.isSubset(e, n)) return !1;
851
- }
852
- }
853
- }
854
- return !0;
855
- }
856
- createCriteriaStateMap(e) {
857
- let t = /* @__PURE__ */ new Map();
858
- for (let n = 0; n < e.length; n++) {
859
- let r = e[n];
860
- t.set(r.field, r);
861
- }
862
- return t;
863
- }
864
- isCriterionBacktracked(e, t) {
865
- if (e.hasValues && !t.hasValues) return !0;
866
- if (e.hasValues && t.hasValues) {
867
- let n = e.includedValues, r = t.includedValues;
868
- if (!this.areSetsEqual(n, r)) return this.isSubset(r, n);
869
- }
870
- if (!e.hasValues && t.hasValues) return !1;
871
- if (e.hasExclude && !t.hasExclude) return !0;
872
- if (!e.hasExclude && t.hasExclude) return !1;
873
- if (e.hasExclude && t.hasExclude) {
874
- let n = e.excludedValues, r = t.excludedValues;
875
- if (!this.areSetsEqual(n, r)) return this.isSubset(r, n);
876
- }
877
- return !1;
878
- }
879
- isSubset(e, t) {
880
- for (let n of e) if (!t.has(n)) return !1;
881
- return !0;
882
- }
883
- areSetsEqual(e, t) {
884
- if (e === t) return !0;
885
- if (!e || !t || e.size !== t.size) return !1;
886
- for (let n of e) if (!t.has(n)) return !1;
887
- return !0;
888
- }
889
- };
890
- //#endregion
891
- export { y as FilterEngine };
1
+ import { FilterEngine as e } from "./filter.mjs";
2
+ export { e as FilterEngine };