@signaldb/core 2.0.0-beta.13 → 2.0.0-beta.14

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 (82) hide show
  1. package/dist/.vite/manifest.json +72 -46
  2. package/dist/AsyncDataAdapter.d.ts +11 -2
  3. package/dist/Collection/Cursor.d.ts +11 -1
  4. package/dist/Collection/Observer.d.ts +31 -0
  5. package/dist/DataAdapter.d.ts +15 -2
  6. package/dist/WorkerDataAdapter.d.ts +25 -2
  7. package/dist/WorkerDataAdapterHost.d.ts +2 -0
  8. package/dist/index.cjs.js +15 -15
  9. package/dist/index.d.ts +2 -0
  10. package/dist/index.mjs +15 -15
  11. package/dist/index10.cjs.js +7 -19
  12. package/dist/index10.mjs +7 -19
  13. package/dist/index11.cjs.js +19 -41
  14. package/dist/index11.mjs +19 -41
  15. package/dist/index12.cjs.js +41 -18
  16. package/dist/index12.mjs +41 -18
  17. package/dist/index13.cjs.js +18 -41
  18. package/dist/index13.mjs +18 -41
  19. package/dist/index14.cjs.js +39 -80
  20. package/dist/index14.mjs +39 -80
  21. package/dist/index15.cjs.js +82 -11
  22. package/dist/index15.mjs +82 -11
  23. package/dist/index16.cjs.js +11 -132
  24. package/dist/index16.mjs +11 -132
  25. package/dist/index17.cjs.js +127 -38
  26. package/dist/index17.mjs +127 -38
  27. package/dist/index18.cjs.js +43 -11
  28. package/dist/index18.mjs +43 -11
  29. package/dist/index19.cjs.js +10 -18
  30. package/dist/index19.mjs +11 -19
  31. package/dist/index20.cjs.js +19 -33
  32. package/dist/index20.mjs +19 -33
  33. package/dist/index21.cjs.js +33 -31
  34. package/dist/index21.mjs +33 -31
  35. package/dist/index22.cjs.js +31 -21
  36. package/dist/index22.mjs +31 -21
  37. package/dist/index23.cjs.js +16 -14
  38. package/dist/index23.mjs +16 -14
  39. package/dist/index24.cjs.js +15 -338
  40. package/dist/index24.mjs +15 -338
  41. package/dist/index25.cjs.js +122 -554
  42. package/dist/index25.mjs +121 -554
  43. package/dist/index26.cjs.js +34 -7
  44. package/dist/index26.mjs +34 -7
  45. package/dist/index27.cjs.js +339 -7
  46. package/dist/index27.mjs +339 -7
  47. package/dist/index28.cjs.js +550 -83
  48. package/dist/index28.mjs +550 -82
  49. package/dist/index29.cjs.js +8 -422
  50. package/dist/index29.mjs +8 -422
  51. package/dist/index30.cjs.js +7 -68
  52. package/dist/index30.mjs +7 -68
  53. package/dist/index31.cjs.js +86 -28
  54. package/dist/index31.mjs +85 -28
  55. package/dist/index32.cjs.js +446 -278
  56. package/dist/index32.mjs +446 -278
  57. package/dist/index33.cjs.js +68 -17
  58. package/dist/index33.mjs +68 -17
  59. package/dist/index34.cjs.js +460 -304
  60. package/dist/index34.mjs +460 -304
  61. package/dist/index35.cjs.js +14 -532
  62. package/dist/index35.mjs +14 -532
  63. package/dist/index36.cjs.js +389 -0
  64. package/dist/index36.mjs +389 -0
  65. package/dist/index37.cjs.js +539 -0
  66. package/dist/index37.mjs +539 -0
  67. package/dist/index4.cjs.js +199 -140
  68. package/dist/index4.mjs +195 -140
  69. package/dist/index5.cjs.js +152 -253
  70. package/dist/index5.mjs +152 -253
  71. package/dist/index6.cjs.js +267 -89
  72. package/dist/index6.mjs +267 -89
  73. package/dist/index7.cjs.js +121 -29
  74. package/dist/index7.mjs +121 -29
  75. package/dist/index8.cjs.js +29 -8
  76. package/dist/index8.mjs +29 -8
  77. package/dist/index9.cjs.js +8 -7
  78. package/dist/index9.mjs +8 -7
  79. package/dist/utils/incrementalQueryUpdate.d.ts +60 -0
  80. package/dist/utils/projectItems.d.ts +12 -0
  81. package/dist/utils/queryDelta.d.ts +83 -0
  82. package/package.json +1 -1
