@devisfuture/mega-collection 1.2.0 → 1.3.0

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
@@ -124,6 +124,9 @@ engine.data([
124
124
  // clear indexes/data for one module
125
125
  engine.clearIndexes("search").clearIndexes("sort").clearIndexes("filter");
126
126
  engine.clearData("search").clearData("sort").clearData("filter");
127
+
128
+ // get original dataset from a specific module
129
+ engine.getOriginData("search"); // "search" | "sort" | "filter"
127
130
  ```
128
131
 
129
132
  ---
@@ -155,6 +158,9 @@ engine.search("name", "john"); // searches a specific field
155
158
  // replace dataset without re-initializing
156
159
  engine.data(users);
157
160
 
161
+ // access original dataset stored in the engine
162
+ engine.getOriginData();
163
+
158
164
  // chain support
159
165
  engine.search("john").clearIndexes().clearData();
160
166
  ```
@@ -182,6 +188,9 @@ engine.filter([
182
188
  // replace dataset without re-initializing
183
189
  engine.data(users);
184
190
 
191
+ // access original dataset stored in the engine
192
+ engine.getOriginData();
193
+
185
194
  engine
186
195
  .filter([{ field: "city", values: ["Miami", "New York"] }])
187
196
  .filter([{ field: "age", values: [25, 30, 35] }])
@@ -217,6 +226,9 @@ engine.sort([{ field: "age", direction: "asc" }]);
217
226
  // replace dataset without re-initializing
218
227
  engine.data(users);
219
228
 
229
+ // access original dataset stored in the engine
230
+ engine.getOriginData();
231
+
220
232
  // chain support
221
233
  engine
222
234
  .sort([{ field: "age", direction: "asc" }])
@@ -251,17 +263,18 @@ Unified facade that composes all three engines around a shared dataset.
251
263
 
252
264
  **Methods:**
253
265
 
254
- | Method | Description |
255
- | ----------------------------------- | ------------------------------------------------------------------- |
256
- | `search(query)` | Search all indexed fields |
257
- | `search(field, query)` | Search a specific field |
258
- | `sort(descriptors)` | Sort using stored dataset |
259
- | `sort(data, descriptors, inPlace?)` | Sort with an explicit dataset |
260
- | `filter(criteria)` | Filter using stored dataset |
261
- | `filter(data, criteria)` | Filter with an explicit dataset |
262
- | `data(data)` | Replace stored dataset for all imported modules |
263
- | `clearIndexes(module)` | Clear indexes for one module (`"search"`, `"sort"`, `"filter"`) |
264
- | `clearData(module)` | Clear stored data for one module (`"search"`, `"sort"`, `"filter"`) |
266
+ | Method | Description |
267
+ | ----------------------------------- | -------------------------------------------------------------------------------------------------------------------------- |
268
+ | `search(query)` | Search all indexed fields |
269
+ | `search(field, query)` | Search a specific field |
270
+ | `sort(descriptors)` | Sort using stored dataset |
271
+ | `sort(data, descriptors, inPlace?)` | Sort with an explicit dataset |
272
+ | `filter(criteria)` | Filter using stored dataset |
273
+ | `filter(data, criteria)` | Filter with an explicit dataset |
274
+ | `getOriginData(module)` | Get the original dataset from one module (`"search"`, `"sort"`, `"filter"`) |
275
+ | `data(data)` | Replace stored dataset for all imported modules, rebuilding configured indexes and resetting filter state where applicable |
276
+ | `clearIndexes(module)` | Clear indexes for one module (`"search"`, `"sort"`, `"filter"`) |
277
+ | `clearData(module)` | Clear stored data for one module (`"search"`, `"sort"`, `"filter"`) |
265
278
 
266
279
  ---
267
280
 
@@ -269,13 +282,14 @@ Unified facade that composes all three engines around a shared dataset.
269
282
 
270
283
  Trigram-based text search engine.
271
284
 
272
- | Method | Description |
273
- | ---------------------- | --------------------------------------- |
274
- | `search(query)` | Search all indexed fields, deduplicated |
275
- | `search(field, query)` | Search a specific indexed field |
276
- | `data(data)` | Replace stored dataset |
277
- | `clearIndexes()` | Clear n-gram indexes |
278
- | `clearData()` | Clear stored data |
285
+ | Method | Description |
286
+ | ---------------------- | ----------------------------------------------------- |
287
+ | `search(query)` | Search all indexed fields, deduplicated |
288
+ | `search(field, query)` | Search a specific indexed field |
289
+ | `getOriginData()` | Get the original stored dataset |
290
+ | `data(data)` | Replace stored dataset and rebuild configured indexes |
291
+ | `clearIndexes()` | Clear n-gram indexes |
292
+ | `clearData()` | Clear stored data |
279
293
 
280
294
  ### `FilterEngine<T>` (filter module)
281
295
 
@@ -287,29 +301,33 @@ Constructor option highlights:
287
301
  | ------------------------ | --------- | ----------------------------------------------------------------------------------------------------------------------------------------- |
288
302
  | `filterByPreviousResult` | `boolean` | When `true`, each `filter(criteria)` call filters from previous result. Defaults to `false` (each call starts from the original dataset). |
289
303
 
290
- | Method | Description |
291
- | ------------------------ | ---------------------------------------------------- |
292
- | `filter(criteria)` | Filter using stored dataset |
293
- | `filter(data, criteria)` | Filter with an explicit dataset |
294
- | `data(data)` | Replace stored dataset |
295
- | `resetFilterState()` | Reset previous-result state for sequential filtering |
296
- | `clearIndexes()` | Free all index memory |
297
- | `clearData()` | Clear stored data |
304
+ | Method | Description |
305
+ | ------------------------ | -------------------------------------------------------------------------- |
306
+ | `filter(criteria)` | Filter using stored dataset |
307
+ | `filter(data, criteria)` | Filter with an explicit dataset |
308
+ | `getOriginData()` | Get the original stored dataset |
309
+ | `data(data)` | Replace stored dataset, rebuild configured indexes, and reset filter state |
310
+ | `resetFilterState()` | Reset previous-result state for sequential filtering |
311
+ | `clearIndexes()` | Free all index memory |
312
+ | `clearData()` | Clear stored data |
298
313
 
299
314
  ### `SortEngine<T>` (sort module)
300
315
 
301
316
  Sorting with pre-compiled comparators and cached sort indexes.
302
317
 
303
- | Method | Description |
304
- | ----------------------------------- | ----------------------------- |
305
- | `sort(descriptors)` | Sort using stored dataset |
306
- | `sort(data, descriptors, inPlace?)` | Sort with an explicit dataset |
307
- | `data(data)` | Replace stored dataset |
308
- | `clearIndexes()` | Free all cached indexes |
309
- | `clearData()` | Clear stored data |
318
+ | Method | Description |
319
+ | ----------------------------------- | ----------------------------------------------------- |
320
+ | `sort(descriptors)` | Sort using stored dataset |
321
+ | `sort(data, descriptors, inPlace?)` | Sort with an explicit dataset |
322
+ | `getOriginData()` | Get the original stored dataset |
323
+ | `data(data)` | Replace stored dataset and rebuild configured indexes |
324
+ | `clearIndexes()` | Free all cached indexes |
325
+ | `clearData()` | Clear stored data |
310
326
 
311
327
  ---
312
328
 
329
+ **Note on `data` method:** Calling the `data` method automatically rebuilds configured indexes and resets any internal state (such as filter state in FilterEngine), so it is sufficient on its own without needing to call `clearIndexes` separately.
330
+
313
331
  ## Types
314
332
 
315
333
  All types are exported from the root package and from each sub-module:
@@ -1 +1 @@
1
- var y=3,T=12;function I(d){let e=d.toLowerCase(),n=Math.min(y,e.length),t=e.length-n+1,r=new Array(t);for(let i=0;i<t;i++)r[i]=e.substring(i,i+n);return r}function m(d){let e=I(d);if(e.length<=T)return new Set(e);let n=new Set,t=e.length-1,r=T-1;for(let i=0;i<=r;i++){let s=Math.round(i*t/r);n.add(e[s]);}return n}function b(d,e){let n=d.get(e);if(n)return n;let t=new Set;return d.set(e,t),t}var x=class{constructor(e={}){this.ngramIndexes=new Map;this.dataset=[];this.indexedFields=new Set;if(this.minQueryLength=e.minQueryLength??1,!!e.data&&(this.dataset=e.data,e.fields?.length)){for(let n of e.fields)this.indexedFields.add(n);this.rebuildConfiguredIndexes();}}rebuildConfiguredIndexes(){this.ngramIndexes.clear();for(let e of this.indexedFields)this.buildIndex(this.dataset,e);}buildIndex(e,n){let t,r;if(Array.isArray(e))t=e,r=n;else {if(!this.dataset.length)throw new Error("TextSearchEngine: no dataset in memory. Either pass `data` in the constructor options, or call buildIndex(data, field).");t=this.dataset,r=e;}this.dataset=t;let i=new Map;for(let s=0,c=t.length;s<c;s++){let o=t[s][r];if(typeof o!="string")continue;let h=o.toLowerCase();for(let a=0,l=h.length;a<l;a++){let g=l-a,u=Math.min(y,g);for(let f=1;f<=u;f++){let p=h.substring(a,a+f);b(i,p).add(s);}}}return this.ngramIndexes.set(r,i),this}search(e,n){return n===void 0?this.withChain(this.searchAllFields(e)):this.withChain(this.searchField(e,n))}normalizeQuery(e){return e.trim().toLowerCase()}searchAllFields(e){let n=[...this.ngramIndexes.keys()],t=this.normalizeQuery(e);if(!t)return this.dataset;if(t.length<this.minQueryLength)return this.dataset;if(!n.length)return this.searchAllFieldsLinear(t);let r=m(t);if(!r.size)return [];let i=new Set,s=[];for(let c of n)for(let o of this.searchFieldWithPreparedQuery(c,t,r))i.has(o)||(i.add(o),s.push(o));return s}searchField(e,n){let t=this.normalizeQuery(n);if(!t)return this.dataset;if(t.length<this.minQueryLength)return this.dataset;if(!this.ngramIndexes.size)return this.searchFieldLinear(e,t);let r=m(t);return r.size?this.searchFieldWithPreparedQuery(e,t,r):[]}searchFieldWithPreparedQuery(e,n,t){let r=this.ngramIndexes.get(e);if(!r)return [];let i=[];for(let h of t){let a=r.get(h);if(!a)return [];i.push(a);}i.sort((h,a)=>h.size-a.size);let s=i[0],c=i.length,o=[];for(let h of s){let a=true;for(let u=1;u<c;u++)if(!i[u].has(h)){a=false;break}if(!a)continue;let l=this.dataset[h];if(!l)continue;let g=l[e];typeof g=="string"&&g.toLowerCase().includes(n)&&o.push(l);}return o}searchAllFieldsLinear(e){if(!this.dataset.length)return [];let n=[];for(let t=0;t<this.dataset.length;t++){let r=this.dataset[t],i=false;for(let s of Object.values(r))if(typeof s=="string"&&s.toLowerCase().includes(e)){i=true;break}i&&n.push(r);}return n}searchFieldLinear(e,n){if(!this.dataset.length)return [];let t=[];for(let r=0;r<this.dataset.length;r++){let i=this.dataset[r][e];typeof i=="string"&&i.toLowerCase().includes(n)&&t.push(this.dataset[r]);}return t}withChain(e){let n=e;return Object.defineProperty(n,"search",{value:(t,r)=>r===void 0?this.search(t):this.search(t,r),enumerable:false,configurable:true,writable:true}),Object.defineProperty(n,"clearIndexes",{value:()=>this.clearIndexes(),enumerable:false,configurable:true,writable:true}),Object.defineProperty(n,"data",{value:t=>this.data(t),enumerable:false,configurable:true,writable:true}),Object.defineProperty(n,"clearData",{value:()=>this.clearData(),enumerable:false,configurable:true,writable:true}),n}clearIndexes(){return this.ngramIndexes.clear(),this}data(e){return this.dataset=e,this.rebuildConfiguredIndexes(),this}clearData(){return this.dataset=[],this.ngramIndexes.clear(),this}};export{x as a};
1
+ var y=3,T=12;function I(l){let e=l.toLowerCase(),n=Math.min(y,e.length),t=e.length-n+1,r=new Array(t);for(let i=0;i<t;i++)r[i]=e.substring(i,i+n);return r}function m(l){let e=I(l);if(e.length<=T)return new Set(e);let n=new Set,t=e.length-1,r=T-1;for(let i=0;i<=r;i++){let s=Math.round(i*t/r);n.add(e[s]);}return n}function b(l,e){let n=l.get(e);if(n)return n;let t=new Set;return l.set(e,t),t}var x=class{constructor(e={}){this.ngramIndexes=new Map;this.dataset=[];this.indexedFields=new Set;if(this.minQueryLength=e.minQueryLength??1,!!e.data&&(this.dataset=e.data,e.fields?.length)){for(let n of e.fields)this.indexedFields.add(n);this.rebuildConfiguredIndexes();}}rebuildConfiguredIndexes(){this.ngramIndexes.clear();for(let e of this.indexedFields)this.buildIndex(this.dataset,e);}buildIndex(e,n){let t,r;if(Array.isArray(e))t=e,r=n;else {if(!this.dataset.length)throw new Error("TextSearchEngine: no dataset in memory. Either pass `data` in the constructor options, or call buildIndex(data, field).");t=this.dataset,r=e;}this.dataset=t;let i=new Map;for(let s=0,c=t.length;s<c;s++){let o=t[s][r];if(typeof o!="string")continue;let h=o.toLowerCase();for(let a=0,d=h.length;a<d;a++){let g=d-a,u=Math.min(y,g);for(let f=1;f<=u;f++){let p=h.substring(a,a+f);b(i,p).add(s);}}}return this.ngramIndexes.set(r,i),this}search(e,n){return n===void 0?this.withChain(this.searchAllFields(e)):this.withChain(this.searchField(e,n))}normalizeQuery(e){return e.trim().toLowerCase()}searchAllFields(e){let n=[...this.ngramIndexes.keys()],t=this.normalizeQuery(e);if(!t)return this.dataset;if(t.length<this.minQueryLength)return this.dataset;if(!n.length)return this.searchAllFieldsLinear(t);let r=m(t);if(!r.size)return [];let i=new Set,s=[];for(let c of n)for(let o of this.searchFieldWithPreparedQuery(c,t,r))i.has(o)||(i.add(o),s.push(o));return s}searchField(e,n){let t=this.normalizeQuery(n);if(!t)return this.dataset;if(t.length<this.minQueryLength)return this.dataset;if(!this.ngramIndexes.size)return this.searchFieldLinear(e,t);let r=m(t);return r.size?this.searchFieldWithPreparedQuery(e,t,r):[]}searchFieldWithPreparedQuery(e,n,t){let r=this.ngramIndexes.get(e);if(!r)return [];let i=[];for(let h of t){let a=r.get(h);if(!a)return [];i.push(a);}i.sort((h,a)=>h.size-a.size);let s=i[0],c=i.length,o=[];for(let h of s){let a=true;for(let u=1;u<c;u++)if(!i[u].has(h)){a=false;break}if(!a)continue;let d=this.dataset[h];if(!d)continue;let g=d[e];typeof g=="string"&&g.toLowerCase().includes(n)&&o.push(d);}return o}searchAllFieldsLinear(e){if(!this.dataset.length)return [];let n=[];for(let t=0;t<this.dataset.length;t++){let r=this.dataset[t],i=false;for(let s of Object.values(r))if(typeof s=="string"&&s.toLowerCase().includes(e)){i=true;break}i&&n.push(r);}return n}searchFieldLinear(e,n){if(!this.dataset.length)return [];let t=[];for(let r=0;r<this.dataset.length;r++){let i=this.dataset[r][e];typeof i=="string"&&i.toLowerCase().includes(n)&&t.push(this.dataset[r]);}return t}withChain(e){let n=e;return Object.defineProperty(n,"search",{value:(t,r)=>r===void 0?this.search(t):this.search(t,r),enumerable:false,configurable:true,writable:true}),Object.defineProperty(n,"clearIndexes",{value:()=>this.clearIndexes(),enumerable:false,configurable:true,writable:true}),Object.defineProperty(n,"getOriginData",{value:()=>this.getOriginData(),enumerable:false,configurable:true,writable:true}),Object.defineProperty(n,"data",{value:t=>this.data(t),enumerable:false,configurable:true,writable:true}),Object.defineProperty(n,"clearData",{value:()=>this.clearData(),enumerable:false,configurable:true,writable:true}),n}clearIndexes(){return this.ngramIndexes.clear(),this}getOriginData(){return this.dataset}data(e){return this.dataset=e,this.rebuildConfiguredIndexes(),this}clearData(){return this.dataset=[],this.ngramIndexes.clear(),this}};export{x as a};
@@ -0,0 +1 @@
1
+ var T=class{constructor(e){let{imports:n,data:t,...r}=e,a=new Set(n),s={},l={},c={},d={},u={};for(let M of a){let f=M.prototype,g=this.getMethodNames(f);if(g.length===0)continue;let p=this.getModuleInitOptions(M.name,g,r),i=new M({data:t,...p}),o=this.getModuleName(g);o&&this.hasMethod(i,"clearIndexes")&&!l[o]&&(l[o]=i.clearIndexes.bind(i)),o&&this.hasMethod(i,"clearData")&&!c[o]&&(c[o]=i.clearData.bind(i)),o&&this.hasMethod(i,"data")&&!d[o]&&(d[o]=i.data.bind(i)),o&&this.hasMethod(i,"getOriginData")&&!u[o]&&(u[o]=i.getOriginData.bind(i));for(let h of g)s[h]||this.hasMethod(i,h)&&(s[h]=i[h].bind(i));}this.engine=Object.keys(s).length>0?s:null,this.clearIndexMethods=l,this.clearDataMethods=c,this.setDataMethods=d,this.getOriginDataMethods=u;}getModuleName(e){return e.includes("search")?"search":e.includes("sort")?"sort":e.includes("filter")?"filter":null}getModuleInitOptions(e,n,t){let r={},a=t[e];this.isRecord(a)&&Object.assign(r,a);for(let s of n){let l=t[s];this.isRecord(l)&&Object.assign(r,l);}return r}getMethodNames(e){let n=e;return Object.getOwnPropertyNames(n).filter(t=>t==="constructor"?false:typeof n[t]=="function")}hasMethod(e,n){return typeof e=="object"&&e!==null&&typeof e[n]=="function"}isRecord(e){return typeof e=="object"&&e!==null}callEngineMethod(e,n){let t=this.engine?.[e];if(!t)throw new Error(`MergeEngines: Method "${e}" is not available. Add module with method "${e}" to the \`imports\` array.`);return t(...n)}search(e,n){if(!this.engine?.search)throw new Error("MergeEngines: TextSearchEngine is not available. Add TextSearchEngine to the `imports` array.");return n===void 0?this.withChain(this.callEngineMethod("search",[e])):this.withChain(this.callEngineMethod("search",[e,n]))}sort(e,n,t){if(!this.engine?.sort)throw new Error("MergeEngines: SortEngine is not available. Add SortEngine to the `imports` array.");return n===void 0?this.withChain(this.callEngineMethod("sort",[e])):this.withChain(this.callEngineMethod("sort",[e,n,t]))}filter(e,n){if(!this.engine?.filter)throw new Error("MergeEngines: FilterEngine is not available. Add FilterEngine to the `imports` array.");return n===void 0?this.withChain(this.callEngineMethod("filter",[e])):this.withChain(this.callEngineMethod("filter",[e,n]))}withChain(e){let n=e;return Object.defineProperty(n,"search",{value:(t,r)=>r===void 0?this.search(t):this.search(t,r),enumerable:false,configurable:true,writable:true}),Object.defineProperty(n,"sort",{value:(t,r,a)=>r===void 0?this.sort(e,t,a):this.sort(t,r,a),enumerable:false,configurable:true,writable:true}),Object.defineProperty(n,"filter",{value:(t,r)=>r===void 0?this.filter(e,t):this.filter(t,r),enumerable:false,configurable:true,writable:true}),Object.defineProperty(n,"clearIndexes",{value:t=>(this.clearIndexes(t),this.withChain(e)),enumerable:false,configurable:true,writable:true}),Object.defineProperty(n,"clearData",{value:t=>(this.clearData(t),this.withChain(e)),enumerable:false,configurable:true,writable:true}),Object.defineProperty(n,"data",{value:t=>this.data(t),enumerable:false,configurable:true,writable:true}),Object.defineProperty(n,"getOriginData",{value:t=>this.getOriginData(t),enumerable:false,configurable:true,writable:true}),n}getOriginData(e){let n=this.getOriginDataMethods[e];if(n)return n();let t={search:"TextSearchEngine",sort:"SortEngine",filter:"FilterEngine"};throw new Error(`MergeEngines: ${t[e]} is not available. Add ${t[e]} to the \`imports\` array.`)}clearIndexes(e){let n=this.clearIndexMethods[e];if(n)return n(),this;let t={search:"TextSearchEngine",sort:"SortEngine",filter:"FilterEngine"};throw new Error(`MergeEngines: ${t[e]} is not available. Add ${t[e]} to the \`imports\` array.`)}data(e){let n=["search","sort","filter"];for(let t of n){let r=this.setDataMethods[t];r&&r(e);}return this}clearData(e){let n=this.clearDataMethods[e];if(n)return n(),this;let t={search:"TextSearchEngine",sort:"SortEngine",filter:"FilterEngine"};throw new Error(`MergeEngines: ${t[e]} is not available. Add ${t[e]} to the \`imports\` array.`)}};export{T as a};
@@ -0,0 +1 @@
1
+ var u=class{constructor(t={}){this.cache=new Map;this.dataset=[];this.indexedFields=new Set;if(t.data&&(this.dataset=t.data,t.fields?.length)){for(let s of t.fields)this.indexedFields.add(s);this.rebuildConfiguredIndexes();}}rebuildConfiguredIndexes(){this.cache.clear();for(let t of this.indexedFields)this.buildIndex(this.dataset,t);}buildIndex(t,s){let a,e;if(Array.isArray(t))a=t,e=s;else {if(!this.dataset.length)throw new Error("SortEngine: no dataset in memory. Either pass `data` in the constructor options, or call buildIndex(data, field).");a=this.dataset,e=t;}this.dataset=a;let r=a.length,n=new Uint32Array(r);for(let o=0;o<r;o++)n[o]=o;let l=a.map(o=>o[e]);if(typeof l[0]=="number"){let o=new Float64Array(r);for(let d=0;d<r;d++)o[d]=l[d];n.sort((d,c)=>o[d]-o[c]);}else n.sort((o,d)=>{let c=l[o],h=l[d];return c<h?-1:c>h?1:0});return this.cache.set(e,{indexes:n,dataRef:a,itemCount:r,fieldSnapshot:l}),this}clearIndexes(){return this.cache.clear(),this}clearData(){return this.dataset=[],this.cache.clear(),this}data(t){return this.dataset=t,this.rebuildConfiguredIndexes(),this}getOriginData(){return this.dataset}sort(t,s,a=false){let e,r;if(s===void 0){if(!this.dataset.length)throw new Error("SortEngine: no dataset in memory. Either pass `data` in the constructor options, or call sort(data, descriptors).");e=this.dataset,r=t;}else e=t,r=s;if(r.length===0||e.length===0)return this.withChain(e);if(r.length===1){let{field:i,direction:o}=r[0],d=this.cache.get(i);if(d&&d.dataRef===e&&d.itemCount===e.length&&this.isFieldSnapshotValid(e,i,d.fieldSnapshot))return this.withChain(this.reconstructFromIndex(e,d.indexes,o))}let n=a?e:e.slice();if(r.length===1&&e.length>0&&typeof e[0][r[0].field]=="number")return this.withChain(this.radixSortNumeric(n,r[0].field,r[0].direction));let l=this.buildComparator(r);return n.sort(l),this.withChain(n)}withChain(t){let s=t;return Object.defineProperty(s,"sort",{value:(a,e,r=false)=>e===void 0?this.sort(t,a,r):this.sort(a,e,r),enumerable:false,configurable:true,writable:true}),Object.defineProperty(s,"clearIndexes",{value:()=>this.clearIndexes(),enumerable:false,configurable:true,writable:true}),Object.defineProperty(s,"data",{value:a=>this.data(a),enumerable:false,configurable:true,writable:true}),Object.defineProperty(s,"getOriginData",{value:()=>this.getOriginData(),enumerable:false,configurable:true,writable:true}),Object.defineProperty(s,"clearData",{value:()=>this.clearData(),enumerable:false,configurable:true,writable:true}),s}reconstructFromIndex(t,s,a){let e=t.length,r=new Array(e);if(a==="asc")for(let n=0;n<e;n++)r[n]=t[s[n]];else for(let n=0;n<e;n++)r[n]=t[s[e-1-n]];return r}isFieldSnapshotValid(t,s,a){for(let e=0;e<t.length;e++)if(t[e][s]!==a[e])return false;return true}buildComparator(t){let s=t.map(({field:r})=>r),a=t.map(({direction:r})=>r==="asc"?1:-1),e=s.length;return (r,n)=>{for(let l=0;l<e;l++){let i=r[s[l]],o=n[s[l]];if(i<o)return -a[l];if(i>o)return a[l]}return 0}}radixSortNumeric(t,s,a){let e=t.length,r=new Float64Array(e);for(let i=0;i<e;i++)r[i]=t[i][s];let n=new Uint32Array(e);for(let i=0;i<e;i++)n[i]=i;n.sort((i,o)=>r[i]-r[o]);let l=new Array(e);if(a==="asc")for(let i=0;i<e;i++)l[i]=t[n[i]];else for(let i=0;i<e;i++)l[e-1-i]=t[n[i]];return l}};export{u as a};
@@ -0,0 +1 @@
1
+ var p=class{constructor(){this.indexes=new Map;}buildIndex(e,r){let n=new Map;for(let s=0,t=e.length;s<t;s++){let i=e[s],a=i[r];if(a==null)continue;let l=n.get(a);l?l.push(i):n.set(a,[i]);}this.indexes.set(r,n);}getByValue(e,r){let n=this.indexes.get(e);return n?n.get(r)??[]:[]}getByValues(e,r){let n=this.indexes.get(e);if(!n)return [];if(r.length===1)return n.get(r[0])??[];let s=new Set,t=[];for(let i=0;i<r.length;i++){let a=n.get(r[i]);if(a!==void 0)for(let l=0;l<a.length;l++){let o=a[l];s.has(o)||(s.add(o),t.push(o));}}return t}hasIndex(e){return this.indexes.has(e)}clear(){this.indexes.clear();}getIndexMap(e){return this.indexes.get(e)}};var v=class{constructor(e={}){this.dataset=[];this.indexedFields=new Set;this.previousResult=null;this.previousCriteria=null;this.previousBaseData=null;if(this.indexer=new p,this.filterByPreviousResult=e.filterByPreviousResult??false,!!e.data&&(this.dataset=e.data,e.fields?.length)){for(let r of e.fields)this.indexedFields.add(r);this.rebuildConfiguredIndexes();}}rebuildConfiguredIndexes(){this.indexer.clear();for(let e of this.indexedFields)this.buildIndex(this.dataset,e);}buildIndex(e,r){if(!Array.isArray(e)){if(!this.dataset.length)throw new Error("FilterEngine: no dataset in memory. Either pass `data` in the constructor options, or call buildIndex(data, field).");return this.indexer.buildIndex(this.dataset,e),this}return this.dataset=e,this.previousResult=null,this.previousCriteria=null,this.previousBaseData=null,this.indexer.buildIndex(e,r),this}clearIndexes(){return this.indexer.clear(),this}resetFilterState(){return this.previousResult=null,this.previousCriteria=null,this.previousBaseData=null,this}clearData(){return this.dataset=[],this.indexer.clear(),this.resetFilterState(),this}data(e){return this.dataset=e,this.resetFilterState(),this.rebuildConfiguredIndexes(),this}getOriginData(){return this.dataset}filter(e,r){let n=r===void 0,s,t,i;if(n){if(!this.dataset.length)throw new Error("FilterEngine: no dataset in memory. Either pass `data` in the constructor options, or call filter(data, criteria).");if(t=e,this.filterByPreviousResult&&this.previousResult!==null&&this.previousCriteria!==null&&this.previousBaseData===this.dataset){let u=this.hasCriteriaAdditions(this.previousCriteria,t),h=this.hasCriteriaRemovals(this.previousCriteria,t);if(!u&&!h)return this.withChain(this.previousResult);u&&!h?(s=this.previousResult,i=this.getAddedCriteria(this.previousCriteria,t)):(s=this.dataset,i=t);}else s=this.dataset,i=t;}else if(t=r,s=e,this.filterByPreviousResult&&this.previousResult!==null&&this.previousCriteria!==null&&this.previousBaseData===s){let u=this.hasCriteriaAdditions(this.previousCriteria,t),h=this.hasCriteriaRemovals(this.previousCriteria,t);if(!u&&!h)return this.withChain(this.previousResult);u&&!h?(s=this.previousResult,i=this.getAddedCriteria(this.previousCriteria,t)):i=t;}else i=t;if(t.length===0)return this.filterByPreviousResult&&(this.previousResult=null,this.previousCriteria=null,this.previousBaseData=null),this.withChain(n?this.dataset:s);n&&!i&&(i=t);let{indexedCriteria:a,linearCriteria:l}=i.reduce((u,h)=>(this.indexer.hasIndex(h.field)?u.indexedCriteria.push(h):u.linearCriteria.push(h),u),{indexedCriteria:[],linearCriteria:[]}),o;if(a.length>0&&l.length===0)return o=this.filterViaIndex(a,s),this.filterByPreviousResult&&(this.previousResult=o,this.previousCriteria=this.cloneCriteria(t),this.previousBaseData=n?this.dataset:e),this.withChain(o);if(a.length>0&&l.length>0){let u=this.filterViaIndex(a,s);return o=this.linearFilter(u,l),this.filterByPreviousResult&&(this.previousResult=o,this.previousCriteria=this.cloneCriteria(t),this.previousBaseData=n?this.dataset:e),this.withChain(o)}return o=this.linearFilter(s,i),this.filterByPreviousResult&&(this.previousResult=o,this.previousCriteria=this.cloneCriteria(t),this.previousBaseData=n?this.dataset:e),this.withChain(o)}withChain(e){let r=e;return Object.defineProperty(r,"filter",{value:(n,s)=>s===void 0?this.filter(e,n):this.filter(n,s),enumerable:false,configurable:true,writable:true}),Object.defineProperty(r,"clearIndexes",{value:()=>this.clearIndexes(),enumerable:false,configurable:true,writable:true}),Object.defineProperty(r,"data",{value:n=>this.data(n),enumerable:false,configurable:true,writable:true}),Object.defineProperty(r,"getOriginData",{value:()=>this.getOriginData(),enumerable:false,configurable:true,writable:true}),Object.defineProperty(r,"clearData",{value:()=>this.clearData(),enumerable:false,configurable:true,writable:true}),Object.defineProperty(r,"resetFilterState",{value:()=>this.resetFilterState(),enumerable:false,configurable:true,writable:true}),r}cloneCriteria(e){return e.map(({field:r,values:n})=>({field:r,values:[...n]}))}hasCriteriaAdditions(e,r){let n=new Map(e.map(({field:t,values:i})=>[t,new Set(i)])),s=new Map(r.map(({field:t,values:i})=>[t,new Set(i)]));for(let[t,i]of s){let a=n.get(t);if(!a)return true;for(let l of i)if(!a.has(l))return true}return false}hasCriteriaRemovals(e,r){let n=new Map(e.map(({field:t,values:i})=>[t,new Set(i)])),s=new Map(r.map(({field:t,values:i})=>[t,new Set(i)]));for(let[t,i]of n){let a=s.get(t);if(!a)return true;for(let l of i)if(!a.has(l))return true}return false}getAddedCriteria(e,r){let n=new Map(e.map(({field:t,values:i})=>[t,new Set(i)])),s=[];for(let{field:t,values:i}of r){let a=n.get(t);if(!a){s.push({field:t,values:[...i]});continue}let l=i.filter(o=>!a.has(o));l.length>0&&s.push({field:t,values:l});}return s}linearFilter(e,r){let n=new Map(r.map(({field:i,values:a})=>[i,new Set(a)])),s=r.map(({field:i})=>i),t=[];for(let i=0;i<e.length;i++){let a=e[i],l=true;for(let o=0;o<s.length;o++){let u=s[o];if(!n.get(u).has(a[u])){l=false;break}}l&&t.push(a);}return t}filterViaIndex(e,r){let s=r!==this.dataset?new Set(r):null;if(e.length===1){let d=this.indexer.getByValues(e[0].field,e[0].values);return s?d.filter(f=>s.has(f)):d}let t=e.map(d=>({criterion:d,size:this.estimateIndexSize(d)})).sort((d,f)=>d.size-f.size),{field:i,values:a}=t[0].criterion,l=this.indexer.getByValues(i,a);if(l.length===0)return [];let o=new Map(t.slice(1).map(({criterion:{field:d,values:f}})=>[d,new Set(f)])),u=Array.from(o.keys()),h=[];for(let d=0;d<l.length;d++){let f=l[d],c=true;for(let g=0;g<u.length;g++){let T=u[g];if(!o.get(T).has(f[T])){c=false;break}}c&&s&&!s.has(f)&&(c=false),c&&h.push(f);}return h}estimateIndexSize(e){let r=this.indexer.getIndexMap(e.field);return r?e.values.reduce((n,s)=>{let t=r.get(s);return t?n+t.length:n},0):1/0}};export{v as a};
@@ -14,6 +14,7 @@ interface FilterEngineOptions<T extends CollectionItem = CollectionItem> {
14
14
  interface FilterEngineChain<T extends CollectionItem> {
15
15
  filter(criteria: FilterCriterion<T>[]): T[] & FilterEngineChain<T>;
16
16
  filter(data: T[], criteria: FilterCriterion<T>[]): T[] & FilterEngineChain<T>;
17
+ getOriginData(): T[];
17
18
  data(data: T[]): FilterEngine<T>;
18
19
  clearIndexes(): FilterEngine<T>;
19
20
  clearData(): FilterEngine<T>;
@@ -40,6 +41,7 @@ declare class FilterEngine<T extends CollectionItem> {
40
41
  resetFilterState(): this;
41
42
  clearData(): this;
42
43
  data(data: T[]): this;
44
+ getOriginData(): T[];
43
45
  /**
44
46
  * Filters the data based on the given criteria.
45
47
  */
@@ -1 +1 @@
1
- export{a as FilterEngine}from'../chunk-2HRTLOF4.mjs';
1
+ export{a as FilterEngine}from'../chunk-VJ4JZMHD.mjs';
package/dist/index.mjs CHANGED
@@ -1 +1 @@
1
- export{a as TextSearchEngine}from'./chunk-XWZBWMQK.mjs';export{a as FilterEngine}from'./chunk-2HRTLOF4.mjs';export{a as SortEngine}from'./chunk-Z33ISQHF.mjs';export{a as MergeEngines}from'./chunk-CVYC4YF4.mjs';
1
+ export{a as TextSearchEngine}from'./chunk-574GKMWM.mjs';export{a as FilterEngine}from'./chunk-VJ4JZMHD.mjs';export{a as SortEngine}from'./chunk-FZVBLKAD.mjs';export{a as MergeEngines}from'./chunk-A5FBM5K3.mjs';
@@ -27,6 +27,7 @@ interface MergeEnginesChain<T extends CollectionItem> {
27
27
  sort(data: T[], descriptors: SortDescriptor<T>[], inPlace?: boolean): T[] & MergeEnginesChain<T>;
28
28
  filter(criteria: FilterCriterion<T>[]): T[] & MergeEnginesChain<T>;
29
29
  filter(data: T[], criteria: FilterCriterion<T>[]): T[] & MergeEnginesChain<T>;
30
+ getOriginData(module: MergeModuleName): T[];
30
31
  data(data: T[]): MergeEngines<T>;
31
32
  clearIndexes(module: MergeModuleName): T[] & MergeEnginesChain<T>;
32
33
  clearData(module: MergeModuleName): T[] & MergeEnginesChain<T>;
@@ -36,6 +37,7 @@ declare class MergeEngines<T extends CollectionItem> {
36
37
  private readonly clearIndexMethods;
37
38
  private readonly clearDataMethods;
38
39
  private readonly setDataMethods;
40
+ private readonly getOriginDataMethods;
39
41
  /**
40
42
  * Creates a new MergeEngines instance with the given options.
41
43
  * Collects all modules from imports.
@@ -66,6 +68,7 @@ declare class MergeEngines<T extends CollectionItem> {
66
68
  filter(criteria: FilterCriterion<T>[]): T[] & MergeEnginesChain<T>;
67
69
  filter(data: T[], criteria: FilterCriterion<T>[]): T[] & MergeEnginesChain<T>;
68
70
  private withChain;
71
+ getOriginData(module: MergeModuleName): T[];
69
72
  clearIndexes(module: MergeModuleName): this;
70
73
  data(data: T[]): this;
71
74
  clearData(module: MergeModuleName): this;
@@ -1 +1 @@
1
- export{a as MergeEngines}from'../chunk-CVYC4YF4.mjs';
1
+ export{a as MergeEngines}from'../chunk-A5FBM5K3.mjs';
@@ -14,6 +14,7 @@ interface TextSearchEngineOptions<T extends CollectionItem = CollectionItem> {
14
14
  interface TextSearchEngineChain<T extends CollectionItem> {
15
15
  search(query: string): T[] & TextSearchEngineChain<T>;
16
16
  search(field: keyof T & string, query: string): T[] & TextSearchEngineChain<T>;
17
+ getOriginData(): T[];
17
18
  data(data: T[]): TextSearchEngine<T>;
18
19
  clearIndexes(): TextSearchEngine<T>;
19
20
  clearData(): TextSearchEngine<T>;
@@ -57,6 +58,7 @@ declare class TextSearchEngine<T extends CollectionItem> {
57
58
  private searchFieldLinear;
58
59
  private withChain;
59
60
  clearIndexes(): this;
61
+ getOriginData(): T[];
60
62
  data(data: T[]): this;
61
63
  clearData(): this;
62
64
  }
@@ -1 +1 @@
1
- export{a as TextSearchEngine}from'../chunk-XWZBWMQK.mjs';
1
+ export{a as TextSearchEngine}from'../chunk-574GKMWM.mjs';
@@ -13,6 +13,7 @@ interface SortEngineOptions<T extends CollectionItem = CollectionItem> {
13
13
  interface SortEngineChain<T extends CollectionItem> {
14
14
  sort(descriptors: SortDescriptor<T>[]): T[] & SortEngineChain<T>;
15
15
  sort(data: T[], descriptors: SortDescriptor<T>[], inPlace?: boolean): T[] & SortEngineChain<T>;
16
+ getOriginData(): T[];
16
17
  data(data: T[]): SortEngine<T>;
17
18
  clearIndexes(): SortEngine<T>;
18
19
  clearData(): SortEngine<T>;
@@ -36,6 +37,7 @@ declare class SortEngine<T extends CollectionItem> {
36
37
  clearIndexes(): this;
37
38
  clearData(): this;
38
39
  data(data: T[]): this;
40
+ getOriginData(): T[];
39
41
  /**
40
42
  * Sorts the data based on the given descriptors.
41
43
  */
@@ -1 +1 @@
1
- export{a as SortEngine}from'../chunk-Z33ISQHF.mjs';
1
+ export{a as SortEngine}from'../chunk-FZVBLKAD.mjs';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@devisfuture/mega-collection",
3
- "version": "1.2.0",
3
+ "version": "1.3.0",
4
4
  "description": "High-performance search, filter & sort engine for 100K+ item collections in JavaScript/TypeScript",
5
5
  "exports": {
6
6
  ".": {
@@ -1 +0,0 @@
1
- var p=class{constructor(){this.indexes=new Map;}buildIndex(e,r){let n=new Map;for(let s=0,t=e.length;s<t;s++){let i=e[s],a=i[r];if(a==null)continue;let l=n.get(a);l?l.push(i):n.set(a,[i]);}this.indexes.set(r,n);}getByValue(e,r){let n=this.indexes.get(e);return n?n.get(r)??[]:[]}getByValues(e,r){let n=this.indexes.get(e);if(!n)return [];if(r.length===1)return n.get(r[0])??[];let s=new Set,t=[];for(let i=0;i<r.length;i++){let a=n.get(r[i]);if(a!==void 0)for(let l=0;l<a.length;l++){let o=a[l];s.has(o)||(s.add(o),t.push(o));}}return t}hasIndex(e){return this.indexes.has(e)}clear(){this.indexes.clear();}getIndexMap(e){return this.indexes.get(e)}};var g=class{constructor(e={}){this.dataset=[];this.indexedFields=new Set;this.previousResult=null;this.previousCriteria=null;this.previousBaseData=null;if(this.indexer=new p,this.filterByPreviousResult=e.filterByPreviousResult??false,!!e.data&&(this.dataset=e.data,e.fields?.length)){for(let r of e.fields)this.indexedFields.add(r);this.rebuildConfiguredIndexes();}}rebuildConfiguredIndexes(){this.indexer.clear();for(let e of this.indexedFields)this.buildIndex(this.dataset,e);}buildIndex(e,r){if(!Array.isArray(e)){if(!this.dataset.length)throw new Error("FilterEngine: no dataset in memory. Either pass `data` in the constructor options, or call buildIndex(data, field).");return this.indexer.buildIndex(this.dataset,e),this}return this.dataset=e,this.previousResult=null,this.previousCriteria=null,this.previousBaseData=null,this.indexer.buildIndex(e,r),this}clearIndexes(){return this.indexer.clear(),this}resetFilterState(){return this.previousResult=null,this.previousCriteria=null,this.previousBaseData=null,this}clearData(){return this.dataset=[],this.indexer.clear(),this.resetFilterState(),this}data(e){return this.dataset=e,this.resetFilterState(),this.rebuildConfiguredIndexes(),this}filter(e,r){let n=r===void 0,s,t,i;if(n){if(!this.dataset.length)throw new Error("FilterEngine: no dataset in memory. Either pass `data` in the constructor options, or call filter(data, criteria).");if(t=e,this.filterByPreviousResult&&this.previousResult!==null&&this.previousCriteria!==null&&this.previousBaseData===this.dataset){let u=this.hasCriteriaAdditions(this.previousCriteria,t),h=this.hasCriteriaRemovals(this.previousCriteria,t);if(!u&&!h)return this.withChain(this.previousResult);u&&!h?(s=this.previousResult,i=this.getAddedCriteria(this.previousCriteria,t)):(s=this.dataset,i=t);}else s=this.dataset,i=t;}else if(t=r,s=e,this.filterByPreviousResult&&this.previousResult!==null&&this.previousCriteria!==null&&this.previousBaseData===s){let u=this.hasCriteriaAdditions(this.previousCriteria,t),h=this.hasCriteriaRemovals(this.previousCriteria,t);if(!u&&!h)return this.withChain(this.previousResult);u&&!h?(s=this.previousResult,i=this.getAddedCriteria(this.previousCriteria,t)):i=t;}else i=t;if(t.length===0)return this.filterByPreviousResult&&(this.previousResult=null,this.previousCriteria=null,this.previousBaseData=null),this.withChain(n?this.dataset:s);n&&!i&&(i=t);let{indexedCriteria:a,linearCriteria:l}=i.reduce((u,h)=>(this.indexer.hasIndex(h.field)?u.indexedCriteria.push(h):u.linearCriteria.push(h),u),{indexedCriteria:[],linearCriteria:[]}),o;if(a.length>0&&l.length===0)return o=this.filterViaIndex(a,s),this.filterByPreviousResult&&(this.previousResult=o,this.previousCriteria=this.cloneCriteria(t),this.previousBaseData=n?this.dataset:e),this.withChain(o);if(a.length>0&&l.length>0){let u=this.filterViaIndex(a,s);return o=this.linearFilter(u,l),this.filterByPreviousResult&&(this.previousResult=o,this.previousCriteria=this.cloneCriteria(t),this.previousBaseData=n?this.dataset:e),this.withChain(o)}return o=this.linearFilter(s,i),this.filterByPreviousResult&&(this.previousResult=o,this.previousCriteria=this.cloneCriteria(t),this.previousBaseData=n?this.dataset:e),this.withChain(o)}withChain(e){let r=e;return Object.defineProperty(r,"filter",{value:(n,s)=>s===void 0?this.filter(e,n):this.filter(n,s),enumerable:false,configurable:true,writable:true}),Object.defineProperty(r,"clearIndexes",{value:()=>this.clearIndexes(),enumerable:false,configurable:true,writable:true}),Object.defineProperty(r,"data",{value:n=>this.data(n),enumerable:false,configurable:true,writable:true}),Object.defineProperty(r,"clearData",{value:()=>this.clearData(),enumerable:false,configurable:true,writable:true}),Object.defineProperty(r,"resetFilterState",{value:()=>this.resetFilterState(),enumerable:false,configurable:true,writable:true}),r}cloneCriteria(e){return e.map(({field:r,values:n})=>({field:r,values:[...n]}))}hasCriteriaAdditions(e,r){let n=new Map(e.map(({field:t,values:i})=>[t,new Set(i)])),s=new Map(r.map(({field:t,values:i})=>[t,new Set(i)]));for(let[t,i]of s){let a=n.get(t);if(!a)return true;for(let l of i)if(!a.has(l))return true}return false}hasCriteriaRemovals(e,r){let n=new Map(e.map(({field:t,values:i})=>[t,new Set(i)])),s=new Map(r.map(({field:t,values:i})=>[t,new Set(i)]));for(let[t,i]of n){let a=s.get(t);if(!a)return true;for(let l of i)if(!a.has(l))return true}return false}getAddedCriteria(e,r){let n=new Map(e.map(({field:t,values:i})=>[t,new Set(i)])),s=[];for(let{field:t,values:i}of r){let a=n.get(t);if(!a){s.push({field:t,values:[...i]});continue}let l=i.filter(o=>!a.has(o));l.length>0&&s.push({field:t,values:l});}return s}linearFilter(e,r){let n=new Map(r.map(({field:i,values:a})=>[i,new Set(a)])),s=r.map(({field:i})=>i),t=[];for(let i=0;i<e.length;i++){let a=e[i],l=true;for(let o=0;o<s.length;o++){let u=s[o];if(!n.get(u).has(a[u])){l=false;break}}l&&t.push(a);}return t}filterViaIndex(e,r){let s=r!==this.dataset?new Set(r):null;if(e.length===1){let d=this.indexer.getByValues(e[0].field,e[0].values);return s?d.filter(f=>s.has(f)):d}let t=e.map(d=>({criterion:d,size:this.estimateIndexSize(d)})).sort((d,f)=>d.size-f.size),{field:i,values:a}=t[0].criterion,l=this.indexer.getByValues(i,a);if(l.length===0)return [];let o=new Map(t.slice(1).map(({criterion:{field:d,values:f}})=>[d,new Set(f)])),u=Array.from(o.keys()),h=[];for(let d=0;d<l.length;d++){let f=l[d],c=true;for(let T=0;T<u.length;T++){let v=u[T];if(!o.get(v).has(f[v])){c=false;break}}c&&s&&!s.has(f)&&(c=false),c&&h.push(f);}return h}estimateIndexSize(e){let r=this.indexer.getIndexMap(e.field);return r?e.values.reduce((n,s)=>{let t=r.get(s);return t?n+t.length:n},0):1/0}};export{g as a};
@@ -1 +0,0 @@
1
- var T=class{constructor(e){let{imports:t,data:n,...r}=e,s=new Set(t),a={},l={},g={},d={};for(let u of s){let M=u.prototype,c=this.getMethodNames(M);if(c.length===0)continue;let f=this.getModuleInitOptions(u.name,c,r),i=new u({data:n,...f}),o=this.getModuleName(c);o&&this.hasMethod(i,"clearIndexes")&&!l[o]&&(l[o]=i.clearIndexes.bind(i)),o&&this.hasMethod(i,"clearData")&&!g[o]&&(g[o]=i.clearData.bind(i)),o&&this.hasMethod(i,"data")&&!d[o]&&(d[o]=i.data.bind(i));for(let h of c)a[h]||this.hasMethod(i,h)&&(a[h]=i[h].bind(i));}this.engine=Object.keys(a).length>0?a:null,this.clearIndexMethods=l,this.clearDataMethods=g,this.setDataMethods=d;}getModuleName(e){return e.includes("search")?"search":e.includes("sort")?"sort":e.includes("filter")?"filter":null}getModuleInitOptions(e,t,n){let r={},s=n[e];this.isRecord(s)&&Object.assign(r,s);for(let a of t){let l=n[a];this.isRecord(l)&&Object.assign(r,l);}return r}getMethodNames(e){let t=e;return Object.getOwnPropertyNames(t).filter(n=>n==="constructor"?false:typeof t[n]=="function")}hasMethod(e,t){return typeof e=="object"&&e!==null&&typeof e[t]=="function"}isRecord(e){return typeof e=="object"&&e!==null}callEngineMethod(e,t){let n=this.engine?.[e];if(!n)throw new Error(`MergeEngines: Method "${e}" is not available. Add module with method "${e}" to the \`imports\` array.`);return n(...t)}search(e,t){if(!this.engine?.search)throw new Error("MergeEngines: TextSearchEngine is not available. Add TextSearchEngine to the `imports` array.");return t===void 0?this.withChain(this.callEngineMethod("search",[e])):this.withChain(this.callEngineMethod("search",[e,t]))}sort(e,t,n){if(!this.engine?.sort)throw new Error("MergeEngines: SortEngine is not available. Add SortEngine to the `imports` array.");return t===void 0?this.withChain(this.callEngineMethod("sort",[e])):this.withChain(this.callEngineMethod("sort",[e,t,n]))}filter(e,t){if(!this.engine?.filter)throw new Error("MergeEngines: FilterEngine is not available. Add FilterEngine to the `imports` array.");return t===void 0?this.withChain(this.callEngineMethod("filter",[e])):this.withChain(this.callEngineMethod("filter",[e,t]))}withChain(e){let t=e;return Object.defineProperty(t,"search",{value:(n,r)=>r===void 0?this.search(n):this.search(n,r),enumerable:false,configurable:true,writable:true}),Object.defineProperty(t,"sort",{value:(n,r,s)=>r===void 0?this.sort(e,n,s):this.sort(n,r,s),enumerable:false,configurable:true,writable:true}),Object.defineProperty(t,"filter",{value:(n,r)=>r===void 0?this.filter(e,n):this.filter(n,r),enumerable:false,configurable:true,writable:true}),Object.defineProperty(t,"clearIndexes",{value:n=>(this.clearIndexes(n),this.withChain(e)),enumerable:false,configurable:true,writable:true}),Object.defineProperty(t,"clearData",{value:n=>(this.clearData(n),this.withChain(e)),enumerable:false,configurable:true,writable:true}),Object.defineProperty(t,"data",{value:n=>this.data(n),enumerable:false,configurable:true,writable:true}),t}clearIndexes(e){let t=this.clearIndexMethods[e];if(t)return t(),this;let n={search:"TextSearchEngine",sort:"SortEngine",filter:"FilterEngine"};throw new Error(`MergeEngines: ${n[e]} is not available. Add ${n[e]} to the \`imports\` array.`)}data(e){let t=["search","sort","filter"];for(let n of t){let r=this.setDataMethods[n];r&&r(e);}return this}clearData(e){let t=this.clearDataMethods[e];if(t)return t(),this;let n={search:"TextSearchEngine",sort:"SortEngine",filter:"FilterEngine"};throw new Error(`MergeEngines: ${n[e]} is not available. Add ${n[e]} to the \`imports\` array.`)}};export{T as a};
@@ -1 +0,0 @@
1
- var u=class{constructor(t={}){this.cache=new Map;this.dataset=[];this.indexedFields=new Set;if(t.data&&(this.dataset=t.data,t.fields?.length)){for(let s of t.fields)this.indexedFields.add(s);this.rebuildConfiguredIndexes();}}rebuildConfiguredIndexes(){this.cache.clear();for(let t of this.indexedFields)this.buildIndex(this.dataset,t);}buildIndex(t,s){let o,e;if(Array.isArray(t))o=t,e=s;else {if(!this.dataset.length)throw new Error("SortEngine: no dataset in memory. Either pass `data` in the constructor options, or call buildIndex(data, field).");o=this.dataset,e=t;}this.dataset=o;let r=o.length,n=new Uint32Array(r);for(let a=0;a<r;a++)n[a]=a;let l=o.map(a=>a[e]);if(typeof l[0]=="number"){let a=new Float64Array(r);for(let d=0;d<r;d++)a[d]=l[d];n.sort((d,c)=>a[d]-a[c]);}else n.sort((a,d)=>{let c=l[a],h=l[d];return c<h?-1:c>h?1:0});return this.cache.set(e,{indexes:n,dataRef:o,itemCount:r,fieldSnapshot:l}),this}clearIndexes(){return this.cache.clear(),this}clearData(){return this.dataset=[],this.cache.clear(),this}data(t){return this.dataset=t,this.rebuildConfiguredIndexes(),this}sort(t,s,o=false){let e,r;if(s===void 0){if(!this.dataset.length)throw new Error("SortEngine: no dataset in memory. Either pass `data` in the constructor options, or call sort(data, descriptors).");e=this.dataset,r=t;}else e=t,r=s;if(r.length===0||e.length===0)return this.withChain(e);if(r.length===1){let{field:i,direction:a}=r[0],d=this.cache.get(i);if(d&&d.dataRef===e&&d.itemCount===e.length&&this.isFieldSnapshotValid(e,i,d.fieldSnapshot))return this.withChain(this.reconstructFromIndex(e,d.indexes,a))}let n=o?e:e.slice();if(r.length===1&&e.length>0&&typeof e[0][r[0].field]=="number")return this.withChain(this.radixSortNumeric(n,r[0].field,r[0].direction));let l=this.buildComparator(r);return n.sort(l),this.withChain(n)}withChain(t){let s=t;return Object.defineProperty(s,"sort",{value:(o,e,r=false)=>e===void 0?this.sort(t,o,r):this.sort(o,e,r),enumerable:false,configurable:true,writable:true}),Object.defineProperty(s,"clearIndexes",{value:()=>this.clearIndexes(),enumerable:false,configurable:true,writable:true}),Object.defineProperty(s,"data",{value:o=>this.data(o),enumerable:false,configurable:true,writable:true}),Object.defineProperty(s,"clearData",{value:()=>this.clearData(),enumerable:false,configurable:true,writable:true}),s}reconstructFromIndex(t,s,o){let e=t.length,r=new Array(e);if(o==="asc")for(let n=0;n<e;n++)r[n]=t[s[n]];else for(let n=0;n<e;n++)r[n]=t[s[e-1-n]];return r}isFieldSnapshotValid(t,s,o){for(let e=0;e<t.length;e++)if(t[e][s]!==o[e])return false;return true}buildComparator(t){let s=t.map(({field:r})=>r),o=t.map(({direction:r})=>r==="asc"?1:-1),e=s.length;return (r,n)=>{for(let l=0;l<e;l++){let i=r[s[l]],a=n[s[l]];if(i<a)return -o[l];if(i>a)return o[l]}return 0}}radixSortNumeric(t,s,o){let e=t.length,r=new Float64Array(e);for(let i=0;i<e;i++)r[i]=t[i][s];let n=new Uint32Array(e);for(let i=0;i<e;i++)n[i]=i;n.sort((i,a)=>r[i]-r[a]);let l=new Array(e);if(o==="asc")for(let i=0;i<e;i++)l[i]=t[n[i]];else for(let i=0;i<e;i++)l[e-1-i]=t[n[i]];return l}};export{u as a};