@pawover/kit 0.0.0-alpha.4 → 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.
- package/dist/hooks/react/useCreation.js +9 -1
- package/dist/hooks/react/useLatest.js +7 -0
- package/dist/hooks/react/useUnmount.js +1 -0
- package/dist/types/hooks/react/useCreation.d.ts +8 -0
- package/dist/types/hooks/react/useLatest.d.ts +7 -0
- package/dist/types/hooks/react/useUnmount.d.ts +1 -0
- package/dist/types/utils/clone.d.ts +13 -0
- package/dist/types/utils/index.d.ts +1 -0
- package/dist/types/utils/typeof.d.ts +8 -0
- package/dist/utils/clone.js +75 -0
- package/dist/utils/index.js +1 -0
- package/dist/utils/typeof.js +35 -0
- package/package.json +1 -3
|
@@ -1,5 +1,13 @@
|
|
|
1
|
-
import { isEqual } from "radashi";
|
|
2
1
|
import { useRef } from "react";
|
|
2
|
+
import { isEqual } from "src/utils";
|
|
3
|
+
/**
|
|
4
|
+
* useCreation
|
|
5
|
+
* @reference https://ahooks.js.org/zh-CN/hooks/use-creation
|
|
6
|
+
*
|
|
7
|
+
* @template T
|
|
8
|
+
* @param {() => T} factory
|
|
9
|
+
* @param {DependencyList} deps
|
|
10
|
+
*/
|
|
3
11
|
export function useCreation(factory, deps) {
|
|
4
12
|
const { current } = useRef({ deps, result: undefined, isInitialized: false });
|
|
5
13
|
if (current.isInitialized === false || !isEqual(current.deps, deps)) {
|
|
@@ -1,2 +1,10 @@
|
|
|
1
1
|
import { type DependencyList } from "react";
|
|
2
|
+
/**
|
|
3
|
+
* useCreation
|
|
4
|
+
* @reference https://ahooks.js.org/zh-CN/hooks/use-creation
|
|
5
|
+
*
|
|
6
|
+
* @template T
|
|
7
|
+
* @param {() => T} factory
|
|
8
|
+
* @param {DependencyList} deps
|
|
9
|
+
*/
|
|
2
10
|
export declare function useCreation<T>(factory: () => T, deps: DependencyList): T;
|
|
@@ -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;
|
|
@@ -27,3 +27,11 @@ export declare function isURLSearchParams<T extends Window>(value: unknown): val
|
|
|
27
27
|
export declare function isClass<T extends Class<AnyObject>>(value: unknown): value is T;
|
|
28
28
|
export declare function isInteger<T extends number>(value: unknown): value is T;
|
|
29
29
|
export declare function isIterable<T extends Iterable<unknown>>(value: unknown): value is T;
|
|
30
|
+
/**
|
|
31
|
+
* 判断给定的值是否相等
|
|
32
|
+
* @reference https://github.com/radashi-org/radashi/blob/main/src/typed/isEqual.ts
|
|
33
|
+
*
|
|
34
|
+
* @param {T} x
|
|
35
|
+
* @param {T} y
|
|
36
|
+
*/
|
|
37
|
+
export declare function isEqual<T>(x: T, y: T): boolean;
|
|
@@ -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/dist/utils/typeof.js
CHANGED
|
@@ -112,3 +112,38 @@ export function isInteger(value) {
|
|
|
112
112
|
export function isIterable(value) {
|
|
113
113
|
return isObject(value) && Symbol.iterator in value;
|
|
114
114
|
}
|
|
115
|
+
/**
|
|
116
|
+
* 判断给定的值是否相等
|
|
117
|
+
* @reference https://github.com/radashi-org/radashi/blob/main/src/typed/isEqual.ts
|
|
118
|
+
*
|
|
119
|
+
* @param {T} x
|
|
120
|
+
* @param {T} y
|
|
121
|
+
*/
|
|
122
|
+
export function isEqual(x, y) {
|
|
123
|
+
if (Object.is(x, y)) {
|
|
124
|
+
return true;
|
|
125
|
+
}
|
|
126
|
+
if (x instanceof Date && y instanceof Date) {
|
|
127
|
+
return x.getTime() === y.getTime();
|
|
128
|
+
}
|
|
129
|
+
if (x instanceof RegExp && y instanceof RegExp) {
|
|
130
|
+
return x.toString() === y.toString();
|
|
131
|
+
}
|
|
132
|
+
if (typeof x !== "object" || x === null || typeof y !== "object" || y === null) {
|
|
133
|
+
return false;
|
|
134
|
+
}
|
|
135
|
+
const keysX = Reflect.ownKeys(x);
|
|
136
|
+
const keysY = Reflect.ownKeys(y);
|
|
137
|
+
if (keysX.length !== keysY.length) {
|
|
138
|
+
return false;
|
|
139
|
+
}
|
|
140
|
+
for (const key of keysX) {
|
|
141
|
+
if (!Reflect.has(y, key)) {
|
|
142
|
+
return false;
|
|
143
|
+
}
|
|
144
|
+
if (!isEqual(x[key], y[key])) {
|
|
145
|
+
return false;
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
return true;
|
|
149
|
+
}
|
package/package.json
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
"description": "pawover's kit",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
7
|
-
"version": "0.0.0-alpha.
|
|
7
|
+
"version": "0.0.0-alpha.6",
|
|
8
8
|
"packageManager": "pnpm@10.20.0",
|
|
9
9
|
"engines": {
|
|
10
10
|
"node": ">=22.20.0"
|
|
@@ -61,7 +61,6 @@
|
|
|
61
61
|
"eslint-plugin-antfu": "^3.1.1",
|
|
62
62
|
"globals": "^16.5.0",
|
|
63
63
|
"prettier": "^3.6.2",
|
|
64
|
-
"radashi": "^12.7.0",
|
|
65
64
|
"react": "^19.2.0",
|
|
66
65
|
"rimraf": "^6.1.0",
|
|
67
66
|
"taze": "^19.9.0",
|
|
@@ -72,7 +71,6 @@
|
|
|
72
71
|
},
|
|
73
72
|
"peerDependencies": {
|
|
74
73
|
"ahooks": ">=3.9.6",
|
|
75
|
-
"radashi": ">=12.7.0",
|
|
76
74
|
"react": ">=19.2.0",
|
|
77
75
|
"vue": ">=3.5.24"
|
|
78
76
|
},
|