@pawover/kit 0.0.0-alpha.5 → 0.0.0-alpha.6
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.
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export interface CloningStrategy {
|
|
2
|
+
cloneMap: <K, V>(parent: Map<K, V>, track: (newParent: Map<K, V>) => Map<K, V>, clone: <T>(value: T) => T) => Map<K, V> | null;
|
|
3
|
+
cloneSet: <T>(parent: Set<T>, track: (newParent: Set<T>) => Set<T>, clone: <T>(value: T) => T) => Set<T> | null;
|
|
4
|
+
cloneArray: <T>(parent: readonly T[], track: (newParent: T[]) => T[], clone: <T>(value: T) => T) => T[] | null;
|
|
5
|
+
cloneObject: <T extends AnyObject>(parent: T, track: (newParent: T) => T, clone: <T>(value: T) => T) => T | null;
|
|
6
|
+
cloneOther: <T>(parent: T, track: (newParent: T) => T, clone: <T>(value: T) => T) => T | null;
|
|
7
|
+
}
|
|
8
|
+
export declare const DefaultCloningStrategy: CloningStrategy;
|
|
9
|
+
/**
|
|
10
|
+
* cloneDeep
|
|
11
|
+
* @reference https://github.com/radashi-org/radashi/blob/main/src/object/cloneDeep.ts
|
|
12
|
+
*/
|
|
13
|
+
export declare function cloneDeep<T extends AnyObject>(root: T, customStrategy?: Partial<CloningStrategy>): T;
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { isArray, isMap, isObject, isSet } from "./typeof";
|
|
2
|
+
export const DefaultCloningStrategy = {
|
|
3
|
+
cloneMap(input, track, clone) {
|
|
4
|
+
const output = track(new Map());
|
|
5
|
+
for (const [key, value] of input) {
|
|
6
|
+
output.set(key, clone(value));
|
|
7
|
+
}
|
|
8
|
+
return output;
|
|
9
|
+
},
|
|
10
|
+
cloneSet(input, track, clone) {
|
|
11
|
+
const output = track(new Set());
|
|
12
|
+
for (const value of input) {
|
|
13
|
+
output.add(clone(value));
|
|
14
|
+
}
|
|
15
|
+
return output;
|
|
16
|
+
},
|
|
17
|
+
cloneArray(input, track, clone) {
|
|
18
|
+
// Use .forEach for correct handling of sparse arrays
|
|
19
|
+
const output = track(new Array(input.length));
|
|
20
|
+
input.forEach((value, index) => {
|
|
21
|
+
output[index] = clone(value);
|
|
22
|
+
});
|
|
23
|
+
return output;
|
|
24
|
+
},
|
|
25
|
+
cloneObject(input, track, clone) {
|
|
26
|
+
const output = track(Object.create(Object.getPrototypeOf(input)));
|
|
27
|
+
for (const key of Reflect.ownKeys(input)) {
|
|
28
|
+
// By copying the property descriptors, we preserve computed
|
|
29
|
+
// properties and non-enumerable properties.
|
|
30
|
+
const descriptor = Object.getOwnPropertyDescriptor(input, key);
|
|
31
|
+
if ("value" in descriptor) {
|
|
32
|
+
descriptor.value = clone(descriptor.value);
|
|
33
|
+
}
|
|
34
|
+
Object.defineProperty(output, key, descriptor);
|
|
35
|
+
}
|
|
36
|
+
return output;
|
|
37
|
+
},
|
|
38
|
+
cloneOther(input, track) {
|
|
39
|
+
return track(input);
|
|
40
|
+
},
|
|
41
|
+
};
|
|
42
|
+
/**
|
|
43
|
+
* cloneDeep
|
|
44
|
+
* @reference https://github.com/radashi-org/radashi/blob/main/src/object/cloneDeep.ts
|
|
45
|
+
*/
|
|
46
|
+
export function cloneDeep(root, customStrategy) {
|
|
47
|
+
const strategy = { ...DefaultCloningStrategy, ...customStrategy };
|
|
48
|
+
const tracked = new Map();
|
|
49
|
+
const track = (parent, newParent) => {
|
|
50
|
+
tracked.set(parent, newParent);
|
|
51
|
+
return newParent;
|
|
52
|
+
};
|
|
53
|
+
const clone = (value) => {
|
|
54
|
+
return value && typeof value === "object" ? (tracked.get(value) ?? cloneDeep(value, strategy)) : value;
|
|
55
|
+
};
|
|
56
|
+
const cloneDeep = (parent, strategy) => {
|
|
57
|
+
const cloneParent = (isObject(parent)
|
|
58
|
+
? strategy.cloneObject
|
|
59
|
+
: isArray(parent)
|
|
60
|
+
? strategy.cloneArray
|
|
61
|
+
: isMap(parent)
|
|
62
|
+
? strategy.cloneMap
|
|
63
|
+
: isSet(parent)
|
|
64
|
+
? strategy.cloneSet
|
|
65
|
+
: strategy.cloneOther);
|
|
66
|
+
const newParent = cloneParent(parent, track.bind(null, parent), clone);
|
|
67
|
+
if (!newParent) {
|
|
68
|
+
// Use the default strategy if null is returned.
|
|
69
|
+
return cloneDeep(parent, DefaultCloningStrategy);
|
|
70
|
+
}
|
|
71
|
+
tracked.set(parent, newParent);
|
|
72
|
+
return newParent;
|
|
73
|
+
};
|
|
74
|
+
return cloneDeep(root, strategy);
|
|
75
|
+
}
|
package/dist/utils/index.js
CHANGED
package/package.json
CHANGED
package/dist/hooks/react.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from "./react/index";
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from "./react/index";
|