@composy/store-core 0.0.1
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/CHANGELOG.md +5 -0
- package/README.md +172 -0
- package/dist/index.js +1852 -0
- package/dist/index.js.map +1 -0
- package/dist/index.min.js +1082 -0
- package/dist/index.min.js.map +1 -0
- package/es/core/async-action.d.ts +95 -0
- package/es/core/async-action.d.ts.map +1 -0
- package/es/core/async-action.js +116 -0
- package/es/core/async-action.js.map +1 -0
- package/es/core/base-store.d.ts +85 -0
- package/es/core/base-store.d.ts.map +1 -0
- package/es/core/base-store.js +185 -0
- package/es/core/base-store.js.map +1 -0
- package/es/core/batch-manager.d.ts +46 -0
- package/es/core/batch-manager.d.ts.map +1 -0
- package/es/core/batch-manager.js +64 -0
- package/es/core/batch-manager.js.map +1 -0
- package/es/core/create-store.d.ts +66 -0
- package/es/core/create-store.d.ts.map +1 -0
- package/es/core/create-store.js +29 -0
- package/es/core/create-store.js.map +1 -0
- package/es/core/event-bus.d.ts +34 -0
- package/es/core/event-bus.d.ts.map +1 -0
- package/es/core/event-bus.js +71 -0
- package/es/core/event-bus.js.map +1 -0
- package/es/core/index.d.ts +14 -0
- package/es/core/index.d.ts.map +1 -0
- package/es/core/index.js +17 -0
- package/es/core/pub-sub.d.ts +41 -0
- package/es/core/pub-sub.d.ts.map +1 -0
- package/es/core/pub-sub.js +71 -0
- package/es/core/pub-sub.js.map +1 -0
- package/es/engine/index.d.ts +10 -0
- package/es/engine/index.d.ts.map +1 -0
- package/es/engine/index.js +12 -0
- package/es/engine/plugin.d.ts +20 -0
- package/es/engine/plugin.d.ts.map +1 -0
- package/es/engine/plugin.js +153 -0
- package/es/engine/plugin.js.map +1 -0
- package/es/engine/types.d.ts +81 -0
- package/es/engine/types.d.ts.map +1 -0
- package/es/engine/types.js +10 -0
- package/es/errors/index.d.ts +208 -0
- package/es/errors/index.d.ts.map +1 -0
- package/es/errors/index.js +217 -0
- package/es/errors/index.js.map +1 -0
- package/es/index.d.ts +27 -0
- package/es/index.d.ts.map +1 -0
- package/es/index.js +34 -0
- package/es/index.js.map +1 -0
- package/es/middleware/index.d.ts +13 -0
- package/es/middleware/index.d.ts.map +1 -0
- package/es/middleware/index.js +16 -0
- package/es/middleware/logger.d.ts +67 -0
- package/es/middleware/logger.d.ts.map +1 -0
- package/es/middleware/logger.js +136 -0
- package/es/middleware/logger.js.map +1 -0
- package/es/middleware/middleware-manager.d.ts +40 -0
- package/es/middleware/middleware-manager.d.ts.map +1 -0
- package/es/middleware/middleware-manager.js +79 -0
- package/es/middleware/middleware-manager.js.map +1 -0
- package/es/middleware/persist.d.ts +34 -0
- package/es/middleware/persist.d.ts.map +1 -0
- package/es/middleware/persist.js +102 -0
- package/es/middleware/persist.js.map +1 -0
- package/es/middleware/time-travel.d.ts +30 -0
- package/es/middleware/time-travel.d.ts.map +1 -0
- package/es/middleware/time-travel.js +151 -0
- package/es/middleware/time-travel.js.map +1 -0
- package/es/middleware/validation.d.ts +100 -0
- package/es/middleware/validation.d.ts.map +1 -0
- package/es/middleware/validation.js +74 -0
- package/es/middleware/validation.js.map +1 -0
- package/es/plugins/define-plugin.d.ts +74 -0
- package/es/plugins/define-plugin.d.ts.map +1 -0
- package/es/plugins/define-plugin.js +52 -0
- package/es/plugins/define-plugin.js.map +1 -0
- package/es/plugins/index.d.ts +8 -0
- package/es/plugins/index.d.ts.map +1 -0
- package/es/plugins/index.js +13 -0
- package/es/plugins/plugin-manager.d.ts +35 -0
- package/es/plugins/plugin-manager.d.ts.map +1 -0
- package/es/plugins/plugin-manager.js +77 -0
- package/es/plugins/plugin-manager.js.map +1 -0
- package/es/types/index.d.ts +12 -0
- package/es/types/index.d.ts.map +1 -0
- package/es/types/index.js +10 -0
- package/es/types/middleware.d.ts +172 -0
- package/es/types/middleware.d.ts.map +1 -0
- package/es/types/middleware.js +10 -0
- package/es/types/plugin.d.ts +136 -0
- package/es/types/plugin.d.ts.map +1 -0
- package/es/types/plugin.js +10 -0
- package/es/types/store.d.ts +198 -0
- package/es/types/store.d.ts.map +1 -0
- package/es/types/store.js +10 -0
- package/es/types/subscription.d.ts +158 -0
- package/es/types/subscription.d.ts.map +1 -0
- package/es/types/subscription.js +10 -0
- package/es/types/utility.d.ts +312 -0
- package/es/types/utility.d.ts.map +1 -0
- package/es/types/utility.js +10 -0
- package/es/utils/debounce-throttle.d.ts +86 -0
- package/es/utils/debounce-throttle.d.ts.map +1 -0
- package/es/utils/debounce-throttle.js +86 -0
- package/es/utils/debounce-throttle.js.map +1 -0
- package/es/utils/deep-clone.d.ts +143 -0
- package/es/utils/deep-clone.d.ts.map +1 -0
- package/es/utils/deep-clone.js +193 -0
- package/es/utils/deep-clone.js.map +1 -0
- package/es/utils/index.d.ts +13 -0
- package/es/utils/index.d.ts.map +1 -0
- package/es/utils/index.js +14 -0
- package/es/utils/selector.d.ts +213 -0
- package/es/utils/selector.d.ts.map +1 -0
- package/es/utils/selector.js +151 -0
- package/es/utils/selector.js.map +1 -0
- package/esm/core/async-action.d.ts +95 -0
- package/esm/core/async-action.js +116 -0
- package/esm/core/async-action.js.map +1 -0
- package/esm/core/base-store.d.ts +85 -0
- package/esm/core/base-store.js +185 -0
- package/esm/core/base-store.js.map +1 -0
- package/esm/core/batch-manager.d.ts +46 -0
- package/esm/core/batch-manager.js +64 -0
- package/esm/core/batch-manager.js.map +1 -0
- package/esm/core/create-store.d.ts +66 -0
- package/esm/core/create-store.js +29 -0
- package/esm/core/create-store.js.map +1 -0
- package/esm/core/event-bus.d.ts +34 -0
- package/esm/core/event-bus.js +71 -0
- package/esm/core/event-bus.js.map +1 -0
- package/esm/core/index.d.ts +14 -0
- package/esm/core/index.js +17 -0
- package/esm/core/pub-sub.d.ts +41 -0
- package/esm/core/pub-sub.js +71 -0
- package/esm/core/pub-sub.js.map +1 -0
- package/esm/engine/index.d.ts +10 -0
- package/esm/engine/index.js +12 -0
- package/esm/engine/plugin.d.ts +20 -0
- package/esm/engine/plugin.js +153 -0
- package/esm/engine/plugin.js.map +1 -0
- package/esm/engine/types.d.ts +81 -0
- package/esm/errors/index.d.ts +208 -0
- package/esm/errors/index.js +217 -0
- package/esm/errors/index.js.map +1 -0
- package/esm/index.d.ts +27 -0
- package/esm/index.js +36 -0
- package/esm/index.js.map +1 -0
- package/esm/middleware/index.d.ts +13 -0
- package/esm/middleware/logger.d.ts +67 -0
- package/esm/middleware/logger.js +119 -0
- package/esm/middleware/logger.js.map +1 -0
- package/esm/middleware/middleware-manager.d.ts +40 -0
- package/esm/middleware/middleware-manager.js +79 -0
- package/esm/middleware/middleware-manager.js.map +1 -0
- package/esm/middleware/persist.d.ts +34 -0
- package/esm/middleware/persist.js +102 -0
- package/esm/middleware/persist.js.map +1 -0
- package/esm/middleware/time-travel.d.ts +30 -0
- package/esm/middleware/time-travel.js +151 -0
- package/esm/middleware/time-travel.js.map +1 -0
- package/esm/middleware/validation.d.ts +100 -0
- package/esm/middleware/validation.js +74 -0
- package/esm/middleware/validation.js.map +1 -0
- package/esm/plugins/define-plugin.d.ts +74 -0
- package/esm/plugins/define-plugin.js +52 -0
- package/esm/plugins/define-plugin.js.map +1 -0
- package/esm/plugins/index.d.ts +8 -0
- package/esm/plugins/plugin-manager.d.ts +35 -0
- package/esm/plugins/plugin-manager.js +77 -0
- package/esm/plugins/plugin-manager.js.map +1 -0
- package/esm/types/index.d.ts +12 -0
- package/esm/types/middleware.d.ts +172 -0
- package/esm/types/plugin.d.ts +136 -0
- package/esm/types/store.d.ts +198 -0
- package/esm/types/subscription.d.ts +158 -0
- package/esm/types/utility.d.ts +312 -0
- package/esm/utils/debounce-throttle.d.ts +86 -0
- package/esm/utils/debounce-throttle.js +86 -0
- package/esm/utils/debounce-throttle.js.map +1 -0
- package/esm/utils/deep-clone.d.ts +143 -0
- package/esm/utils/deep-clone.js +193 -0
- package/esm/utils/deep-clone.js.map +1 -0
- package/esm/utils/index.d.ts +13 -0
- package/esm/utils/selector.d.ts +213 -0
- package/esm/utils/selector.js +151 -0
- package/esm/utils/selector.js.map +1 -0
- package/lib/core/async-action.cjs +118 -0
- package/lib/core/async-action.cjs.map +1 -0
- package/lib/core/async-action.d.ts +95 -0
- package/lib/core/base-store.cjs +187 -0
- package/lib/core/base-store.cjs.map +1 -0
- package/lib/core/base-store.d.ts +85 -0
- package/lib/core/batch-manager.cjs +69 -0
- package/lib/core/batch-manager.cjs.map +1 -0
- package/lib/core/batch-manager.d.ts +46 -0
- package/lib/core/create-store.cjs +31 -0
- package/lib/core/create-store.cjs.map +1 -0
- package/lib/core/create-store.d.ts +66 -0
- package/lib/core/event-bus.cjs +74 -0
- package/lib/core/event-bus.cjs.map +1 -0
- package/lib/core/event-bus.d.ts +34 -0
- package/lib/core/index.cjs +29 -0
- package/lib/core/index.d.ts +14 -0
- package/lib/core/pub-sub.cjs +74 -0
- package/lib/core/pub-sub.cjs.map +1 -0
- package/lib/core/pub-sub.d.ts +41 -0
- package/lib/engine/index.cjs +16 -0
- package/lib/engine/index.d.ts +10 -0
- package/lib/engine/plugin.cjs +157 -0
- package/lib/engine/plugin.cjs.map +1 -0
- package/lib/engine/plugin.d.ts +20 -0
- package/lib/engine/types.cjs +12 -0
- package/lib/engine/types.d.ts +81 -0
- package/lib/errors/index.cjs +233 -0
- package/lib/errors/index.cjs.map +1 -0
- package/lib/errors/index.d.ts +208 -0
- package/lib/index.cjs +101 -0
- package/lib/index.cjs.map +1 -0
- package/lib/index.d.ts +27 -0
- package/lib/middleware/index.cjs +28 -0
- package/lib/middleware/index.d.ts +13 -0
- package/lib/middleware/logger.cjs +140 -0
- package/lib/middleware/logger.cjs.map +1 -0
- package/lib/middleware/logger.d.ts +67 -0
- package/lib/middleware/middleware-manager.cjs +83 -0
- package/lib/middleware/middleware-manager.cjs.map +1 -0
- package/lib/middleware/middleware-manager.d.ts +40 -0
- package/lib/middleware/persist.cjs +105 -0
- package/lib/middleware/persist.cjs.map +1 -0
- package/lib/middleware/persist.d.ts +34 -0
- package/lib/middleware/time-travel.cjs +154 -0
- package/lib/middleware/time-travel.cjs.map +1 -0
- package/lib/middleware/time-travel.d.ts +30 -0
- package/lib/middleware/validation.cjs +76 -0
- package/lib/middleware/validation.cjs.map +1 -0
- package/lib/middleware/validation.d.ts +100 -0
- package/lib/package.json +3 -0
- package/lib/plugins/define-plugin.cjs +56 -0
- package/lib/plugins/define-plugin.cjs.map +1 -0
- package/lib/plugins/define-plugin.d.ts +74 -0
- package/lib/plugins/index.cjs +20 -0
- package/lib/plugins/index.d.ts +8 -0
- package/lib/plugins/plugin-manager.cjs +81 -0
- package/lib/plugins/plugin-manager.cjs.map +1 -0
- package/lib/plugins/plugin-manager.d.ts +35 -0
- package/lib/types/index.cjs +12 -0
- package/lib/types/index.d.ts +12 -0
- package/lib/types/middleware.cjs +12 -0
- package/lib/types/middleware.d.ts +172 -0
- package/lib/types/plugin.cjs +12 -0
- package/lib/types/plugin.d.ts +136 -0
- package/lib/types/store.cjs +12 -0
- package/lib/types/store.d.ts +198 -0
- package/lib/types/subscription.cjs +12 -0
- package/lib/types/subscription.d.ts +158 -0
- package/lib/types/utility.cjs +12 -0
- package/lib/types/utility.d.ts +312 -0
- package/lib/utils/debounce-throttle.cjs +89 -0
- package/lib/utils/debounce-throttle.cjs.map +1 -0
- package/lib/utils/debounce-throttle.d.ts +86 -0
- package/lib/utils/deep-clone.cjs +202 -0
- package/lib/utils/deep-clone.cjs.map +1 -0
- package/lib/utils/deep-clone.d.ts +143 -0
- package/lib/utils/index.cjs +34 -0
- package/lib/utils/index.d.ts +13 -0
- package/lib/utils/selector.cjs +161 -0
- package/lib/utils/selector.cjs.map +1 -0
- package/lib/utils/selector.d.ts +213 -0
- package/package.json +155 -0
|
@@ -0,0 +1,312 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 工具类型定义
|
|
3
|
+
*
|
|
4
|
+
* 提供常用的类型工具,用于增强类型安全
|
|
5
|
+
* @module types/utility
|
|
6
|
+
*/
|
|
7
|
+
/**
|
|
8
|
+
* 深度部分类型
|
|
9
|
+
*
|
|
10
|
+
* 将对象的所有属性(包括嵌套属性)设为可选
|
|
11
|
+
*
|
|
12
|
+
* @template T - 原始类型
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* ```typescript
|
|
16
|
+
* interface User {
|
|
17
|
+
* name: string
|
|
18
|
+
* address: { city: string; zip: string }
|
|
19
|
+
* }
|
|
20
|
+
*
|
|
21
|
+
* type PartialUser = DeepPartial<User>
|
|
22
|
+
* // { name?: string; address?: { city?: string; zip?: string } }
|
|
23
|
+
* ```
|
|
24
|
+
*/
|
|
25
|
+
export type DeepPartial<T> = T extends object ? T extends Array<infer U> ? Array<DeepPartial<U>> : T extends Map<infer K, infer V> ? Map<DeepPartial<K>, DeepPartial<V>> : T extends Set<infer U> ? Set<DeepPartial<U>> : T extends Function ? T : {
|
|
26
|
+
[P in keyof T]?: DeepPartial<T[P]>;
|
|
27
|
+
} : T;
|
|
28
|
+
/**
|
|
29
|
+
* 深度只读类型
|
|
30
|
+
*
|
|
31
|
+
* 将对象的所有属性(包括嵌套属性)设为只读
|
|
32
|
+
*
|
|
33
|
+
* @template T - 原始类型
|
|
34
|
+
*
|
|
35
|
+
* @example
|
|
36
|
+
* ```typescript
|
|
37
|
+
* interface Config {
|
|
38
|
+
* api: { url: string; timeout: number }
|
|
39
|
+
* }
|
|
40
|
+
*
|
|
41
|
+
* type ReadonlyConfig = DeepReadonly<Config>
|
|
42
|
+
* // { readonly api: { readonly url: string; readonly timeout: number } }
|
|
43
|
+
* ```
|
|
44
|
+
*/
|
|
45
|
+
export type DeepReadonly<T> = T extends object ? T extends Array<infer U> ? ReadonlyArray<DeepReadonly<U>> : T extends Map<infer K, infer V> ? ReadonlyMap<DeepReadonly<K>, DeepReadonly<V>> : T extends Set<infer U> ? ReadonlySet<DeepReadonly<U>> : T extends Function ? T : {
|
|
46
|
+
readonly [P in keyof T]: DeepReadonly<T[P]>;
|
|
47
|
+
} : T;
|
|
48
|
+
/**
|
|
49
|
+
* 只读 Map 类型
|
|
50
|
+
*/
|
|
51
|
+
type ReadonlyMap<K, V> = Omit<Map<K, V>, 'set' | 'delete' | 'clear'>;
|
|
52
|
+
/**
|
|
53
|
+
* 只读 Set 类型
|
|
54
|
+
*/
|
|
55
|
+
type ReadonlySet<T> = Omit<Set<T>, 'add' | 'delete' | 'clear'>;
|
|
56
|
+
/**
|
|
57
|
+
* 获取对象的所有路径
|
|
58
|
+
*
|
|
59
|
+
* @template T - 对象类型
|
|
60
|
+
*
|
|
61
|
+
* @example
|
|
62
|
+
* ```typescript
|
|
63
|
+
* interface State {
|
|
64
|
+
* user: { name: string; age: number }
|
|
65
|
+
* settings: { theme: string }
|
|
66
|
+
* }
|
|
67
|
+
*
|
|
68
|
+
* type Paths = PathsOf<State>
|
|
69
|
+
* // 'user' | 'user.name' | 'user.age' | 'settings' | 'settings.theme'
|
|
70
|
+
* ```
|
|
71
|
+
*/
|
|
72
|
+
export type PathsOf<T, Prefix extends string = ''> = T extends object ? T extends Array<unknown> ? Prefix : {
|
|
73
|
+
[K in keyof T & string]: K extends string ? Prefix extends '' ? K | PathsOf<T[K], K> : `${Prefix}.${K}` | PathsOf<T[K], `${Prefix}.${K}`> : never;
|
|
74
|
+
}[keyof T & string] : Prefix;
|
|
75
|
+
/**
|
|
76
|
+
* 根据路径获取类型
|
|
77
|
+
*
|
|
78
|
+
* @template T - 对象类型
|
|
79
|
+
* @template P - 路径字符串
|
|
80
|
+
*
|
|
81
|
+
* @example
|
|
82
|
+
* ```typescript
|
|
83
|
+
* interface State {
|
|
84
|
+
* user: { name: string; age: number }
|
|
85
|
+
* }
|
|
86
|
+
*
|
|
87
|
+
* type UserName = PathValue<State, 'user.name'> // string
|
|
88
|
+
* type User = PathValue<State, 'user'> // { name: string; age: number }
|
|
89
|
+
* ```
|
|
90
|
+
*/
|
|
91
|
+
export type PathValue<T, P extends string> = P extends `${infer K}.${infer Rest}` ? K extends keyof T ? PathValue<T[K], Rest> : never : P extends keyof T ? T[P] : never;
|
|
92
|
+
/**
|
|
93
|
+
* 提取函数的参数类型
|
|
94
|
+
*
|
|
95
|
+
* @template T - 函数类型
|
|
96
|
+
*/
|
|
97
|
+
export type ArgumentsOf<T extends (...args: unknown[]) => unknown> = T extends (...args: infer P) => unknown ? P : never;
|
|
98
|
+
/**
|
|
99
|
+
* 提取函数的返回类型(异步函数自动解包 Promise)
|
|
100
|
+
*
|
|
101
|
+
* @template T - 函数类型
|
|
102
|
+
*/
|
|
103
|
+
export type AsyncReturnType<T extends (...args: unknown[]) => unknown> = T extends (...args: unknown[]) => Promise<infer R> ? R : T extends (...args: unknown[]) => infer R ? R : never;
|
|
104
|
+
/**
|
|
105
|
+
* 非空类型
|
|
106
|
+
*
|
|
107
|
+
* 从类型中排除 null 和 undefined
|
|
108
|
+
*
|
|
109
|
+
* @template T - 原始类型
|
|
110
|
+
*/
|
|
111
|
+
export type NonNullable<T> = T extends null | undefined ? never : T;
|
|
112
|
+
/**
|
|
113
|
+
* 可为 null 的类型
|
|
114
|
+
*
|
|
115
|
+
* @template T - 原始类型
|
|
116
|
+
*/
|
|
117
|
+
export type Nullable<T> = T | null;
|
|
118
|
+
/**
|
|
119
|
+
* 可选的类型
|
|
120
|
+
*
|
|
121
|
+
* @template T - 原始类型
|
|
122
|
+
*/
|
|
123
|
+
export type Optional<T> = T | undefined;
|
|
124
|
+
/**
|
|
125
|
+
* 品牌类型
|
|
126
|
+
*
|
|
127
|
+
* 用于创建名义类型系统
|
|
128
|
+
*
|
|
129
|
+
* @template T - 基础类型
|
|
130
|
+
* @template Brand - 品牌标识
|
|
131
|
+
*
|
|
132
|
+
* @example
|
|
133
|
+
* ```typescript
|
|
134
|
+
* type UserId = Brand<string, 'UserId'>
|
|
135
|
+
* type PostId = Brand<string, 'PostId'>
|
|
136
|
+
*
|
|
137
|
+
* // UserId 和 PostId 不能互相赋值,即使底层都是 string
|
|
138
|
+
* ```
|
|
139
|
+
*/
|
|
140
|
+
export type Brand<T, Brand extends string> = T & {
|
|
141
|
+
__brand: Brand;
|
|
142
|
+
};
|
|
143
|
+
/**
|
|
144
|
+
* 严格对象类型
|
|
145
|
+
*
|
|
146
|
+
* 确保对象不包含多余的属性
|
|
147
|
+
*
|
|
148
|
+
* @template T - 期望类型
|
|
149
|
+
* @template Actual - 实际类型
|
|
150
|
+
*/
|
|
151
|
+
export type Exact<T, Actual extends T> = T & {
|
|
152
|
+
[K in keyof Actual]: K extends keyof T ? T[K] : never;
|
|
153
|
+
};
|
|
154
|
+
/**
|
|
155
|
+
* 获取对象的必需键
|
|
156
|
+
*
|
|
157
|
+
* @template T - 对象类型
|
|
158
|
+
*/
|
|
159
|
+
export type RequiredKeys<T> = {
|
|
160
|
+
[K in keyof T]-?: undefined extends T[K] ? never : K;
|
|
161
|
+
}[keyof T];
|
|
162
|
+
/**
|
|
163
|
+
* 获取对象的可选键
|
|
164
|
+
*
|
|
165
|
+
* @template T - 对象类型
|
|
166
|
+
*/
|
|
167
|
+
export type OptionalKeys<T> = {
|
|
168
|
+
[K in keyof T]-?: undefined extends T[K] ? K : never;
|
|
169
|
+
}[keyof T];
|
|
170
|
+
/**
|
|
171
|
+
* 选择对象的部分属性设为必需
|
|
172
|
+
*
|
|
173
|
+
* @template T - 对象类型
|
|
174
|
+
* @template K - 需要设为必需的键
|
|
175
|
+
*/
|
|
176
|
+
export type RequireKeys<T, K extends keyof T> = Omit<T, K> & Required<Pick<T, K>>;
|
|
177
|
+
/**
|
|
178
|
+
* 选择对象的部分属性设为可选
|
|
179
|
+
*
|
|
180
|
+
* @template T - 对象类型
|
|
181
|
+
* @template K - 需要设为可选的键
|
|
182
|
+
*/
|
|
183
|
+
export type PartialKeys<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>>;
|
|
184
|
+
/**
|
|
185
|
+
* 合并两个类型
|
|
186
|
+
*
|
|
187
|
+
* 第二个类型的属性会覆盖第一个类型
|
|
188
|
+
*
|
|
189
|
+
* @template T - 第一个类型
|
|
190
|
+
* @template U - 第二个类型
|
|
191
|
+
*/
|
|
192
|
+
export type Merge<T, U> = Omit<T, keyof U> & U;
|
|
193
|
+
/**
|
|
194
|
+
* 获取两个类型的公共键
|
|
195
|
+
*
|
|
196
|
+
* @template T - 第一个类型
|
|
197
|
+
* @template U - 第二个类型
|
|
198
|
+
*/
|
|
199
|
+
export type CommonKeys<T, U> = keyof T & keyof U;
|
|
200
|
+
/**
|
|
201
|
+
* 函数类型
|
|
202
|
+
*/
|
|
203
|
+
export type AnyFunction = (...args: unknown[]) => unknown;
|
|
204
|
+
/**
|
|
205
|
+
* 异步函数类型
|
|
206
|
+
*/
|
|
207
|
+
export type AsyncFunction = (...args: unknown[]) => Promise<unknown>;
|
|
208
|
+
/**
|
|
209
|
+
* 类类型
|
|
210
|
+
*
|
|
211
|
+
* @template T - 实例类型
|
|
212
|
+
*/
|
|
213
|
+
export type Constructor<T = unknown> = new (...args: unknown[]) => T;
|
|
214
|
+
/**
|
|
215
|
+
* 抽象类类型
|
|
216
|
+
*
|
|
217
|
+
* @template T - 实例类型
|
|
218
|
+
*/
|
|
219
|
+
export type AbstractConstructor<T = unknown> = abstract new (...args: unknown[]) => T;
|
|
220
|
+
/**
|
|
221
|
+
* 确保类型不为 never
|
|
222
|
+
*
|
|
223
|
+
* @template T - 待检查类型
|
|
224
|
+
*/
|
|
225
|
+
export type IsNever<T> = [T] extends [never] ? true : false;
|
|
226
|
+
/**
|
|
227
|
+
* 确保类型是数组
|
|
228
|
+
*
|
|
229
|
+
* @template T - 待检查类型
|
|
230
|
+
*/
|
|
231
|
+
export type IsArray<T> = T extends unknown[] ? true : false;
|
|
232
|
+
/**
|
|
233
|
+
* 确保类型是对象(非数组、非函数)
|
|
234
|
+
*
|
|
235
|
+
* @template T - 待检查类型
|
|
236
|
+
*/
|
|
237
|
+
export type IsPlainObject<T> = T extends object ? T extends Function ? false : T extends unknown[] ? false : true : false;
|
|
238
|
+
/**
|
|
239
|
+
* 值类型为指定类型的键
|
|
240
|
+
*
|
|
241
|
+
* @template T - 对象类型
|
|
242
|
+
* @template V - 值类型
|
|
243
|
+
*
|
|
244
|
+
* @example
|
|
245
|
+
* ```typescript
|
|
246
|
+
* interface User {
|
|
247
|
+
* id: number
|
|
248
|
+
* name: string
|
|
249
|
+
* age: number
|
|
250
|
+
* }
|
|
251
|
+
*
|
|
252
|
+
* type NumberKeys = KeysOfType<User, number> // 'id' | 'age'
|
|
253
|
+
* ```
|
|
254
|
+
*/
|
|
255
|
+
export type KeysOfType<T, V> = {
|
|
256
|
+
[K in keyof T]: T[K] extends V ? K : never;
|
|
257
|
+
}[keyof T];
|
|
258
|
+
/**
|
|
259
|
+
* 排除值类型为指定类型的键
|
|
260
|
+
*
|
|
261
|
+
* @template T - 对象类型
|
|
262
|
+
* @template V - 要排除的值类型
|
|
263
|
+
*/
|
|
264
|
+
export type KeysExcludeType<T, V> = {
|
|
265
|
+
[K in keyof T]: T[K] extends V ? never : K;
|
|
266
|
+
}[keyof T];
|
|
267
|
+
/**
|
|
268
|
+
* 仅保留值类型为指定类型的属性
|
|
269
|
+
*
|
|
270
|
+
* @template T - 对象类型
|
|
271
|
+
* @template V - 值类型
|
|
272
|
+
*/
|
|
273
|
+
export type PickByType<T, V> = Pick<T, KeysOfType<T, V>>;
|
|
274
|
+
/**
|
|
275
|
+
* 排除值类型为指定类型的属性
|
|
276
|
+
*
|
|
277
|
+
* @template T - 对象类型
|
|
278
|
+
* @template V - 要排除的值类型
|
|
279
|
+
*/
|
|
280
|
+
export type OmitByType<T, V> = Pick<T, KeysExcludeType<T, V>>;
|
|
281
|
+
/**
|
|
282
|
+
* 提取 Promise 的值类型
|
|
283
|
+
*
|
|
284
|
+
* @template T - Promise 类型
|
|
285
|
+
*/
|
|
286
|
+
export type Awaited<T> = T extends Promise<infer U> ? Awaited<U> : T;
|
|
287
|
+
/**
|
|
288
|
+
* 联合类型转交叉类型
|
|
289
|
+
*
|
|
290
|
+
* @template U - 联合类型
|
|
291
|
+
*/
|
|
292
|
+
export type UnionToIntersection<U> = (U extends unknown ? (k: U) => void : never) extends (k: infer I) => void ? I : never;
|
|
293
|
+
/**
|
|
294
|
+
* 元组类型转联合类型
|
|
295
|
+
*
|
|
296
|
+
* @template T - 元组类型
|
|
297
|
+
*/
|
|
298
|
+
export type TupleToUnion<T extends unknown[]> = T[number];
|
|
299
|
+
/**
|
|
300
|
+
* 字符串字面量首字母大写
|
|
301
|
+
*
|
|
302
|
+
* @template S - 字符串类型
|
|
303
|
+
*/
|
|
304
|
+
export type Capitalize<S extends string> = S extends `${infer F}${infer R}` ? `${Uppercase<F>}${R}` : S;
|
|
305
|
+
/**
|
|
306
|
+
* 字符串字面量首字母小写
|
|
307
|
+
*
|
|
308
|
+
* @template S - 字符串类型
|
|
309
|
+
*/
|
|
310
|
+
export type Uncapitalize<S extends string> = S extends `${infer F}${infer R}` ? `${Lowercase<F>}${R}` : S;
|
|
311
|
+
export {};
|
|
312
|
+
//# sourceMappingURL=utility.d.ts.map
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 防抖和节流工具函数
|
|
3
|
+
*
|
|
4
|
+
* 提供性能优化的防抖、节流功能
|
|
5
|
+
* @module utils/debounce-throttle
|
|
6
|
+
*/
|
|
7
|
+
/**
|
|
8
|
+
* 防抖函数返回类型
|
|
9
|
+
*/
|
|
10
|
+
export interface DebouncedFunction<T extends (...args: never[]) => void> {
|
|
11
|
+
(...args: Parameters<T>): void;
|
|
12
|
+
/** 取消待执行的调用 */
|
|
13
|
+
cancel: () => void;
|
|
14
|
+
/** 立即执行待处理的调用 */
|
|
15
|
+
flush: () => void;
|
|
16
|
+
/** 是否有待处理的调用 */
|
|
17
|
+
pending: () => boolean;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* 节流函数返回类型
|
|
21
|
+
*/
|
|
22
|
+
export interface ThrottledFunction<T extends (...args: never[]) => void> {
|
|
23
|
+
(...args: Parameters<T>): void;
|
|
24
|
+
/** 取消待执行的调用 */
|
|
25
|
+
cancel: () => void;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* 创建防抖函数
|
|
29
|
+
*
|
|
30
|
+
* 在连续调用停止后,延迟指定时间执行函数
|
|
31
|
+
*
|
|
32
|
+
* @template T - 函数类型
|
|
33
|
+
* @param fn - 需要防抖的函数
|
|
34
|
+
* @param delay - 延迟时间(毫秒)
|
|
35
|
+
* @param immediate - 是否在首次调用时立即执行
|
|
36
|
+
* @returns 防抖后的函数
|
|
37
|
+
*
|
|
38
|
+
* @example
|
|
39
|
+
* ```typescript
|
|
40
|
+
* const debouncedSearch = debounce((query: string) => {
|
|
41
|
+
* fetchResults(query)
|
|
42
|
+
* }, 300)
|
|
43
|
+
*
|
|
44
|
+
* // 连续调用只会在最后一次调用后 300ms 执行
|
|
45
|
+
* debouncedSearch('a')
|
|
46
|
+
* debouncedSearch('ab')
|
|
47
|
+
* debouncedSearch('abc') // 只有这次会触发
|
|
48
|
+
*
|
|
49
|
+
* // 取消待执行的调用
|
|
50
|
+
* debouncedSearch.cancel()
|
|
51
|
+
* ```
|
|
52
|
+
*/
|
|
53
|
+
export declare function debounce<T extends (...args: never[]) => void>(fn: T, delay: number, immediate?: boolean): DebouncedFunction<T>;
|
|
54
|
+
/**
|
|
55
|
+
* 创建节流函数
|
|
56
|
+
*
|
|
57
|
+
* 在指定时间间隔内最多执行一次函数
|
|
58
|
+
*
|
|
59
|
+
* @template T - 函数类型
|
|
60
|
+
* @param fn - 需要节流的函数
|
|
61
|
+
* @param interval - 间隔时间(毫秒)
|
|
62
|
+
* @param options - 节流选项
|
|
63
|
+
* @param options.leading - 是否在时间窗开始时立即执行
|
|
64
|
+
* @param options.trailing - 是否在时间窗结束时补执行最后一次调用
|
|
65
|
+
* @returns 节流后的函数
|
|
66
|
+
*
|
|
67
|
+
* @example
|
|
68
|
+
* ```typescript
|
|
69
|
+
* const throttledUpdate = throttle((x: number, y: number) => {
|
|
70
|
+
* updatePosition(x, y)
|
|
71
|
+
* }, 100)
|
|
72
|
+
*
|
|
73
|
+
* // 在 100ms 内多次调用,只会执行一次
|
|
74
|
+
* window.addEventListener('mousemove', (e) => {
|
|
75
|
+
* throttledUpdate(e.clientX, e.clientY)
|
|
76
|
+
* })
|
|
77
|
+
*
|
|
78
|
+
* // 取消
|
|
79
|
+
* throttledUpdate.cancel()
|
|
80
|
+
* ```
|
|
81
|
+
*/
|
|
82
|
+
export declare function throttle<T extends (...args: never[]) => void>(fn: T, interval: number, options?: {
|
|
83
|
+
leading?: boolean;
|
|
84
|
+
trailing?: boolean;
|
|
85
|
+
}): ThrottledFunction<T>;
|
|
86
|
+
//# sourceMappingURL=debounce-throttle.d.ts.map
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
* ***********************************
|
|
3
|
+
* @composy/store-core v0.0.1 *
|
|
4
|
+
* Built with rollup *
|
|
5
|
+
* Build time: 2024-08-17 11:27:33 *
|
|
6
|
+
* Build mode: production *
|
|
7
|
+
* Minified: No *
|
|
8
|
+
* ***********************************
|
|
9
|
+
*/
|
|
10
|
+
//#region src/utils/debounce-throttle.ts
|
|
11
|
+
function debounce(fn, delay, immediate = false) {
|
|
12
|
+
let timer = null;
|
|
13
|
+
let lastArgs = null;
|
|
14
|
+
const debounced = ((...args) => {
|
|
15
|
+
lastArgs = args;
|
|
16
|
+
if (timer) clearTimeout(timer);
|
|
17
|
+
if (immediate && !timer) {
|
|
18
|
+
fn(...args);
|
|
19
|
+
timer = setTimeout(() => {
|
|
20
|
+
timer = null;
|
|
21
|
+
lastArgs = null;
|
|
22
|
+
}, delay);
|
|
23
|
+
} else timer = setTimeout(() => {
|
|
24
|
+
fn(...args);
|
|
25
|
+
timer = null;
|
|
26
|
+
lastArgs = null;
|
|
27
|
+
}, delay);
|
|
28
|
+
});
|
|
29
|
+
debounced.cancel = () => {
|
|
30
|
+
if (timer) {
|
|
31
|
+
clearTimeout(timer);
|
|
32
|
+
timer = null;
|
|
33
|
+
lastArgs = null;
|
|
34
|
+
}
|
|
35
|
+
};
|
|
36
|
+
debounced.flush = () => {
|
|
37
|
+
if (timer && lastArgs) {
|
|
38
|
+
clearTimeout(timer);
|
|
39
|
+
fn(...lastArgs);
|
|
40
|
+
timer = null;
|
|
41
|
+
lastArgs = null;
|
|
42
|
+
}
|
|
43
|
+
};
|
|
44
|
+
debounced.pending = () => timer !== null;
|
|
45
|
+
return debounced;
|
|
46
|
+
}
|
|
47
|
+
function throttle(fn, interval, options = {}) {
|
|
48
|
+
const { leading = true, trailing = true } = options;
|
|
49
|
+
let timer = null;
|
|
50
|
+
let lastTime = 0;
|
|
51
|
+
let lastArgs = null;
|
|
52
|
+
const throttled = ((...args) => {
|
|
53
|
+
const now = Date.now();
|
|
54
|
+
const remaining = interval - (now - lastTime);
|
|
55
|
+
lastArgs = args;
|
|
56
|
+
if (remaining <= 0 || remaining > interval) {
|
|
57
|
+
if (timer) {
|
|
58
|
+
clearTimeout(timer);
|
|
59
|
+
timer = null;
|
|
60
|
+
}
|
|
61
|
+
lastTime = now;
|
|
62
|
+
if (leading || lastTime !== 0) fn(...args);
|
|
63
|
+
lastArgs = null;
|
|
64
|
+
} else if (!timer && trailing) timer = setTimeout(() => {
|
|
65
|
+
lastTime = trailing ? Date.now() : 0;
|
|
66
|
+
timer = null;
|
|
67
|
+
if (lastArgs) {
|
|
68
|
+
fn(...lastArgs);
|
|
69
|
+
lastArgs = null;
|
|
70
|
+
}
|
|
71
|
+
}, remaining);
|
|
72
|
+
});
|
|
73
|
+
throttled.cancel = () => {
|
|
74
|
+
if (timer) {
|
|
75
|
+
clearTimeout(timer);
|
|
76
|
+
timer = null;
|
|
77
|
+
}
|
|
78
|
+
lastTime = 0;
|
|
79
|
+
lastArgs = null;
|
|
80
|
+
};
|
|
81
|
+
return throttled;
|
|
82
|
+
}
|
|
83
|
+
//#endregion
|
|
84
|
+
export { debounce, throttle };
|
|
85
|
+
/*! End of @composy/store-core | Powered by @composy/builder */
|
|
86
|
+
//# sourceMappingURL=debounce-throttle.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"debounce-throttle.js","names":[],"sources":["../../src/utils/debounce-throttle.ts"],"sourcesContent":["/**\n * 防抖和节流工具函数\n *\n * 提供性能优化的防抖、节流功能\n * @module utils/debounce-throttle\n */\n\n/**\n * 防抖函数返回类型\n */\nexport interface DebouncedFunction<T extends (...args: never[]) => void> {\n (...args: Parameters<T>): void\n /** 取消待执行的调用 */\n cancel: () => void\n /** 立即执行待处理的调用 */\n flush: () => void\n /** 是否有待处理的调用 */\n pending: () => boolean\n}\n\n/**\n * 节流函数返回类型\n */\nexport interface ThrottledFunction<T extends (...args: never[]) => void> {\n (...args: Parameters<T>): void\n /** 取消待执行的调用 */\n cancel: () => void\n}\n\n/**\n * 创建防抖函数\n *\n * 在连续调用停止后,延迟指定时间执行函数\n *\n * @template T - 函数类型\n * @param fn - 需要防抖的函数\n * @param delay - 延迟时间(毫秒)\n * @param immediate - 是否在首次调用时立即执行\n * @returns 防抖后的函数\n *\n * @example\n * ```typescript\n * const debouncedSearch = debounce((query: string) => {\n * fetchResults(query)\n * }, 300)\n *\n * // 连续调用只会在最后一次调用后 300ms 执行\n * debouncedSearch('a')\n * debouncedSearch('ab')\n * debouncedSearch('abc') // 只有这次会触发\n *\n * // 取消待执行的调用\n * debouncedSearch.cancel()\n * ```\n */\nexport function debounce<T extends (...args: never[]) => void>(\n fn: T,\n delay: number,\n immediate = false,\n): DebouncedFunction<T> {\n let timer: ReturnType<typeof setTimeout> | null = null\n let lastArgs: Parameters<T> | null = null\n\n const debounced = ((...args: Parameters<T>) => {\n lastArgs = args\n\n if (timer) {\n clearTimeout(timer)\n }\n\n if (immediate && !timer) {\n fn(...args)\n timer = setTimeout(() => {\n timer = null\n lastArgs = null\n }, delay)\n }\n else {\n timer = setTimeout(() => {\n fn(...args)\n timer = null\n lastArgs = null\n }, delay)\n }\n }) as DebouncedFunction<T>\n\n debounced.cancel = () => {\n if (timer) {\n clearTimeout(timer)\n timer = null\n lastArgs = null\n }\n }\n\n debounced.flush = () => {\n if (timer && lastArgs) {\n clearTimeout(timer)\n fn(...lastArgs)\n timer = null\n lastArgs = null\n }\n }\n\n debounced.pending = () => timer !== null\n\n return debounced\n}\n\n/**\n * 创建节流函数\n *\n * 在指定时间间隔内最多执行一次函数\n *\n * @template T - 函数类型\n * @param fn - 需要节流的函数\n * @param interval - 间隔时间(毫秒)\n * @param options - 节流选项\n * @param options.leading - 是否在时间窗开始时立即执行\n * @param options.trailing - 是否在时间窗结束时补执行最后一次调用\n * @returns 节流后的函数\n *\n * @example\n * ```typescript\n * const throttledUpdate = throttle((x: number, y: number) => {\n * updatePosition(x, y)\n * }, 100)\n *\n * // 在 100ms 内多次调用,只会执行一次\n * window.addEventListener('mousemove', (e) => {\n * throttledUpdate(e.clientX, e.clientY)\n * })\n *\n * // 取消\n * throttledUpdate.cancel()\n * ```\n */\nexport function throttle<T extends (...args: never[]) => void>(\n fn: T,\n interval: number,\n options: { leading?: boolean, trailing?: boolean } = {},\n): ThrottledFunction<T> {\n const { leading = true, trailing = true } = options\n let timer: ReturnType<typeof setTimeout> | null = null\n let lastTime = 0\n let lastArgs: Parameters<T> | null = null\n\n const throttled = ((...args: Parameters<T>) => {\n const now = Date.now()\n const remaining = interval - (now - lastTime)\n\n lastArgs = args\n\n if (remaining <= 0 || remaining > interval) {\n if (timer) {\n clearTimeout(timer)\n timer = null\n }\n lastTime = now\n if (leading || lastTime !== 0) {\n fn(...args)\n }\n lastArgs = null\n }\n else if (!timer && trailing) {\n timer = setTimeout(() => {\n lastTime = trailing ? Date.now() : 0\n timer = null\n if (lastArgs) {\n fn(...lastArgs)\n lastArgs = null\n }\n }, remaining)\n }\n }) as ThrottledFunction<T>\n\n throttled.cancel = () => {\n if (timer) {\n clearTimeout(timer)\n timer = null\n }\n lastTime = 0\n lastArgs = null\n }\n\n return throttled\n}\n"],"mappings":";;;;;;;;;;AAuDO,SAAS,SACd,IACA,OACA,YAAY,OACU;CACtB,IAAI,QAA8C;CAClD,IAAI,WAAiC;CAErC,MAAM,cAAa,GAAI,SAAwB;EAC7C,WAAW;EAEX,IAAI,OACF,aAAa,KAAK;EAGpB,IAAI,aAAa,CAAC,OAAO;GACvB,GAAG,GAAG,IAAI;GACV,QAAQ,iBAAiB;IACvB,QAAQ;IACR,WAAW;GACb,GAAG,KAAK;EACV,OAEE,QAAQ,iBAAiB;GACvB,GAAG,GAAG,IAAI;GACV,QAAQ;GACR,WAAW;EACb,GAAG,KAAK;CAEZ;CAEA,UAAU,eAAe;EACvB,IAAI,OAAO;GACT,aAAa,KAAK;GAClB,QAAQ;GACR,WAAW;EACb;CACF;CAEA,UAAU,cAAc;EACtB,IAAI,SAAS,UAAU;GACrB,aAAa,KAAK;GAClB,GAAG,GAAG,QAAQ;GACd,QAAQ;GACR,WAAW;EACb;CACF;CAEA,UAAU,gBAAgB,UAAU;CAEpC,OAAO;AACT;AA8BO,SAAS,SACd,IACA,UACA,UAAqD,CAAC,GAChC;CACtB,MAAM,EAAE,UAAU,MAAM,WAAW,SAAS;CAC5C,IAAI,QAA8C;CAClD,IAAI,WAAW;CACf,IAAI,WAAiC;CAErC,MAAM,cAAa,GAAI,SAAwB;EAC7C,MAAM,MAAM,KAAK,IAAI;EACrB,MAAM,YAAY,YAAY,MAAM;EAEpC,WAAW;EAEX,IAAI,aAAa,KAAK,YAAY,UAAU;GAC1C,IAAI,OAAO;IACT,aAAa,KAAK;IAClB,QAAQ;GACV;GACA,WAAW;GACX,IAAI,WAAW,aAAa,GAC1B,GAAG,GAAG,IAAI;GAEZ,WAAW;EACb,OAAA,IACS,CAAC,SAAS,UACjB,QAAQ,iBAAiB;GACvB,WAAW,WAAW,KAAK,IAAI,IAAI;GACnC,QAAQ;GACR,IAAI,UAAU;IACZ,GAAG,GAAG,QAAQ;IACd,WAAW;GACb;EACF,GAAG,SAAS;CAEhB;CAEA,UAAU,eAAe;EACvB,IAAI,OAAO;GACT,aAAa,KAAK;GAClB,QAAQ;EACV;EACA,WAAW;EACX,WAAW;CACb;CAEA,OAAO;AACT"}
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 深拷贝工具
|
|
3
|
+
*
|
|
4
|
+
* 提供高性能的深拷贝、浅拷贝和深度合并功能
|
|
5
|
+
* @module utils/deep-clone
|
|
6
|
+
*/
|
|
7
|
+
/**
|
|
8
|
+
* 判断是否为对象
|
|
9
|
+
*
|
|
10
|
+
* @param value - 待检查的值
|
|
11
|
+
* @returns 是否为对象
|
|
12
|
+
*/
|
|
13
|
+
export declare function isObject(value: unknown): value is Record<string, unknown>;
|
|
14
|
+
/**
|
|
15
|
+
* 判断是否为普通对象(非数组、非内置对象)
|
|
16
|
+
*
|
|
17
|
+
* @param value - 待检查的值
|
|
18
|
+
* @returns 是否为普通对象
|
|
19
|
+
*/
|
|
20
|
+
export declare function isPlainObject(value: unknown): value is Record<string, unknown>;
|
|
21
|
+
/**
|
|
22
|
+
* 深拷贝配置选项
|
|
23
|
+
*/
|
|
24
|
+
export interface DeepCloneOptions {
|
|
25
|
+
/** 是否优先使用 structuredClone(默认 true) */
|
|
26
|
+
useStructuredClone?: boolean;
|
|
27
|
+
/** 是否克隆函数(默认 false,函数保持引用) */
|
|
28
|
+
cloneFunctions?: boolean;
|
|
29
|
+
/** 是否克隆 Symbol 属性(默认 false) */
|
|
30
|
+
cloneSymbols?: boolean;
|
|
31
|
+
/** 最大深度(默认无限制) */
|
|
32
|
+
maxDepth?: number;
|
|
33
|
+
/** 自定义克隆器 */
|
|
34
|
+
customCloner?: <T>(value: T, clone: <U>(v: U) => U) => T | undefined;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* 深拷贝
|
|
38
|
+
*
|
|
39
|
+
* 高性能深拷贝实现,优先使用原生 structuredClone(如果可用)
|
|
40
|
+
*
|
|
41
|
+
* @template T - 值类型
|
|
42
|
+
* @param value - 要拷贝的值
|
|
43
|
+
* @param options - 配置选项
|
|
44
|
+
* @returns 拷贝后的值
|
|
45
|
+
*
|
|
46
|
+
* @example
|
|
47
|
+
* ```typescript
|
|
48
|
+
* const obj = { a: 1, b: { c: 2 } }
|
|
49
|
+
* const cloned = deepClone(obj)
|
|
50
|
+
* cloned.b.c = 3
|
|
51
|
+
* console.log(obj.b.c) // 2
|
|
52
|
+
* ```
|
|
53
|
+
*
|
|
54
|
+
* @example
|
|
55
|
+
* ```typescript
|
|
56
|
+
* // 带配置的深拷贝
|
|
57
|
+
* const cloned = deepClone(obj, {
|
|
58
|
+
* maxDepth: 5,
|
|
59
|
+
* cloneSymbols: true
|
|
60
|
+
* })
|
|
61
|
+
* ```
|
|
62
|
+
*/
|
|
63
|
+
export declare function deepClone<T>(value: T, options?: DeepCloneOptions): T;
|
|
64
|
+
/**
|
|
65
|
+
* 浅拷贝
|
|
66
|
+
*
|
|
67
|
+
* @template T - 值类型
|
|
68
|
+
* @param value - 要拷贝的值
|
|
69
|
+
* @returns 拷贝后的值
|
|
70
|
+
*
|
|
71
|
+
* @example
|
|
72
|
+
* ```typescript
|
|
73
|
+
* const arr = [1, 2, { a: 3 }]
|
|
74
|
+
* const cloned = shallowClone(arr)
|
|
75
|
+
* cloned[2].a = 4
|
|
76
|
+
* console.log(arr[2].a) // 4 (浅拷贝,嵌套对象共享引用)
|
|
77
|
+
* ```
|
|
78
|
+
*/
|
|
79
|
+
export declare function shallowClone<T>(value: T): T;
|
|
80
|
+
/**
|
|
81
|
+
* 深度合并配置选项
|
|
82
|
+
*/
|
|
83
|
+
export interface DeepMergeOptions {
|
|
84
|
+
/** 数组合并策略:'replace' 替换 | 'concat' 连接 | 'merge' 按索引合并 */
|
|
85
|
+
arrayStrategy?: 'replace' | 'concat' | 'merge';
|
|
86
|
+
/** 是否克隆源对象(默认 true) */
|
|
87
|
+
clone?: boolean;
|
|
88
|
+
/** 最大深度 */
|
|
89
|
+
maxDepth?: number;
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* 深度合并对象
|
|
93
|
+
*
|
|
94
|
+
* 将多个源对象深度合并到目标对象
|
|
95
|
+
*
|
|
96
|
+
* @template T - 目标对象类型
|
|
97
|
+
* @param target - 目标对象
|
|
98
|
+
* @param sources - 源对象列表
|
|
99
|
+
* @returns 合并后的对象
|
|
100
|
+
*
|
|
101
|
+
* @example
|
|
102
|
+
* ```typescript
|
|
103
|
+
* const target = { a: 1, b: { c: 2 } }
|
|
104
|
+
* const source = { b: { d: 3 }, e: 4 }
|
|
105
|
+
* const result = deepMerge(target, source)
|
|
106
|
+
* // { a: 1, b: { c: 2, d: 3 }, e: 4 }
|
|
107
|
+
* ```
|
|
108
|
+
*/
|
|
109
|
+
export declare function deepMerge<T extends Record<string, unknown>>(target: T, ...sources: Array<Partial<T>>): T;
|
|
110
|
+
/**
|
|
111
|
+
* 带配置的深度合并
|
|
112
|
+
*
|
|
113
|
+
* @template T - 目标对象类型
|
|
114
|
+
* @param target - 目标对象
|
|
115
|
+
* @param options - 合并配置
|
|
116
|
+
* @param sources - 源对象列表
|
|
117
|
+
* @returns 合并后的对象
|
|
118
|
+
*/
|
|
119
|
+
export declare function deepMergeWithOptions<T extends Record<string, unknown>>(target: T, options: DeepMergeOptions, ...sources: Array<Partial<T>>): T;
|
|
120
|
+
/**
|
|
121
|
+
* 比较两个值是否深度相等
|
|
122
|
+
*
|
|
123
|
+
* @param a - 第一个值
|
|
124
|
+
* @param b - 第二个值
|
|
125
|
+
* @returns 是否相等
|
|
126
|
+
*
|
|
127
|
+
* @example
|
|
128
|
+
* ```typescript
|
|
129
|
+
* deepEqual({ a: 1 }, { a: 1 }) // true
|
|
130
|
+
* deepEqual([1, 2], [1, 2]) // true
|
|
131
|
+
* deepEqual({ a: { b: 1 } }, { a: { b: 2 } }) // false
|
|
132
|
+
* ```
|
|
133
|
+
*/
|
|
134
|
+
export declare function deepEqual(a: unknown, b: unknown): boolean;
|
|
135
|
+
/**
|
|
136
|
+
* 冻结对象(深度)
|
|
137
|
+
*
|
|
138
|
+
* @template T - 对象类型
|
|
139
|
+
* @param obj - 要冻结的对象
|
|
140
|
+
* @returns 冻结后的对象
|
|
141
|
+
*/
|
|
142
|
+
export declare function deepFreeze<T extends object>(obj: T): Readonly<T>;
|
|
143
|
+
//# sourceMappingURL=deep-clone.d.ts.map
|