@signaldb/core 2.0.0-beta.1 → 2.0.0-beta.10
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/dist/.vite/manifest.json +122 -82
- package/dist/AsyncDataAdapter.d.ts +44 -7
- package/dist/AutoFetchDataAdapter.d.ts +5 -7
- package/dist/Collection/Cursor.d.ts +6 -5
- package/dist/Collection/Observer.d.ts +2 -0
- package/dist/Collection/index.d.ts +34 -25
- package/dist/Collection/types.d.ts +10 -2
- package/dist/DataAdapter.d.ts +12 -6
- package/dist/DefaultDataAdapter.d.ts +6 -6
- package/dist/WorkerDataAdapter.d.ts +3 -5
- package/dist/WorkerDataAdapterHost.d.ts +5 -5
- package/dist/createIndex.d.ts +2 -2
- package/dist/createIndexProvider.d.ts +2 -2
- package/dist/createReactivityAdapter.d.ts +2 -2
- package/dist/createStorageAdapter.d.ts +1 -1
- package/dist/getIndexInfo.d.ts +3 -4
- package/dist/index.cjs.js +33 -31
- package/dist/index.d.ts +2 -1
- package/dist/index.mjs +17 -32
- package/dist/index10.cjs.js +23 -0
- package/dist/index10.mjs +21 -17
- package/dist/index11.cjs.js +45 -0
- package/dist/index11.mjs +44 -15
- package/dist/index12.cjs.js +22 -0
- package/dist/index12.mjs +21 -350
- package/dist/index13.cjs.js +45 -0
- package/dist/index13.mjs +44 -419
- package/dist/index14.cjs.js +86 -0
- package/dist/index14.mjs +85 -184
- package/dist/index15.cjs.js +15 -0
- package/dist/index15.mjs +14 -386
- package/dist/index16.cjs.js +136 -0
- package/dist/index16.mjs +133 -577
- package/dist/index17.cjs.js +47 -0
- package/dist/index17.mjs +46 -150
- package/dist/index18.cjs.js +15 -0
- package/dist/index18.mjs +14 -26
- package/dist/index19.cjs.js +23 -0
- package/dist/index19.mjs +22 -42
- package/dist/index2.cjs.js +33 -0
- package/dist/index2.mjs +32 -257
- package/dist/index20.cjs.js +37 -0
- package/dist/index20.mjs +36 -7
- package/dist/index21.cjs.js +35 -0
- package/dist/index21.mjs +33 -103
- package/dist/index22.cjs.js +25 -0
- package/dist/index22.mjs +23 -127
- package/dist/index23.cjs.js +19 -0
- package/dist/index23.mjs +18 -7
- package/dist/index24.cjs.js +342 -0
- package/dist/index24.mjs +340 -26
- package/dist/index25.cjs.js +542 -0
- package/dist/index25.mjs +541 -10
- package/dist/index26.cjs.js +11 -0
- package/dist/index26.mjs +10 -29
- package/dist/index27.cjs.js +11 -0
- package/dist/index27.mjs +10 -46
- package/dist/index28.cjs.js +90 -0
- package/dist/index28.mjs +87 -7
- package/dist/index29.cjs.js +424 -0
- package/dist/index29.mjs +423 -9
- package/dist/index3.cjs.js +19 -0
- package/dist/index3.mjs +18 -541
- package/dist/index30.cjs.js +72 -0
- package/dist/index30.mjs +71 -5
- package/dist/index31.cjs.js +191 -0
- package/dist/index31.mjs +190 -7
- package/dist/index32.cjs.js +21 -0
- package/dist/index32.mjs +20 -29
- package/dist/index33.cjs.js +336 -0
- package/dist/index33.mjs +335 -42
- package/dist/index34.cjs.js +539 -0
- package/dist/index34.mjs +539 -0
- package/dist/index4.cjs.js +150 -0
- package/dist/index4.mjs +149 -5
- package/dist/index5.cjs.js +254 -0
- package/dist/index5.mjs +252 -4
- package/dist/index6.cjs.js +125 -0
- package/dist/index6.mjs +124 -29
- package/dist/index7.cjs.js +33 -0
- package/dist/index7.mjs +32 -12
- package/dist/index8.cjs.js +12 -0
- package/dist/index8.mjs +10 -4
- package/dist/index9.cjs.js +11 -0
- package/dist/index9.mjs +10 -133
- package/dist/types/IndexProvider.d.ts +2 -2
- package/dist/types/Modifier.d.ts +1 -1
- package/dist/types/ReactivityAdapter.d.ts +1 -1
- package/dist/utils/createSignal.d.ts +2 -2
- package/dist/utils/deepClone.d.ts +1 -1
- package/dist/utils/getMatchingKeys.d.ts +2 -2
- package/dist/utils/isFieldExpression.d.ts +1 -1
- package/dist/utils/match.d.ts +1 -1
- package/dist/utils/modify.d.ts +1 -1
- package/dist/utils/queryId.d.ts +2 -2
- package/dist/utils/reactiveOrAsync.d.ts +59 -0
- package/package.json +3 -3
- package/dist/index.cjs10.js +0 -18
- package/dist/index.cjs11.js +0 -15
- package/dist/index.cjs12.js +0 -350
- package/dist/index.cjs13.js +0 -419
- package/dist/index.cjs14.js +0 -184
- package/dist/index.cjs15.js +0 -386
- package/dist/index.cjs16.js +0 -579
- package/dist/index.cjs17.js +0 -150
- package/dist/index.cjs18.js +0 -26
- package/dist/index.cjs19.js +0 -43
- package/dist/index.cjs2.js +0 -258
- package/dist/index.cjs20.js +0 -7
- package/dist/index.cjs21.js +0 -104
- package/dist/index.cjs22.js +0 -129
- package/dist/index.cjs23.js +0 -7
- package/dist/index.cjs24.js +0 -27
- package/dist/index.cjs25.js +0 -10
- package/dist/index.cjs26.js +0 -29
- package/dist/index.cjs27.js +0 -46
- package/dist/index.cjs28.js +0 -8
- package/dist/index.cjs29.js +0 -9
- package/dist/index.cjs3.js +0 -542
- package/dist/index.cjs30.js +0 -5
- package/dist/index.cjs31.js +0 -7
- package/dist/index.cjs32.js +0 -29
- package/dist/index.cjs33.js +0 -42
- package/dist/index.cjs4.js +0 -5
- package/dist/index.cjs5.js +0 -5
- package/dist/index.cjs6.js +0 -29
- package/dist/index.cjs7.js +0 -12
- package/dist/index.cjs8.js +0 -5
- package/dist/index.cjs9.js +0 -133
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
//#region src/utils/deepClone.ts
|
|
2
|
+
/**
|
|
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.
|
|
9
|
+
*/
|
|
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);
|
|
45
|
+
}
|
|
46
|
+
//#endregion
|
|
47
|
+
exports.default = deepClone;
|
package/dist/index17.mjs
CHANGED
|
@@ -1,151 +1,47 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
/**
|
|
34
|
-
* Determines if the observer has no active callbacks registered for any events.
|
|
35
|
-
* @returns A boolean indicating whether the observer is empty (i.e., no callbacks are registered).
|
|
36
|
-
*/
|
|
37
|
-
isEmpty() {
|
|
38
|
-
return !this.hasCallbacks([
|
|
39
|
-
"added",
|
|
40
|
-
"addedBefore",
|
|
41
|
-
"changed",
|
|
42
|
-
"changedField",
|
|
43
|
-
"movedBefore",
|
|
44
|
-
"removed"
|
|
45
|
-
]);
|
|
46
|
-
}
|
|
47
|
-
/**
|
|
48
|
-
* Compares the previous state of items with the new state and triggers the appropriate callbacks
|
|
49
|
-
* for events such as added, removed, changed, or moved items.
|
|
50
|
-
* @param getItems - A function that returns a promise resolving to the new items or the items themselves.
|
|
51
|
-
*/
|
|
52
|
-
runChecks(getItems) {
|
|
53
|
-
const result = getItems();
|
|
54
|
-
if (result instanceof Promise) {
|
|
55
|
-
void result.then((newItems) => this.checkItems(newItems));
|
|
56
|
-
} else {
|
|
57
|
-
this.checkItems(result);
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
checkItems(newItems) {
|
|
61
|
-
const oldItemsMap = new Map(this.previousItems.map((item, index) => [
|
|
62
|
-
item.id,
|
|
63
|
-
{ item, index, beforeItem: this.previousItems[index + 1] || null }
|
|
64
|
-
]));
|
|
65
|
-
const newItemsMap = new Map(newItems.map((item, index) => [
|
|
66
|
-
item.id,
|
|
67
|
-
{ item, index, beforeItem: newItems[index + 1] || null }
|
|
68
|
-
]));
|
|
69
|
-
if (this.hasCallbacks(["changed", "changedField", "movedBefore", "removed"])) {
|
|
70
|
-
oldItemsMap.forEach(({ item: oldItem, index, beforeItem: oldBeforeItem }) => {
|
|
71
|
-
const newItem = newItemsMap.get(oldItem.id);
|
|
72
|
-
if (newItem) {
|
|
73
|
-
if (this.hasCallbacks(["changed", "changedField"]) && !isEqual(newItem.item, oldItem)) {
|
|
74
|
-
this.call("changed", newItem.item);
|
|
75
|
-
if (this.hasCallbacks(["changedField"])) {
|
|
76
|
-
const keys = uniqueBy([
|
|
77
|
-
...Object.keys(newItem.item),
|
|
78
|
-
...Object.keys(oldItem)
|
|
79
|
-
], (value) => value);
|
|
80
|
-
keys.forEach((key) => {
|
|
81
|
-
if (isEqual(newItem.item[key], oldItem[key]))
|
|
82
|
-
return;
|
|
83
|
-
this.call("changedField", newItem.item, key, oldItem[key], newItem.item[key]);
|
|
84
|
-
});
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
if (newItem.index !== index && newItem.beforeItem?.id !== oldBeforeItem?.id) {
|
|
88
|
-
this.call("movedBefore", newItem.item, newItem.beforeItem);
|
|
89
|
-
}
|
|
90
|
-
} else {
|
|
91
|
-
this.call("removed", oldItem);
|
|
92
|
-
}
|
|
93
|
-
});
|
|
94
|
-
}
|
|
95
|
-
if (this.hasCallbacks(["added", "addedBefore"])) {
|
|
96
|
-
newItems.forEach((newItem, index) => {
|
|
97
|
-
const oldItem = oldItemsMap.get(newItem.id);
|
|
98
|
-
if (oldItem)
|
|
99
|
-
return;
|
|
100
|
-
this.call("added", newItem);
|
|
101
|
-
this.call("addedBefore", newItem, newItems[index + 1] || null);
|
|
102
|
-
});
|
|
103
|
-
}
|
|
104
|
-
this.previousItems = newItems;
|
|
105
|
-
Object.keys(this.callbacks).forEach((key) => {
|
|
106
|
-
const event = key;
|
|
107
|
-
const callbacks = this.callbacks[event];
|
|
108
|
-
this.callbacks[event] = callbacks.map((callback) => ({
|
|
109
|
-
...callback,
|
|
110
|
-
options: {
|
|
111
|
-
...callback.options,
|
|
112
|
-
isInitial: false
|
|
113
|
-
}
|
|
114
|
-
}));
|
|
115
|
-
});
|
|
116
|
-
}
|
|
117
|
-
/**
|
|
118
|
-
* Stops the observer by unbinding all events and cleaning up resources.
|
|
119
|
-
*/
|
|
120
|
-
stop() {
|
|
121
|
-
this.unbindEvents();
|
|
122
|
-
}
|
|
123
|
-
/**
|
|
124
|
-
* Registers callbacks for specific events to observe changes in the collection.
|
|
125
|
-
* @param callbacks - An object containing the callbacks for various events (e.g., 'added', 'removed').
|
|
126
|
-
* @param skipInitial - A boolean indicating whether to skip invoking the callbacks for the initial state of the collection.
|
|
127
|
-
*/
|
|
128
|
-
addCallbacks(callbacks, skipInitial = false) {
|
|
129
|
-
Object.keys(callbacks).forEach((key) => {
|
|
130
|
-
const typedKey = key;
|
|
131
|
-
this.callbacks[typedKey].push({
|
|
132
|
-
callback: callbacks[typedKey],
|
|
133
|
-
options: { skipInitial, isInitial: true }
|
|
134
|
-
});
|
|
135
|
-
});
|
|
136
|
-
}
|
|
137
|
-
/**
|
|
138
|
-
* Removes the specified callbacks for specific events, unregistering them from the observer.
|
|
139
|
-
* @param callbacks - An object containing the callbacks to be removed for various events.
|
|
140
|
-
*/
|
|
141
|
-
removeCallbacks(callbacks) {
|
|
142
|
-
Object.keys(callbacks).forEach((key) => {
|
|
143
|
-
const typedKey = key;
|
|
144
|
-
const index = this.callbacks[typedKey].findIndex(({ callback }) => callback === callbacks[typedKey]);
|
|
145
|
-
this.callbacks[typedKey].splice(index, 1);
|
|
146
|
-
});
|
|
147
|
-
}
|
|
1
|
+
//#region src/utils/deepClone.ts
|
|
2
|
+
/**
|
|
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.
|
|
9
|
+
*/
|
|
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;
|
|
148
33
|
}
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
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);
|
|
45
|
+
}
|
|
46
|
+
//#endregion
|
|
47
|
+
export { deepClone as default };
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
let mingo = require("mingo");
|
|
2
|
+
//#region src/utils/match.ts
|
|
3
|
+
/**
|
|
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.
|
|
10
|
+
*/
|
|
11
|
+
function match(item, selector) {
|
|
12
|
+
return new mingo.Query(selector).test(item);
|
|
13
|
+
}
|
|
14
|
+
//#endregion
|
|
15
|
+
exports.default = match;
|
package/dist/index18.mjs
CHANGED
|
@@ -1,27 +1,15 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
return value;
|
|
14
|
-
},
|
|
15
|
-
set(newValue) {
|
|
16
|
-
if (isEqual(value, newValue))
|
|
17
|
-
return;
|
|
18
|
-
value = newValue;
|
|
19
|
-
if (dependency)
|
|
20
|
-
dependency.notify();
|
|
21
|
-
}
|
|
22
|
-
};
|
|
23
|
-
return signal;
|
|
1
|
+
import { Query } from "mingo";
|
|
2
|
+
//#region src/utils/match.ts
|
|
3
|
+
/**
|
|
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.
|
|
10
|
+
*/
|
|
11
|
+
function match(item, selector) {
|
|
12
|
+
return new Query(selector).test(item);
|
|
24
13
|
}
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
};
|
|
14
|
+
//#endregion
|
|
15
|
+
export { match as default };
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
const require_deepClone = require("./index17.cjs.js");
|
|
2
|
+
let mingo = require("mingo");
|
|
3
|
+
//#region src/utils/modify.ts
|
|
4
|
+
/**
|
|
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 }
|
|
15
|
+
*/
|
|
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;
|
|
21
|
+
}
|
|
22
|
+
//#endregion
|
|
23
|
+
exports.default = modify;
|
package/dist/index19.mjs
CHANGED
|
@@ -1,43 +1,23 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
});
|
|
22
|
-
return result2;
|
|
23
|
-
}
|
|
24
|
-
if (value instanceof RegExp)
|
|
25
|
-
return new RegExp(value);
|
|
26
|
-
const result = {};
|
|
27
|
-
for (const key in value) {
|
|
28
|
-
if (Object.hasOwnProperty.call(value, key)) {
|
|
29
|
-
result[key] = clone(value[key]);
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
return result;
|
|
1
|
+
import deepClone from "./index17.mjs";
|
|
2
|
+
import { update } from "mingo";
|
|
3
|
+
//#region src/utils/modify.ts
|
|
4
|
+
/**
|
|
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 }
|
|
15
|
+
*/
|
|
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;
|
|
33
21
|
}
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
return structuredClone(object);
|
|
37
|
-
/* istanbul ignore next -- @preserve */
|
|
38
|
-
return clone(object);
|
|
39
|
-
}
|
|
40
|
-
export {
|
|
41
|
-
clone,
|
|
42
|
-
deepClone as default
|
|
43
|
-
};
|
|
22
|
+
//#endregion
|
|
23
|
+
export { modify as default };
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
//#region src/utils/isEqual.ts
|
|
2
|
+
/**
|
|
3
|
+
* Compares two values for deep equality.
|
|
4
|
+
* @param a - The first value to compare.
|
|
5
|
+
* @param b - The second value to compare.
|
|
6
|
+
* @returns - Returns `true` if the two values are deeply equal, otherwise `false`.
|
|
7
|
+
* @example
|
|
8
|
+
* isEqual({ a: 1 }, { a: 1 }); // true
|
|
9
|
+
* isEqual([1, 2], [1, 2]); // true
|
|
10
|
+
* isEqual(new Date(0), new Date(0)); // true
|
|
11
|
+
* isEqual(/abc/, /abc/); // true
|
|
12
|
+
* isEqual({ a: 1 }, { a: 2 }); // false
|
|
13
|
+
* isEqual(null, null); // true
|
|
14
|
+
*/
|
|
15
|
+
function isEqual(a, b) {
|
|
16
|
+
if (Object.is(a, b)) return true;
|
|
17
|
+
if (a instanceof RegExp && b instanceof RegExp) return a.toString() === b.toString();
|
|
18
|
+
if (a instanceof Date && b instanceof Date) return a.getTime() === b.getTime();
|
|
19
|
+
if (typeof a !== "object") return false;
|
|
20
|
+
if (typeof b !== "object") return false;
|
|
21
|
+
if (a === null) return false;
|
|
22
|
+
if (b === null) return false;
|
|
23
|
+
const aKeys = Object.keys(a);
|
|
24
|
+
const bKeys = Object.keys(b);
|
|
25
|
+
if (aKeys.length !== bKeys.length) return false;
|
|
26
|
+
for (const key of aKeys) {
|
|
27
|
+
if (!bKeys.includes(key)) return false;
|
|
28
|
+
if (!isEqual(a[key], b[key])) return false;
|
|
29
|
+
}
|
|
30
|
+
return true;
|
|
31
|
+
}
|
|
32
|
+
//#endregion
|
|
33
|
+
exports.default = isEqual;
|