@@ -1,47 +1,136 @@
1
- //#region src/utils/deepClone.ts
1
+ const require_intersection = require("./index16.cjs.js");
2
+ //#region src/getIndexInfo.ts
2
3
  /**
3
- * Performs a deep clone of a value, supporting various types including arrays, objects,
4
- * Maps, Sets, Dates, and RegExps. Functions are not supported and will throw an error.
5
- * @template T - The type of the value to clone.
6
- * @param value - The value to deep clone.
7
- * @returns A deep copy of the provided value.
8
- * @throws {Error} An error if the value is a function, as cloning functions is not supported.
4
+ * Retrieves merged index information for a given flat selector by querying multiple
5
+ * index providers. Combines results from all index providers to determine matched ids
6
+ * and an optimized selector.
7
+ * @template T - The type of the items in the collection.
8
+ * @template I - The type of the unique identifier for the items.
9
+ * @param queryFunctions - An array of index providers to query.
10
+ * @param selector - The flat selector used to filter items.
11
+ * @returns An object containing:
12
+ * - `matched`: A boolean indicating if the selector matched any items.
13
+ * - `ids`: An array of matched item ids.
14
+ * - `optimizedSelector`: A flat selector optimized based on the index results.
9
15
  */
10
- function clone(value) {
11
- if (typeof value === "function") throw new Error("Cloning functions is not supported");
12
- if (value === null || typeof value !== "object") return value;
13
- if (value instanceof Date) return new Date(value);
14
- if (Array.isArray(value)) return value.map((item) => clone(item));
15
- if (value instanceof Map) {
16
- const result = /* @__PURE__ */ new Map();
17
- value.forEach((currentValue, key) => {
18
- result.set(key, clone(currentValue));
16
+ function getMergedIndexInfo(queryFunctions, selector) {
17
+ return queryFunctions.reduce((memoOrPromise, queryFunction) => {
18
+ const resultOrPromise = queryFunction(selector);
19
+ const processResult = (memo, result) => {
20
+ if (!result.matched) return memo;
21
+ const optimizedSelector = result.keepSelector ? memo.optimizedSelector : Object.fromEntries(Object.entries(memo.optimizedSelector).filter(([key]) => !result.fields.includes(key)));
22
+ return {
23
+ matched: true,
24
+ ids: [...new Set(memo.matched ? require_intersection.default(memo.ids, result.ids) : result.ids)],
25
+ optimizedSelector
26
+ };
27
+ };
28
+ if (resultOrPromise instanceof Promise) return resultOrPromise.then(async (result) => {
29
+ return processResult(memoOrPromise instanceof Promise ? await memoOrPromise : memoOrPromise, result);
19
30
  });
20
- return result;
21
- }
22
- if (value instanceof Set) {
23
- const result = /* @__PURE__ */ new Set();
24
- value.forEach((currentValue) => {
25
- result.add(clone(currentValue));
31
+ const memo = memoOrPromise;
32
+ if (memo instanceof Promise) throw new Error("Mixing async and sync index providers is not supported");
33
+ return processResult(memo, resultOrPromise);
34
+ }, {
35
+ matched: false,
36
+ ids: [],
37
+ optimizedSelector: { ...selector }
38
+ });
39
+ }
40
+ /**
41
+ * Optimizes a logic gate ($and/$or) of selectors by querying multiple index providers.
42
+ * @param queryFunctions - An array of index providers to query.
43
+ * @param logicGate - An array of selectors representing the logic gate.
44
+ * @param idsCallback - A callback function called with matched ids.
45
+ * @returns An array of optimized selectors or a promise that resolves to such an array.
46
+ */
47
+ function optimizeLogicGate(queryFunctions, logicGate, idsCallback) {
48
+ return logicGate.reduce((memoOrPromise, sel) => {
49
+ const getSelector = (indexInfo) => {
50
+ const { matched: selMatched, ids: selIds, optimizedSelector } = indexInfo;
51
+ if (selMatched) {
52
+ idsCallback(true, selIds);
53
+ if (Object.keys(optimizedSelector).length > 0) return optimizedSelector;
54
+ } else {
55
+ idsCallback(false, []);
56
+ return sel;
57
+ }
58
+ };
59
+ const indexInfoOrPromise = getIndexInfo(queryFunctions, sel);
60
+ if (indexInfoOrPromise instanceof Promise) return indexInfoOrPromise.then(async (indexInfo) => {
61
+ const memo = memoOrPromise instanceof Promise ? await memoOrPromise : memoOrPromise;
62
+ const optimizedSelector = getSelector(indexInfo);
63
+ if (optimizedSelector) memo.push(optimizedSelector);
64
+ return memo;
26
65
  });
27
- return result;
28
- }
29
- if (value instanceof RegExp) return new RegExp(value);
30
- const result = {};
31
- for (const key in value) if (Object.hasOwnProperty.call(value, key)) result[key] = clone(value[key]);
32
- return result;
66
+ const memo = memoOrPromise;
67
+ if (memo instanceof Promise) throw new Error("Mixing async and sync index providers is not supported");
68
+ const optimizedSelector = getSelector(indexInfoOrPromise);
69
+ if (optimizedSelector) memo.push(optimizedSelector);
70
+ return memo;
71
+ }, []);
33
72
  }
34
73
  /**
35
- * Creates a deep clone of an object. Uses the `structuredClone` function if available,
36
- * otherwise falls back to a manual deep clone implementation.
37
- * @template T - The type of the object to clone.
38
- * @param object - The object to deep clone.
39
- * @returns A deep copy of the provided object.
74
+ * Retrieves index information for a given complex selector by querying multiple
75
+ * index providers. Handles nested `$and` and `$or` conditions in the selector and
76
+ * optimizes the selector to minimize processing overhead.
77
+ * @template T - The type of the items in the collection.
78
+ * @template I - The type of the unique identifier for the items.
79
+ * @param queryFunctions - An array of index providers to query.
80
+ * @param selector - The complex selector used to filter items.
81
+ * @returns An object containing:
82
+ * - `matched`: A boolean indicating if the selector matched any items.
83
+ * - `ids`: An array of matched item ids.
84
+ * - `optimizedSelector`: A selector optimized based on the index results, with unused
85
+ * conditions removed.
40
86
  */
41
- function deepClone(object) {
42
- if (typeof structuredClone === "function") return structuredClone(object);
43
- /* istanbul ignore next -- @preserve */
44
- return clone(object);
87
+ function getIndexInfo(queryFunctions, selector) {
88
+ if (selector == null || Object.keys(selector).length <= 0) return {
89
+ matched: false,
90
+ ids: [],
91
+ optimizedSelector: selector
92
+ };
93
+ const { $and, $or, ...rest } = selector;
94
+ const flatInfoOrPromise = getMergedIndexInfo(queryFunctions, rest);
95
+ const processFlatInfo = (flatInfo) => {
96
+ let { matched, ids } = flatInfo;
97
+ const newSelector = flatInfo.optimizedSelector;
98
+ const $andNewOrPromise = Array.isArray($and) ? optimizeLogicGate(queryFunctions, $and, (match, selIds) => {
99
+ if (!match) return;
100
+ ids = matched ? require_intersection.default(ids, selIds) : selIds;
101
+ matched = true;
102
+ }) : void 0;
103
+ const process$and = ($andNew) => {
104
+ if ($andNew && $andNew.length > 0) newSelector.$and = $andNew;
105
+ let hasNonIndexField = false;
106
+ const matchedBefore = matched;
107
+ const idsBefore = ids;
108
+ const process$or = ($orNew) => {
109
+ if (hasNonIndexField || $orNew && $orNew.length > 0) {
110
+ newSelector.$or = $or;
111
+ matched = matchedBefore;
112
+ ids = idsBefore;
113
+ }
114
+ return {
115
+ matched,
116
+ ids: ids || [],
117
+ optimizedSelector: newSelector
118
+ };
119
+ };
120
+ const $orNewOrPromise = Array.isArray($or) ? optimizeLogicGate(queryFunctions, $or, (match, selIds) => {
121
+ if (match) {
122
+ ids = [...new Set([...ids, ...selIds])];
123
+ matched = true;
124
+ } else hasNonIndexField = true;
125
+ }) : void 0;
126
+ if ($orNewOrPromise instanceof Promise) return $orNewOrPromise.then(($orNew) => process$or($orNew));
127
+ return process$or($orNewOrPromise);
128
+ };
129
+ if ($andNewOrPromise instanceof Promise) return $andNewOrPromise.then(($andNew) => process$and($andNew));
130
+ return process$and($andNewOrPromise);
131
+ };
132
+ if (flatInfoOrPromise instanceof Promise) return flatInfoOrPromise.then(processFlatInfo);
133
+ return processFlatInfo(flatInfoOrPromise);
45
134
  }
46
135
  //#endregion
47
- exports.default = deepClone;
136
+ exports.default = getIndexInfo;
package/dist/index17.mjs CHANGED
@@ -1,47 +1,136 @@
1
- //#region src/utils/deepClone.ts
1
+ import intersection from "./index16.mjs";
2
+ //#region src/getIndexInfo.ts
2
3
  /**
3
- * Performs a deep clone of a value, supporting various types including arrays, objects,
4
- * Maps, Sets, Dates, and RegExps. Functions are not supported and will throw an error.
5
- * @template T - The type of the value to clone.
6
- * @param value - The value to deep clone.
7
- * @returns A deep copy of the provided value.
8
- * @throws {Error} An error if the value is a function, as cloning functions is not supported.
4
+ * Retrieves merged index information for a given flat selector by querying multiple
5
+ * index providers. Combines results from all index providers to determine matched ids
6
+ * and an optimized selector.
7
+ * @template T - The type of the items in the collection.
8
+ * @template I - The type of the unique identifier for the items.
9
+ * @param queryFunctions - An array of index providers to query.
10
+ * @param selector - The flat selector used to filter items.
11
+ * @returns An object containing:
12
+ * - `matched`: A boolean indicating if the selector matched any items.
13
+ * - `ids`: An array of matched item ids.
14
+ * - `optimizedSelector`: A flat selector optimized based on the index results.
9
15
  */
10
- function clone(value) {
11
- if (typeof value === "function") throw new Error("Cloning functions is not supported");
12
- if (value === null || typeof value !== "object") return value;
13
- if (value instanceof Date) return new Date(value);
14
- if (Array.isArray(value)) return value.map((item) => clone(item));
15
- if (value instanceof Map) {
16
- const result = /* @__PURE__ */ new Map();
17
- value.forEach((currentValue, key) => {
18
- result.set(key, clone(currentValue));
16
+ function getMergedIndexInfo(queryFunctions, selector) {
17
+ return queryFunctions.reduce((memoOrPromise, queryFunction) => {
18
+ const resultOrPromise = queryFunction(selector);
19
+ const processResult = (memo, result) => {
20
+ if (!result.matched) return memo;
21
+ const optimizedSelector = result.keepSelector ? memo.optimizedSelector : Object.fromEntries(Object.entries(memo.optimizedSelector).filter(([key]) => !result.fields.includes(key)));
22
+ return {
23
+ matched: true,
24
+ ids: [...new Set(memo.matched ? intersection(memo.ids, result.ids) : result.ids)],
25
+ optimizedSelector
26
+ };
27
+ };
28
+ if (resultOrPromise instanceof Promise) return resultOrPromise.then(async (result) => {
29
+ return processResult(memoOrPromise instanceof Promise ? await memoOrPromise : memoOrPromise, result);
19
30
  });
20
- return result;
21
- }
22
- if (value instanceof Set) {
23
- const result = /* @__PURE__ */ new Set();
24
- value.forEach((currentValue) => {
25
- result.add(clone(currentValue));
31
+ const memo = memoOrPromise;
32
+ if (memo instanceof Promise) throw new Error("Mixing async and sync index providers is not supported");
33
+ return processResult(memo, resultOrPromise);
34
+ }, {
35
+ matched: false,
36
+ ids: [],
37
+ optimizedSelector: { ...selector }
38
+ });
39
+ }
40
+ /**
41
+ * Optimizes a logic gate ($and/$or) of selectors by querying multiple index providers.
42
+ * @param queryFunctions - An array of index providers to query.
43
+ * @param logicGate - An array of selectors representing the logic gate.
44
+ * @param idsCallback - A callback function called with matched ids.
45
+ * @returns An array of optimized selectors or a promise that resolves to such an array.
46
+ */
47
+ function optimizeLogicGate(queryFunctions, logicGate, idsCallback) {
48
+ return logicGate.reduce((memoOrPromise, sel) => {
49
+ const getSelector = (indexInfo) => {
50
+ const { matched: selMatched, ids: selIds, optimizedSelector } = indexInfo;
51
+ if (selMatched) {
52
+ idsCallback(true, selIds);
53
+ if (Object.keys(optimizedSelector).length > 0) return optimizedSelector;
54
+ } else {
55
+ idsCallback(false, []);
56
+ return sel;
57
+ }
58
+ };
59
+ const indexInfoOrPromise = getIndexInfo(queryFunctions, sel);
60
+ if (indexInfoOrPromise instanceof Promise) return indexInfoOrPromise.then(async (indexInfo) => {
61
+ const memo = memoOrPromise instanceof Promise ? await memoOrPromise : memoOrPromise;
62
+ const optimizedSelector = getSelector(indexInfo);
63
+ if (optimizedSelector) memo.push(optimizedSelector);
64
+ return memo;
26
65
  });
27
- return result;
28
- }
29
- if (value instanceof RegExp) return new RegExp(value);
30
- const result = {};
31
- for (const key in value) if (Object.hasOwnProperty.call(value, key)) result[key] = clone(value[key]);
32
- return result;
66
+ const memo = memoOrPromise;
67
+ if (memo instanceof Promise) throw new Error("Mixing async and sync index providers is not supported");
68
+ const optimizedSelector = getSelector(indexInfoOrPromise);
69
+ if (optimizedSelector) memo.push(optimizedSelector);
70
+ return memo;
71
+ }, []);
33
72
  }
34
73
  /**
35
- * Creates a deep clone of an object. Uses the `structuredClone` function if available,
36
- * otherwise falls back to a manual deep clone implementation.
37
- * @template T - The type of the object to clone.
38
- * @param object - The object to deep clone.
39
- * @returns A deep copy of the provided object.
74
+ * Retrieves index information for a given complex selector by querying multiple
75
+ * index providers. Handles nested `$and` and `$or` conditions in the selector and
76
+ * optimizes the selector to minimize processing overhead.
77
+ * @template T - The type of the items in the collection.
78
+ * @template I - The type of the unique identifier for the items.
79
+ * @param queryFunctions - An array of index providers to query.
80
+ * @param selector - The complex selector used to filter items.
81
+ * @returns An object containing:
82
+ * - `matched`: A boolean indicating if the selector matched any items.
83
+ * - `ids`: An array of matched item ids.
84
+ * - `optimizedSelector`: A selector optimized based on the index results, with unused
85
+ * conditions removed.
40
86
  */
41
- function deepClone(object) {
42
- if (typeof structuredClone === "function") return structuredClone(object);
43
- /* istanbul ignore next -- @preserve */
44
- return clone(object);
87
+ function getIndexInfo(queryFunctions, selector) {
88
+ if (selector == null || Object.keys(selector).length <= 0) return {
89
+ matched: false,
90
+ ids: [],
91
+ optimizedSelector: selector
92
+ };
93
+ const { $and, $or, ...rest } = selector;
94
+ const flatInfoOrPromise = getMergedIndexInfo(queryFunctions, rest);
95
+ const processFlatInfo = (flatInfo) => {
96
+ let { matched, ids } = flatInfo;
97
+ const newSelector = flatInfo.optimizedSelector;
98
+ const $andNewOrPromise = Array.isArray($and) ? optimizeLogicGate(queryFunctions, $and, (match, selIds) => {
99
+ if (!match) return;
100
+ ids = matched ? intersection(ids, selIds) : selIds;
101
+ matched = true;
102
+ }) : void 0;
103
+ const process$and = ($andNew) => {
104
+ if ($andNew && $andNew.length > 0) newSelector.$and = $andNew;
105
+ let hasNonIndexField = false;
106
+ const matchedBefore = matched;
107
+ const idsBefore = ids;
108
+ const process$or = ($orNew) => {
109
+ if (hasNonIndexField || $orNew && $orNew.length > 0) {
110
+ newSelector.$or = $or;
111
+ matched = matchedBefore;
112
+ ids = idsBefore;
113
+ }
114
+ return {
115
+ matched,
116
+ ids: ids || [],
117
+ optimizedSelector: newSelector
118
+ };
119
+ };
120
+ const $orNewOrPromise = Array.isArray($or) ? optimizeLogicGate(queryFunctions, $or, (match, selIds) => {
121
+ if (match) {
122
+ ids = [...new Set([...ids, ...selIds])];
123
+ matched = true;
124
+ } else hasNonIndexField = true;
125
+ }) : void 0;
126
+ if ($orNewOrPromise instanceof Promise) return $orNewOrPromise.then(($orNew) => process$or($orNew));
127
+ return process$or($orNewOrPromise);
128
+ };
129
+ if ($andNewOrPromise instanceof Promise) return $andNewOrPromise.then(($andNew) => process$and($andNew));
130
+ return process$and($andNewOrPromise);
131
+ };
132
+ if (flatInfoOrPromise instanceof Promise) return flatInfoOrPromise.then(processFlatInfo);
133
+ return processFlatInfo(flatInfoOrPromise);
45
134
  }
46
135
  //#endregion
47
- export { deepClone as default };
136
+ export { getIndexInfo as default };
@@ -1,15 +1,47 @@
1
- let mingo = require("mingo");
2
- //#region src/utils/match.ts
1
+ //#region src/utils/deepClone.ts
3
2
  /**
4
- * Tests whether a given item matches a specified selector.
5
- * Uses the `mingo` library to evaluate the query.
6
- * @template T - The type of the item being tested.
7
- * @param item - The item to test against the selector.
8
- * @param selector - The query selector used to match the item.
9
- * @returns A boolean indicating whether the item matches the selector.
3
+ * Performs a deep clone of a value, supporting various types including arrays, objects,
4
+ * Maps, Sets, Dates, and RegExps. Functions are not supported and will throw an error.
5
+ * @template T - The type of the value to clone.
6
+ * @param value - The value to deep clone.
7
+ * @returns A deep copy of the provided value.
8
+ * @throws {Error} An error if the value is a function, as cloning functions is not supported.
10
9
  */
11
- function match(item, selector) {
12
- return new mingo.Query(selector).test(item);
10
+ function clone(value) {
11
+ if (typeof value === "function") throw new Error("Cloning functions is not supported");
12
+ if (value === null || typeof value !== "object") return value;
13
+ if (value instanceof Date) return new Date(value);
14
+ if (Array.isArray(value)) return value.map((item) => clone(item));
15
+ if (value instanceof Map) {
16
+ const result = /* @__PURE__ */ new Map();
17
+ value.forEach((currentValue, key) => {
18
+ result.set(key, clone(currentValue));
19
+ });
20
+ return result;
21
+ }
22
+ if (value instanceof Set) {
23
+ const result = /* @__PURE__ */ new Set();
24
+ value.forEach((currentValue) => {
25
+ result.add(clone(currentValue));
26
+ });
27
+ return result;
28
+ }
29
+ if (value instanceof RegExp) return new RegExp(value);
30
+ const result = {};
31
+ for (const key in value) if (Object.hasOwnProperty.call(value, key)) result[key] = clone(value[key]);
32
+ return result;
33
+ }
34
+ /**
35
+ * Creates a deep clone of an object. Uses the `structuredClone` function if available,
36
+ * otherwise falls back to a manual deep clone implementation.
37
+ * @template T - The type of the object to clone.
38
+ * @param object - The object to deep clone.
39
+ * @returns A deep copy of the provided object.
40
+ */
41
+ function deepClone(object) {
42
+ if (typeof structuredClone === "function") return structuredClone(object);
43
+ /* istanbul ignore next -- @preserve */
44
+ return clone(object);
13
45
  }
14
46
  //#endregion
15
- exports.default = match;
47
+ exports.default = deepClone;
package/dist/index18.mjs CHANGED
@@ -1,15 +1,47 @@
1
- import { Query } from "mingo";
2
- //#region src/utils/match.ts
1
+ //#region src/utils/deepClone.ts
3
2
  /**
4
- * Tests whether a given item matches a specified selector.
5
- * Uses the `mingo` library to evaluate the query.
6
- * @template T - The type of the item being tested.
7
- * @param item - The item to test against the selector.
8
- * @param selector - The query selector used to match the item.
9
- * @returns A boolean indicating whether the item matches the selector.
3
+ * Performs a deep clone of a value, supporting various types including arrays, objects,
4
+ * Maps, Sets, Dates, and RegExps. Functions are not supported and will throw an error.
5
+ * @template T - The type of the value to clone.
6
+ * @param value - The value to deep clone.
7
+ * @returns A deep copy of the provided value.
8
+ * @throws {Error} An error if the value is a function, as cloning functions is not supported.
10
9
  */
11
- function match(item, selector) {
12
- return new Query(selector).test(item);
10
+ function clone(value) {
11
+ if (typeof value === "function") throw new Error("Cloning functions is not supported");
12
+ if (value === null || typeof value !== "object") return value;
13
+ if (value instanceof Date) return new Date(value);
14
+ if (Array.isArray(value)) return value.map((item) => clone(item));
15
+ if (value instanceof Map) {
16
+ const result = /* @__PURE__ */ new Map();
17
+ value.forEach((currentValue, key) => {
18
+ result.set(key, clone(currentValue));
19
+ });
20
+ return result;
21
+ }
22
+ if (value instanceof Set) {
23
+ const result = /* @__PURE__ */ new Set();
24
+ value.forEach((currentValue) => {
25
+ result.add(clone(currentValue));
26
+ });
27
+ return result;
28
+ }
29
+ if (value instanceof RegExp) return new RegExp(value);
30
+ const result = {};
31
+ for (const key in value) if (Object.hasOwnProperty.call(value, key)) result[key] = clone(value[key]);
32
+ return result;
33
+ }
34
+ /**
35
+ * Creates a deep clone of an object. Uses the `structuredClone` function if available,
36
+ * otherwise falls back to a manual deep clone implementation.
37
+ * @template T - The type of the object to clone.
38
+ * @param object - The object to deep clone.
39
+ * @returns A deep copy of the provided object.
40
+ */
41
+ function deepClone(object) {
42
+ if (typeof structuredClone === "function") return structuredClone(object);
43
+ /* istanbul ignore next -- @preserve */
44
+ return clone(object);
13
45
  }
14
46
  //#endregion
15
- export { match as default };
47
+ export { deepClone as default };
@@ -1,23 +1,15 @@
1
- const require_deepClone = require("./index17.cjs.js");
2
1
  let mingo = require("mingo");
3
- //#region src/utils/modify.ts
2
+ //#region src/utils/match.ts
4
3
  /**
5
- * Applies a modifier to an object and returns a new modified object.
6
- * @template T - The type of the object to be modified.
7
- * @param item - The object to be modified.
8
- * @param modifier - The modifier to apply. This can be any transformation logic.
9
- * @returns - Returns a new object with the modifications applied.
10
- * @example
11
- * const item = { a: 1, b: 2 }
12
- * const modifier = { $set: { b: 3, c: 4 } }
13
- * const result = modify(item, modifier)
14
- * // result: { a: 1, b: 3, c: 4 }
4
+ * Tests whether a given item matches a specified selector.
5
+ * Uses the `mingo` library to evaluate the query.
6
+ * @template T - The type of the item being tested.
7
+ * @param item - The item to test against the selector.
8
+ * @param selector - The query selector used to match the item.
9
+ * @returns A boolean indicating whether the item matches the selector.
15
10
  */
16
- function modify(item, modifier) {
17
- if (!Object.keys(modifier).some((key) => key.startsWith("$"))) return modifier;
18
- const clonedItem = require_deepClone.default(item);
19
- (0, mingo.update)(clonedItem, modifier);
20
- return clonedItem;
11
+ function match(item, selector) {
12
+ return new mingo.Query(selector).test(item);
21
13
  }
22
14
  //#endregion
23
- exports.default = modify;
15
+ exports.default = match;
package/dist/index19.mjs CHANGED
@@ -1,23 +1,15 @@
1
- import deepClone from "./index17.mjs";
2
- import { update } from "mingo";
3
- //#region src/utils/modify.ts
1
+ import { Query } from "mingo";
2
+ //#region src/utils/match.ts
4
3
  /**
5
- * Applies a modifier to an object and returns a new modified object.
6
- * @template T - The type of the object to be modified.
7
- * @param item - The object to be modified.
8
- * @param modifier - The modifier to apply. This can be any transformation logic.
9
- * @returns - Returns a new object with the modifications applied.
10
- * @example
11
- * const item = { a: 1, b: 2 }
12
- * const modifier = { $set: { b: 3, c: 4 } }
13
- * const result = modify(item, modifier)
14
- * // result: { a: 1, b: 3, c: 4 }
4
+ * Tests whether a given item matches a specified selector.
5
+ * Uses the `mingo` library to evaluate the query.
6
+ * @template T - The type of the item being tested.
7
+ * @param item - The item to test against the selector.
8
+ * @param selector - The query selector used to match the item.
9
+ * @returns A boolean indicating whether the item matches the selector.
15
10
  */
16
- function modify(item, modifier) {
17
- if (!Object.keys(modifier).some((key) => key.startsWith("$"))) return modifier;
18
- const clonedItem = deepClone(item);
19
- update(clonedItem, modifier);
20
- return clonedItem;
11
+ function match(item, selector) {
12
+ return new Query(selector).test(item);
21
13
  }
22
14
  //#endregion
23
- export { modify as default };
15
+ export { match as default };
@@ -1,37 +1,23 @@
1
- //#region src/utils/set.ts
1
+ const require_deepClone = require("./index18.cjs.js");
2
+ let mingo = require("mingo");
3
+ //#region src/utils/modify.ts
2
4
  /**
3
- * Sets a value at a specified path within an object. Creates nested structures
4
- * (arrays or objects) as needed to set the value at the correct location. Supports
5
- * deleting the key if the value is `undefined` and the `deleteIfUndefined` flag is set to `true`.
6
- * @template T - The type of the object to modify.
7
- * @template K - The type of the value to set.
8
- * @param object - The object to modify. The object is mutated directly.
9
- * @param path - The path (dot or bracket notation) where the value should be set.
10
- * @param value - The value to set at the specified path.
11
- * @param deleteIfUndefined - A boolean indicating whether to delete the key if the value is `undefined` (default: `false`).
12
- * @returns The modified object.
5
+ * Applies a modifier to an object and returns a new modified object.
6
+ * @template T - The type of the object to be modified.
7
+ * @param item - The object to be modified.
8
+ * @param modifier - The modifier to apply. This can be any transformation logic.
9
+ * @returns - Returns a new object with the modifications applied.
10
+ * @example
11
+ * const item = { a: 1, b: 2 }
12
+ * const modifier = { $set: { b: 3, c: 4 } }
13
+ * const result = modify(item, modifier)
14
+ * // result: { a: 1, b: 3, c: 4 }
13
15
  */
14
- function set(object, path, value, deleteIfUndefined = false) {
15
- if (object == null) return object;
16
- const segments = path.split(/[.[\]]/g);
17
- if (segments[0] === "") segments.shift();
18
- if (segments.at(-1) === "") segments.pop();
19
- const apply = (node) => {
20
- if (segments.length > 1) {
21
- const key = segments.shift();
22
- const nextIsNumber = !Number.isNaN(Number.parseInt(segments[0], 10));
23
- if (node[key] === void 0) node[key] = nextIsNumber ? [] : {};
24
- apply(node[key]);
25
- } else {
26
- if (deleteIfUndefined && value === void 0) {
27
- delete node[segments[0]];
28
- return;
29
- }
30
- node[segments[0]] = value;
31
- }
32
- };
33
- apply(object);
34
- return object;
16
+ function modify(item, modifier) {
17
+ if (!Object.keys(modifier).some((key) => key.startsWith("$"))) return modifier;
18
+ const clonedItem = require_deepClone.default(item);
19
+ (0, mingo.update)(clonedItem, modifier);
20
+ return clonedItem;
35
21
  }
36
22
  //#endregion
37
- exports.default = set;
23
+ exports.default = modify;