@devisfuture/mega-collection 2.4.2 → 2.4.4

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.
package/README.md CHANGED
@@ -9,7 +9,6 @@ If this package saved you some time, a ⭐ on GitHub would be much appreciated.
9
9
  ## Table of Contents
10
10
 
11
11
  - [What does this package solve](#what-does-this-package-solve) – what problem this package helps with
12
- - [Features](#features) – what the package can do
13
12
  - [How it works](#how-it-works) – plain-English explanation of how each engine works internally
14
13
  - [Benchmarks](#benchmarks) – performance numbers and how to run them
15
14
  - [React demo](#react-demo) – example project and live demo
@@ -33,8 +32,6 @@ If this package saved you some time, a ⭐ on GitHub would be much appreciated.
33
32
  - [`TextSearchEngine<T>`](#textsearchenginet-search-module) – engine for text search
34
33
  - [`FilterEngine<T>`](#filterenginet-filter-module) – engine for filtering
35
34
  - [`SortEngine<T>`](#sortenginet-sort-module) – engine for sorting
36
- - [Types](#types) – TypeScript types you can import
37
- - [Build](#build) – commands for build and development
38
35
  - [Contributing](#contributing) – contribution rules
39
36
  - [Security](#security) – security policy
40
37
  - [License](#license) – license information
@@ -0,0 +1,183 @@
1
+ //#region src/State.ts
2
+ var e = class {
3
+ constructor(e = [], t = {}) {
4
+ this.itemIndexLookup = /* @__PURE__ */ new WeakMap(), this.mutationVersion = 0, this.namespaceSequence = 0, this.previousResultState = null, this.indexMaps = /* @__PURE__ */ new Map(), this.scopedRegistry = /* @__PURE__ */ new Map(), this.listeners = /* @__PURE__ */ new Set(), this.originData = e, this.filterByPreviousResult = t.filterByPreviousResult ?? !1, this.rebuildItemIndexLookup();
5
+ }
6
+ subscribe(e) {
7
+ return this.listeners.add(e), () => {
8
+ this.listeners.delete(e);
9
+ };
10
+ }
11
+ getOriginData() {
12
+ return this.originData;
13
+ }
14
+ getItemIndex(e) {
15
+ return this.itemIndexLookup.get(e);
16
+ }
17
+ getMutationVersion() {
18
+ return this.mutationVersion;
19
+ }
20
+ isFilterByPreviousResultEnabled() {
21
+ return this.filterByPreviousResult;
22
+ }
23
+ setFilterByPreviousResult(e) {
24
+ this.filterByPreviousResult = e, e || this.clearPreviousResult();
25
+ }
26
+ getPreviousResult() {
27
+ return this.getPreviousResultState()?.result ?? null;
28
+ }
29
+ getPreviousResultState() {
30
+ return this.previousResultState === null || this.previousResultState.version !== this.mutationVersion ? null : this.previousResultState;
31
+ }
32
+ setPreviousResult(e, t) {
33
+ this.previousResultState = {
34
+ result: e,
35
+ sourceData: t,
36
+ version: this.mutationVersion
37
+ };
38
+ }
39
+ clearPreviousResult() {
40
+ this.previousResultState = null;
41
+ }
42
+ createNamespace(e = "scope") {
43
+ return this.namespaceSequence += 1, `${e}:${this.namespaceSequence}`;
44
+ }
45
+ getScopedValue(e, t) {
46
+ return this.scopedRegistry.get(e)?.get(t);
47
+ }
48
+ getOrCreateScopedValue(e, t, n) {
49
+ let r = this.getScopedValue(e, t);
50
+ if (r !== void 0) return r;
51
+ let i = this.getOrCreateScope(e), a = n();
52
+ return i.set(t, a), a;
53
+ }
54
+ setScopedValue(e, t, n) {
55
+ return this.getOrCreateScope(e).set(t, n), n;
56
+ }
57
+ deleteScopedValue(e, t) {
58
+ let n = this.scopedRegistry.get(e);
59
+ n && (n.delete(t), n.size === 0 && this.scopedRegistry.delete(e));
60
+ }
61
+ clearScope(e) {
62
+ this.scopedRegistry.delete(e);
63
+ }
64
+ data(e) {
65
+ this.originData = e, this.bumpMutationVersion(), this.rebuildItemIndexLookup();
66
+ for (let e of this.indexMaps.keys()) this.rebuildIndexMap(e);
67
+ this.emit({
68
+ type: "data",
69
+ data: e
70
+ });
71
+ }
72
+ add(e) {
73
+ if (e.length === 0) return;
74
+ let t = this.originData.length;
75
+ for (let n = 0; n < e.length; n++) this.originData.push(e[n]), this.itemIndexLookup.set(e[n], t + n);
76
+ this.bumpMutationVersion();
77
+ for (let [n, r] of this.indexMaps) for (let i = 0; i < e.length; i++) r.set(e[i][n], t + i);
78
+ this.emit({
79
+ type: "add",
80
+ items: e,
81
+ startIndex: t
82
+ });
83
+ }
84
+ update(e) {
85
+ let { field: t, data: n } = e, r = n[t];
86
+ if (r == null) return;
87
+ let i = this.getOrCreateIndexMap(t).get(r);
88
+ if (i === void 0) return;
89
+ let a = this.originData[i];
90
+ this.originData[i] = n, this.itemIndexLookup.delete(a), this.itemIndexLookup.set(n, i), this.bumpMutationVersion();
91
+ for (let [e, t] of this.indexMaps) {
92
+ let r = a[e], o = n[e];
93
+ r !== o && (t.get(r) === i && t.delete(r), t.set(o, i));
94
+ }
95
+ this.emit({
96
+ type: "update",
97
+ field: t,
98
+ index: i,
99
+ previousItem: a,
100
+ nextItem: n
101
+ });
102
+ }
103
+ clearData() {
104
+ let e = [];
105
+ this.originData = e, this.itemIndexLookup = /* @__PURE__ */ new WeakMap(), this.bumpMutationVersion();
106
+ for (let e of this.indexMaps.values()) e.clear();
107
+ this.emit({
108
+ type: "clearData",
109
+ data: e
110
+ });
111
+ }
112
+ removeByFieldValue(e, t) {
113
+ let n = this.getOrCreateIndexMap(e).get(t);
114
+ if (n === void 0) return;
115
+ let r = this.originData.length - 1, i = this.originData[n], a = n === r ? null : this.originData[r];
116
+ n !== r && a && (this.originData[n] = a, this.itemIndexLookup.set(a, n)), this.originData.pop(), this.itemIndexLookup.delete(i), this.bumpMutationVersion();
117
+ for (let [e, t] of this.indexMaps) {
118
+ let r = i[e];
119
+ t.get(r) === n && t.delete(r), a && t.set(a[e], n);
120
+ }
121
+ this.emit({
122
+ type: "remove",
123
+ field: e,
124
+ value: t,
125
+ removedItem: i,
126
+ removedIndex: n,
127
+ movedItem: a,
128
+ movedFromIndex: a ? r : null
129
+ });
130
+ }
131
+ removeByFieldValues(e, t) {
132
+ if (t.length === 0) return;
133
+ let n = this.getOrCreateIndexMap(e), r = [];
134
+ for (let e = 0; e < t.length; e++) {
135
+ let i = t[e], a = n.get(i);
136
+ if (a === void 0) continue;
137
+ let o = this.originData.length - 1, s = this.originData[a], c = a === o ? null : this.originData[o];
138
+ a !== o && c && (this.originData[a] = c, this.itemIndexLookup.set(c, a)), this.originData.pop(), this.itemIndexLookup.delete(s);
139
+ for (let [e, t] of this.indexMaps) {
140
+ let n = s[e];
141
+ t.get(n) === a && t.delete(n), c && t.set(c[e], a);
142
+ }
143
+ r.push({
144
+ value: i,
145
+ removedItem: s,
146
+ removedIndex: a,
147
+ movedItem: c,
148
+ movedFromIndex: c ? o : null
149
+ });
150
+ }
151
+ r.length !== 0 && (this.bumpMutationVersion(), this.emit({
152
+ type: "removeMany",
153
+ field: e,
154
+ entries: r
155
+ }));
156
+ }
157
+ emit(e) {
158
+ for (let t of this.listeners) t(e);
159
+ }
160
+ getOrCreateScope(e) {
161
+ let t = this.scopedRegistry.get(e);
162
+ if (t) return t;
163
+ let n = /* @__PURE__ */ new Map();
164
+ return this.scopedRegistry.set(e, n), n;
165
+ }
166
+ bumpMutationVersion() {
167
+ this.mutationVersion += 1, this.clearPreviousResult();
168
+ }
169
+ getOrCreateIndexMap(e) {
170
+ return this.indexMaps.get(e) || this.rebuildIndexMap(e);
171
+ }
172
+ rebuildIndexMap(e) {
173
+ let t = /* @__PURE__ */ new Map();
174
+ for (let n = 0; n < this.originData.length; n++) t.set(this.originData[n][e], n);
175
+ return this.indexMaps.set(e, t), t;
176
+ }
177
+ rebuildItemIndexLookup() {
178
+ this.itemIndexLookup = /* @__PURE__ */ new WeakMap();
179
+ for (let e = 0; e < this.originData.length; e++) this.itemIndexLookup.set(this.originData[e], e);
180
+ }
181
+ }, t = "__merge__";
182
+ //#endregion
183
+ export { e as n, t };
@@ -0,0 +1,512 @@
1
+ import { n as e, t } from "./chunk-BTXXf5A-.mjs";
2
+ //#region src/merge/chain.ts
3
+ function n(e) {
4
+ return {
5
+ value: e,
6
+ enumerable: !1,
7
+ configurable: !0,
8
+ writable: !0
9
+ };
10
+ }
11
+ var r = class {
12
+ constructor(e) {
13
+ this.callbacks = e;
14
+ }
15
+ create(e) {
16
+ let t = e;
17
+ return Object.defineProperties(t, {
18
+ search: n((e, t) => t === void 0 ? this.callbacks.search(e) : this.callbacks.search(e, t)),
19
+ sort: n((t, n, r) => n === void 0 ? this.callbacks.sort(e, t, r) : this.callbacks.sort(t, n, r)),
20
+ filter: n((t, n) => n === void 0 ? this.callbacks.filter(e, t) : this.callbacks.filter(t, n)),
21
+ add: n((e) => this.callbacks.add(e)),
22
+ update: n((e) => this.callbacks.update(e)),
23
+ clearIndexes: n((t) => (this.callbacks.clearIndexes(t), this.create(e))),
24
+ clearData: n((t) => (this.callbacks.clearData(t), this.create(e))),
25
+ data: n((e) => this.callbacks.data(e)),
26
+ getOriginData: n(() => this.callbacks.getOriginData())
27
+ }), t;
28
+ }
29
+ };
30
+ //#endregion
31
+ //#region src/merge/module-adapters.ts
32
+ function i(e) {
33
+ return typeof e == "object" && !!e;
34
+ }
35
+ function a(e, t) {
36
+ return i(e) && typeof e[t] == "function";
37
+ }
38
+ function o(e) {
39
+ return a(e, "search") && a(e, "getOriginData");
40
+ }
41
+ function s(e) {
42
+ return a(e, "sort") && a(e, "getOriginData");
43
+ }
44
+ function c(e) {
45
+ return a(e, "rawFilter") && a(e, "getOriginData");
46
+ }
47
+ var l = (e) => {
48
+ let { prototype: t } = e;
49
+ return c(t) ? "filter" : s(t) ? "sort" : o(t) ? "search" : null;
50
+ }, u = (e, t, n, r) => {
51
+ let i = new e({
52
+ data: t,
53
+ state: n,
54
+ ...r
55
+ });
56
+ return c(i) ? {
57
+ moduleName: "filter",
58
+ executeFilter: (e, t) => t === void 0 ? i.rawFilter(e) : i.rawFilter(e, t),
59
+ clearIndexes: () => i.clearIndexes()
60
+ } : s(i) ? {
61
+ moduleName: "sort",
62
+ executeSort: (e, t, n) => t === void 0 ? i.sort(e) : i.sort(e, t, n),
63
+ clearIndexes: () => i.clearIndexes()
64
+ } : o(i) ? {
65
+ moduleName: "search",
66
+ executeSearch: (e, t) => t === void 0 ? i.search(e) : i.search(e, t),
67
+ clearIndexes: () => i.clearIndexes()
68
+ } : null;
69
+ }, d = "deferSortMutationCacheUpdates", f = "deferSearchMutationIndexUpdates", p = "deferFilterMutationIndexUpdates", m = {
70
+ search: "TextSearchEngine",
71
+ sort: "SortEngine",
72
+ filter: "FilterEngine"
73
+ }, h = class e extends Error {
74
+ constructor(e) {
75
+ super(e), this.name = "MergeEnginesError";
76
+ }
77
+ static unavailableEngine(t) {
78
+ let n = m[t];
79
+ return new e(`MergeEngines: ${n} is not available.`);
80
+ }
81
+ static unavailableGetOriginData() {
82
+ return new e("MergeEngines: getOriginData is not available.");
83
+ }
84
+ static invalidFilterByPreviousResultOption() {
85
+ return new e("MergeEngines: \"filter.filterByPreviousResult\" is not supported. Configure \"filterByPreviousResult\" on the MergeEngines root options.");
86
+ }
87
+ }, g = class {
88
+ constructor(n) {
89
+ this.previousSearchState = null, this.previousFilterState = null, this.previousSortState = null;
90
+ let { imports: i, data: a, filterByPreviousResult: o = !1, ...s } = n;
91
+ this.validateFilterByPreviousResultOptions(s.filter), this.state = new e(a, { filterByPreviousResult: o });
92
+ let c = new Set(i), m = null, h = null, g = null;
93
+ for (let e of c) {
94
+ let t = l(e);
95
+ if (!t) continue;
96
+ let n = this.getModuleInitOptions(t, e.name, s), r = u(e, a, this.state, n);
97
+ r && (r.moduleName === "search" && !m && (m = r), r.moduleName === "sort" && !h && (h = r), r.moduleName === "filter" && !g && (g = r));
98
+ }
99
+ this.searchModule = m, this.sortModule = h, this.filterModule = g, this.sortModule && this.state.setScopedValue(t, d, !0), this.searchModule && this.state.setScopedValue(t, f, !0), this.filterModule && this.state.setScopedValue(t, p, !0), this.state.subscribe((e) => this.handleStateMutation(e)), this.chainBuilder = new r({
100
+ search: (e, t) => t === void 0 ? this.search(e) : this.search(e, t),
101
+ sort: (e, t, n) => this.sort(e, t, n),
102
+ filter: (e, t) => this.filter(e, t),
103
+ getOriginData: () => this.getOriginData(),
104
+ add: (e) => this.add(e),
105
+ update: (e) => this.update(e),
106
+ data: (e) => this.data(e),
107
+ clearIndexes: (e) => this.clearIndexes(e),
108
+ clearData: (e) => this.clearData(e)
109
+ });
110
+ }
111
+ getAdapter(e) {
112
+ return e === "search" ? this.searchModule : e === "sort" ? this.sortModule : this.filterModule;
113
+ }
114
+ getModuleInitOptions(e, t, n) {
115
+ let r = {};
116
+ for (let a of [e, t]) {
117
+ let e = n[a];
118
+ i(e) && Object.assign(r, e);
119
+ }
120
+ return r;
121
+ }
122
+ validateFilterByPreviousResultOptions(e) {
123
+ if (i(e) && Object.prototype.hasOwnProperty.call(e, "filterByPreviousResult")) throw h.invalidFilterByPreviousResultOption();
124
+ }
125
+ isPreviousResultEnabled() {
126
+ return this.state.isFilterByPreviousResultEnabled();
127
+ }
128
+ getPreviousResultInput() {
129
+ return this.isPreviousResultEnabled() ? this.state.getPreviousResult() : null;
130
+ }
131
+ trackPreviousResult(e, t) {
132
+ return this.isPreviousResultEnabled() && this.state.setPreviousResult(e, t), e;
133
+ }
134
+ clearOperationState(e) {
135
+ (!e || e === "search") && (this.previousSearchState = null), (!e || e === "filter") && (this.previousFilterState = null), (!e || e === "sort") && (this.previousSortState = null);
136
+ }
137
+ createSearchCacheKey(e, t) {
138
+ return JSON.stringify([e, t ?? null]);
139
+ }
140
+ createSortCacheKey(e, t) {
141
+ return JSON.stringify({
142
+ descriptors: e,
143
+ inPlace: t ?? !1
144
+ });
145
+ }
146
+ createFilterCacheKey(e) {
147
+ return JSON.stringify(e);
148
+ }
149
+ handleStateMutation(e) {
150
+ switch (e.type) {
151
+ case "add":
152
+ this.queueSearchCacheMutation(e), this.queueFilterCacheMutation(e), this.queueSortCacheMutation(e);
153
+ return;
154
+ case "update":
155
+ this.queueSearchCacheMutation(e), this.queueFilterCacheMutation(e), this.queueSortCacheMutation(e);
156
+ return;
157
+ case "remove":
158
+ this.previousSearchState = null, this.previousFilterState = null, this.queueSortCacheMutation(e);
159
+ return;
160
+ case "removeMany":
161
+ this.previousSearchState = null, this.previousFilterState = null, this.queueSortCacheMutation(e);
162
+ return;
163
+ case "data":
164
+ case "clearData":
165
+ this.previousSearchState = null, this.previousFilterState = null, this.previousSortState = null;
166
+ return;
167
+ }
168
+ }
169
+ queueSearchCacheMutation(e) {
170
+ let t = this.previousSearchState;
171
+ if (t !== null) {
172
+ if (!this.canPatchStoredDatasetSearch(t)) {
173
+ this.previousSearchState = null;
174
+ return;
175
+ }
176
+ this.previousSearchState = {
177
+ ...t,
178
+ version: this.state.getMutationVersion(),
179
+ pendingMutations: t.pendingMutations.concat(e)
180
+ };
181
+ }
182
+ }
183
+ queueFilterCacheMutation(e) {
184
+ let t = this.previousFilterState;
185
+ if (t !== null) {
186
+ if (!this.canPatchStoredDatasetFilter(t)) {
187
+ this.previousFilterState = null;
188
+ return;
189
+ }
190
+ this.previousFilterState = {
191
+ ...t,
192
+ version: this.state.getMutationVersion(),
193
+ pendingMutations: t.pendingMutations.concat(e)
194
+ };
195
+ }
196
+ }
197
+ queueSortCacheMutation(e) {
198
+ let t = this.previousSortState;
199
+ if (t !== null) {
200
+ if (!this.canPatchStoredDatasetSort(t)) {
201
+ this.previousSortState = null;
202
+ return;
203
+ }
204
+ this.previousSortState = {
205
+ ...t,
206
+ version: this.state.getMutationVersion(),
207
+ pendingMutations: t.pendingMutations.concat(e)
208
+ };
209
+ }
210
+ }
211
+ resolvePendingSortCache(e) {
212
+ if (e.pendingMutations.length === 0) return e;
213
+ let t = {
214
+ ...e,
215
+ pendingMutations: []
216
+ };
217
+ for (let n = 0; n < e.pendingMutations.length; n++) if (t = this.applySortCacheMutation(t, e.pendingMutations[n]), t === null) return null;
218
+ return t;
219
+ }
220
+ resolvePendingSearchCache(e) {
221
+ if (e.pendingMutations.length === 0) return e;
222
+ let t = {
223
+ ...e,
224
+ pendingMutations: []
225
+ };
226
+ for (let n = 0; n < e.pendingMutations.length; n++) if (t = this.applySearchCacheMutation(t, e.pendingMutations[n]), t === null) return null;
227
+ return t;
228
+ }
229
+ resolvePendingFilterCache(e) {
230
+ if (e.pendingMutations.length === 0) return e;
231
+ let t = {
232
+ ...e,
233
+ pendingMutations: []
234
+ };
235
+ for (let n = 0; n < e.pendingMutations.length; n++) if (t = this.applyFilterCacheMutation(t, e.pendingMutations[n]), t === null) return null;
236
+ return t;
237
+ }
238
+ applySortCacheMutation(e, t) {
239
+ switch (t.type) {
240
+ case "add": return this.patchSortCacheForAddedItems(e, t.items);
241
+ case "update": return this.patchSortCacheForUpdatedItem(e, t.previousItem, t.nextItem);
242
+ case "remove": return this.patchSortCacheForRemovedItems(e, [t.removedItem]);
243
+ case "removeMany": return this.patchSortCacheForRemovedItems(e, t.entries.map((e) => e.removedItem));
244
+ case "data":
245
+ case "clearData": return null;
246
+ }
247
+ }
248
+ applySearchCacheMutation(e, t) {
249
+ switch (t.type) {
250
+ case "add": return this.patchSearchCacheForAddedItems(e, t.items);
251
+ case "update": return this.patchSearchCacheForUpdatedItem(e, t.previousItem, t.nextItem);
252
+ case "remove":
253
+ case "removeMany":
254
+ case "data":
255
+ case "clearData": return null;
256
+ }
257
+ }
258
+ applyFilterCacheMutation(e, t) {
259
+ switch (t.type) {
260
+ case "add": return this.patchFilterCacheForAddedItems(e, t.items);
261
+ case "update": return this.patchFilterCacheForUpdatedItem(e, t.previousItem, t.nextItem);
262
+ case "remove":
263
+ case "removeMany":
264
+ case "data":
265
+ case "clearData": return null;
266
+ }
267
+ }
268
+ canPatchStoredDatasetSearch(e) {
269
+ return e.originData === this.state.getOriginData() && e.field !== null;
270
+ }
271
+ canPatchStoredDatasetFilter(e) {
272
+ if (e.sourceData !== this.state.getOriginData()) return !1;
273
+ for (let t = 0; t < e.criteria.length; t++) if (!this.isPatchableFilterCriterion(e.criteria[t])) return !1;
274
+ return !0;
275
+ }
276
+ canPatchStoredDatasetSort(e) {
277
+ return e.sourceData === this.state.getOriginData();
278
+ }
279
+ compareItemsByDescriptors(e, t, n) {
280
+ for (let r = 0; r < n.length; r++) {
281
+ let { field: i, direction: a } = n[r], o = e[i], s = t[i];
282
+ if (o < s) return a === "asc" ? -1 : 1;
283
+ if (o > s) return a === "asc" ? 1 : -1;
284
+ }
285
+ return (this.state.getItemIndex(e) ?? -1) - (this.state.getItemIndex(t) ?? -1);
286
+ }
287
+ findSortInsertPosition(e, t, n) {
288
+ let r = 0, i = e.length;
289
+ for (; r < i;) {
290
+ let a = r + i >> 1;
291
+ this.compareItemsByDescriptors(e[a], t, n) <= 0 ? r = a + 1 : i = a;
292
+ }
293
+ return r;
294
+ }
295
+ findDatasetInsertPosition(e, t) {
296
+ let n = this.state.getItemIndex(t) ?? 2 ** 53 - 1;
297
+ for (let t = 0; t < e.length; t++) if ((this.state.getItemIndex(e[t]) ?? 2 ** 53 - 1) > n) return t;
298
+ return e.length;
299
+ }
300
+ doesItemMatchSearchCache(e, t) {
301
+ if (e.field === null || e.lowerQuery.length === 0) return !1;
302
+ let n = t[e.field];
303
+ return typeof n == "string" && n.toLowerCase().includes(e.lowerQuery);
304
+ }
305
+ patchSearchCacheForAddedItems(e, t) {
306
+ if (!this.canPatchStoredDatasetSearch(e)) return null;
307
+ let n = e.result.slice();
308
+ for (let r = 0; r < t.length; r++) {
309
+ let i = t[r];
310
+ this.doesItemMatchSearchCache(e, i) && n.push(i);
311
+ }
312
+ return {
313
+ ...e,
314
+ result: n,
315
+ pendingMutations: [],
316
+ version: this.state.getMutationVersion()
317
+ };
318
+ }
319
+ patchSearchCacheForUpdatedItem(e, t, n) {
320
+ if (!this.canPatchStoredDatasetSearch(e)) return null;
321
+ let r = e.result.slice(), i = r.indexOf(t), a = this.doesItemMatchSearchCache(e, n);
322
+ if (i !== -1) a ? r[i] = n : r.splice(i, 1);
323
+ else if (a) {
324
+ let e = this.findDatasetInsertPosition(r, n);
325
+ r.splice(e, 0, n);
326
+ }
327
+ return {
328
+ ...e,
329
+ result: r,
330
+ pendingMutations: [],
331
+ version: this.state.getMutationVersion()
332
+ };
333
+ }
334
+ isPatchableFilterCriterion(e) {
335
+ return !String(e.field).includes(".");
336
+ }
337
+ doesItemMatchFilterCache(e, t) {
338
+ for (let n = 0; n < e.criteria.length; n++) {
339
+ let r = e.criteria[n];
340
+ if (!this.isPatchableFilterCriterion(r)) return !1;
341
+ let i = t[r.field];
342
+ if (r.values !== void 0 && r.values.length > 0 && !r.values.includes(i) || r.exclude !== void 0 && r.exclude.length > 0 && r.exclude.includes(i)) return !1;
343
+ }
344
+ return !0;
345
+ }
346
+ patchFilterCacheForAddedItems(e, t) {
347
+ if (!this.canPatchStoredDatasetFilter(e)) return null;
348
+ let n = e.result.slice();
349
+ for (let r = 0; r < t.length; r++) {
350
+ let i = t[r];
351
+ this.doesItemMatchFilterCache(e, i) && n.push(i);
352
+ }
353
+ return {
354
+ ...e,
355
+ result: n,
356
+ pendingMutations: [],
357
+ version: this.state.getMutationVersion()
358
+ };
359
+ }
360
+ patchFilterCacheForUpdatedItem(e, t, n) {
361
+ if (!this.canPatchStoredDatasetFilter(e)) return null;
362
+ let r = e.result.slice(), i = r.indexOf(t), a = this.doesItemMatchFilterCache(e, n);
363
+ if (i !== -1) a ? r[i] = n : r.splice(i, 1);
364
+ else if (a) {
365
+ let e = this.findDatasetInsertPosition(r, n);
366
+ r.splice(e, 0, n);
367
+ }
368
+ return {
369
+ ...e,
370
+ result: r,
371
+ pendingMutations: [],
372
+ version: this.state.getMutationVersion()
373
+ };
374
+ }
375
+ patchSortCacheForAddedItems(e, t) {
376
+ if (!this.canPatchStoredDatasetSort(e)) return null;
377
+ let n = e.result.slice();
378
+ for (let r = 0; r < t.length; r++) {
379
+ let i = t[r], a = this.findSortInsertPosition(n, i, e.descriptors);
380
+ n.splice(a, 0, i);
381
+ }
382
+ return {
383
+ ...e,
384
+ result: n,
385
+ pendingMutations: [],
386
+ version: this.state.getMutationVersion()
387
+ };
388
+ }
389
+ patchSortCacheForUpdatedItem(e, t, n) {
390
+ if (!this.canPatchStoredDatasetSort(e)) return null;
391
+ let r = e.result.slice(), i = r.indexOf(t);
392
+ if (i === -1) return e.result.indexOf(n) === -1 ? null : {
393
+ ...e,
394
+ pendingMutations: [],
395
+ version: this.state.getMutationVersion()
396
+ };
397
+ if (!e.descriptors.some(({ field: e }) => t[e] !== n[e])) return r[i] = n, {
398
+ ...e,
399
+ result: r,
400
+ pendingMutations: [],
401
+ version: this.state.getMutationVersion()
402
+ };
403
+ r.splice(i, 1);
404
+ let a = this.findSortInsertPosition(r, n, e.descriptors);
405
+ return r.splice(a, 0, n), {
406
+ ...e,
407
+ result: r,
408
+ pendingMutations: [],
409
+ version: this.state.getMutationVersion()
410
+ };
411
+ }
412
+ patchSortCacheForRemovedItems(e, t) {
413
+ if (!this.canPatchStoredDatasetSort(e)) return null;
414
+ let n = new Set(t);
415
+ return {
416
+ ...e,
417
+ result: Array.prototype.filter.call(e.result, (e) => !n.has(e)),
418
+ pendingMutations: [],
419
+ version: this.state.getMutationVersion()
420
+ };
421
+ }
422
+ search(e, t) {
423
+ if (!this.searchModule) throw h.unavailableEngine("search");
424
+ let n = this.state.getOriginData(), r = this.state.getMutationVersion(), i = this.createSearchCacheKey(e, t), a = this.previousSearchState;
425
+ if (a?.originData === n && a.key === i && a.version === r) {
426
+ let e = this.resolvePendingSearchCache(a);
427
+ if (e !== null) return this.previousSearchState = e, this.withChain(this.trackPreviousResult(e.result, n));
428
+ this.previousSearchState = null;
429
+ }
430
+ let o = t === void 0 ? this.searchModule.executeSearch(e) : this.searchModule.executeSearch(e, t);
431
+ return this.previousSearchState = {
432
+ key: i,
433
+ originData: n,
434
+ result: o,
435
+ version: r,
436
+ field: t === void 0 ? null : e,
437
+ lowerQuery: (t ?? e).trim().toLowerCase(),
438
+ pendingMutations: []
439
+ }, this.withChain(this.trackPreviousResult(o, n));
440
+ }
441
+ sort(e, t, n) {
442
+ if (!this.sortModule) throw h.unavailableEngine("sort");
443
+ let r, i;
444
+ if (t === void 0 ? (i = e, r = this.getPreviousResultInput() ?? this.state.getOriginData()) : (r = e, i = t), !n) {
445
+ let e = this.state.getMutationVersion(), a = this.createSortCacheKey(i, n), o = this.previousSortState;
446
+ if (o?.sourceData === r && o.key === a && o.version === e) {
447
+ let e = this.resolvePendingSortCache(o);
448
+ if (e !== null) return this.previousSortState = e, this.withChain(this.trackPreviousResult(e.result, r));
449
+ this.previousSortState = null;
450
+ }
451
+ let s = t === void 0 && r === this.state.getOriginData() ? this.sortModule.executeSort(i) : this.sortModule.executeSort(r, i, n);
452
+ return this.previousSortState = {
453
+ key: a,
454
+ sourceData: r,
455
+ result: s,
456
+ version: e,
457
+ descriptors: i,
458
+ pendingMutations: []
459
+ }, this.withChain(this.trackPreviousResult(s, r));
460
+ }
461
+ return this.withChain(this.trackPreviousResult(this.sortModule.executeSort(r, i, n), r));
462
+ }
463
+ filter(e, t) {
464
+ if (!this.filterModule) throw h.unavailableEngine("filter");
465
+ if (t === void 0) {
466
+ let t = this.getPreviousResultInput(), n = t ?? this.state.getOriginData(), r = e, i = this.state.getMutationVersion(), a = this.createFilterCacheKey(r), o = this.previousFilterState;
467
+ if (o?.sourceData === n && o.key === a && o.version === i) {
468
+ let e = this.resolvePendingFilterCache(o);
469
+ if (e !== null) return this.previousFilterState = e, this.withChain(this.trackPreviousResult(e.result, n));
470
+ this.previousFilterState = null;
471
+ }
472
+ let s = t === null ? this.filterModule.executeFilter(r) : this.filterModule.executeFilter(t, r);
473
+ return this.previousFilterState = {
474
+ key: a,
475
+ sourceData: n,
476
+ result: s,
477
+ version: i,
478
+ criteria: r,
479
+ pendingMutations: []
480
+ }, this.withChain(this.trackPreviousResult(s, n));
481
+ }
482
+ let n = e;
483
+ return this.withChain(this.trackPreviousResult(this.filterModule.executeFilter(n, t), n));
484
+ }
485
+ withChain(e) {
486
+ return this.chainBuilder.create(e);
487
+ }
488
+ getOriginData() {
489
+ if (this.searchModule || this.sortModule || this.filterModule) return this.state.getOriginData();
490
+ throw h.unavailableGetOriginData();
491
+ }
492
+ add(e) {
493
+ return e.length === 0 || this.state.add(e), this;
494
+ }
495
+ update(e) {
496
+ return this.state.update(e), this;
497
+ }
498
+ clearIndexes(e) {
499
+ let t = this.getAdapter(e);
500
+ if (t) return t.clearIndexes(), this.clearOperationState(e), this;
501
+ throw h.unavailableEngine(e);
502
+ }
503
+ data(e) {
504
+ return this.state.data(e), this;
505
+ }
506
+ clearData(e) {
507
+ if (this.getAdapter(e)) return this.state.clearData(), this;
508
+ throw h.unavailableEngine(e);
509
+ }
510
+ };
511
+ //#endregion
512
+ export { g as t };