twcpt 0.0.2
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/LICENSE +22 -0
- package/README.md +189 -0
- package/dist/charts-CKv8I1ye.js +879 -0
- package/dist/charts-DIdINgA4.cjs +1 -0
- package/dist/charts.cjs.js +1 -0
- package/dist/charts.css +1 -0
- package/dist/charts.d.ts +7 -0
- package/dist/charts.es.js +9 -0
- package/dist/components/j-ch-bar/index.d.ts +50 -0
- package/dist/components/j-ch-bar/types.d.ts +42 -0
- package/dist/components/j-ch-bar-line/index.d.ts +49 -0
- package/dist/components/j-ch-bar-line/types.d.ts +42 -0
- package/dist/components/j-ch-bubble/index.d.ts +54 -0
- package/dist/components/j-ch-bubble/types.d.ts +51 -0
- package/dist/components/j-ch-line/index.d.ts +57 -0
- package/dist/components/j-ch-line/types.d.ts +42 -0
- package/dist/components/j-ch-pie-doughnut/index.d.ts +60 -0
- package/dist/components/j-ch-pie-doughnut/types.d.ts +55 -0
- package/dist/components/j-ch-radar/index.d.ts +49 -0
- package/dist/components/j-ch-radar/types.d.ts +42 -0
- package/dist/components/j-tw-btn/index.d.ts +606 -0
- package/dist/components/j-tw-btn/types.d.ts +104 -0
- package/dist/composables/useI18n.d.ts +12 -0
- package/dist/global.d.ts +75 -0
- package/dist/i18n/en-US/index.d.ts +90 -0
- package/dist/i18n/index.d.ts +10 -0
- package/dist/i18n/zh-CN/index.d.ts +90 -0
- package/dist/index-BrYPwI7i.cjs +1 -0
- package/dist/index-C5mRo5Yi.cjs +1 -0
- package/dist/index-DsQ7r9Ci.js +76 -0
- package/dist/index-s5XCGtM9.js +76 -0
- package/dist/index.css +1 -0
- package/dist/index.d.ts +68 -0
- package/dist/resolver.cjs +1 -0
- package/dist/resolver.d.ts +2 -0
- package/dist/resolver.mjs +36 -0
- package/dist/twcpt-styles.css +1 -0
- package/dist/twcpt-styles.d.ts +1 -0
- package/dist/twcpt-styles.js +1 -0
- package/dist/twcpt.cjs.js +1 -0
- package/dist/twcpt.es.js +2003 -0
- package/dist/types.d.ts +27 -0
- package/dist/utils/chart.d.ts +28 -0
- package/dist/utils/custom-svg.d.ts +3 -0
- package/dist/utils/helper/array.d.ts +289 -0
- package/dist/utils/helper/dom.d.ts +281 -0
- package/dist/utils/helper/index.d.ts +11 -0
- package/dist/utils/helper/number.d.ts +250 -0
- package/dist/utils/helper/object.d.ts +240 -0
- package/dist/utils/helper/perf.d.ts +231 -0
- package/dist/utils/helper/regex.d.ts +265 -0
- package/dist/utils/helper/string.d.ts +248 -0
- package/dist/utils/helper/url.d.ts +259 -0
- package/dist/utils/icon-map.d.ts +19 -0
- package/dist/utils/init.d.ts +35 -0
- package/dist/utils/resolver.d.ts +34 -0
- package/dist/utils/storage.d.ts +3 -0
- package/dist/utils/storageSync.d.ts +20 -0
- package/dist/utils/tool.d.ts +103 -0
- package/dist/utils/website-config.d.ts +15 -0
- package/package.json +90 -0
|
@@ -0,0 +1,250 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 数字处理工具函数
|
|
3
|
+
*/
|
|
4
|
+
/**
|
|
5
|
+
* 生成指定范围的随机整数
|
|
6
|
+
* @param min 最小值(包含)
|
|
7
|
+
* @param max 最大值(包含)
|
|
8
|
+
* @returns 随机整数
|
|
9
|
+
* @example
|
|
10
|
+
* random(1, 10) // 1-10之间的随机整数
|
|
11
|
+
*/
|
|
12
|
+
export declare function random(min: number, max: number): number;
|
|
13
|
+
/**
|
|
14
|
+
* 生成指定范围的随机浮点数
|
|
15
|
+
* @param min 最小值(包含)
|
|
16
|
+
* @param max 最大值(不包含)
|
|
17
|
+
* @param decimals 小数位数,默认 2
|
|
18
|
+
* @returns 随机浮点数
|
|
19
|
+
* @example
|
|
20
|
+
* randomFloat(1, 10, 2) // 1.00-10.00之间的随机数
|
|
21
|
+
*/
|
|
22
|
+
export declare function randomFloat(min: number, max: number, decimals?: number): number;
|
|
23
|
+
/**
|
|
24
|
+
* 生成数字序列
|
|
25
|
+
* @param start 起始值
|
|
26
|
+
* @param end 结束值(包含)
|
|
27
|
+
* @param step 步长,默认 1
|
|
28
|
+
* @returns 数字数组
|
|
29
|
+
* @example
|
|
30
|
+
* range(1, 5) // [1, 2, 3, 4, 5]
|
|
31
|
+
* range(0, 10, 2) // [0, 2, 4, 6, 8, 10]
|
|
32
|
+
*/
|
|
33
|
+
export declare function range(start: number, end: number, step?: number): number[];
|
|
34
|
+
/**
|
|
35
|
+
* 限制数字在指定范围内
|
|
36
|
+
* @param value 数值
|
|
37
|
+
* @param min 最小值
|
|
38
|
+
* @param max 最大值
|
|
39
|
+
* @returns 限制后的数值
|
|
40
|
+
* @example
|
|
41
|
+
* clamp(15, 0, 10) // 10
|
|
42
|
+
* clamp(-5, 0, 10) // 0
|
|
43
|
+
* clamp(5, 0, 10) // 5
|
|
44
|
+
*/
|
|
45
|
+
export declare function clamp(value: number, min: number, max: number): number;
|
|
46
|
+
/**
|
|
47
|
+
* 精确加法(解决 JavaScript 精度问题)
|
|
48
|
+
* @param num1 加数1
|
|
49
|
+
* @param num2 加数2
|
|
50
|
+
* @returns 和
|
|
51
|
+
* @example
|
|
52
|
+
* add(0.1, 0.2) // 0.3 (而不是 0.30000000000000004)
|
|
53
|
+
*/
|
|
54
|
+
export declare function add(num1: number, num2: number): number;
|
|
55
|
+
/**
|
|
56
|
+
* 精确减法
|
|
57
|
+
* @param num1 被减数
|
|
58
|
+
* @param num2 减数
|
|
59
|
+
* @returns 差
|
|
60
|
+
* @example
|
|
61
|
+
* subtract(0.3, 0.1) // 0.2
|
|
62
|
+
*/
|
|
63
|
+
export declare function subtract(num1: number, num2: number): number;
|
|
64
|
+
/**
|
|
65
|
+
* 精确乘法
|
|
66
|
+
* @param num1 乘数1
|
|
67
|
+
* @param num2 乘数2
|
|
68
|
+
* @returns 积
|
|
69
|
+
* @example
|
|
70
|
+
* multiply(0.1, 0.2) // 0.02
|
|
71
|
+
*/
|
|
72
|
+
export declare function multiply(num1: number, num2: number): number;
|
|
73
|
+
/**
|
|
74
|
+
* 精确除法
|
|
75
|
+
* @param num1 被除数
|
|
76
|
+
* @param num2 除数
|
|
77
|
+
* @returns 商
|
|
78
|
+
* @example
|
|
79
|
+
* divide(0.3, 0.1) // 3
|
|
80
|
+
*/
|
|
81
|
+
export declare function divide(num1: number, num2: number): number;
|
|
82
|
+
/**
|
|
83
|
+
* 精确舍入
|
|
84
|
+
* @param value 数值
|
|
85
|
+
* @param decimals 保留小数位数
|
|
86
|
+
* @returns 舍入后的数值
|
|
87
|
+
* @example
|
|
88
|
+
* round(1.2345, 2) // 1.23
|
|
89
|
+
*/
|
|
90
|
+
export declare function round(value: number, decimals?: number): number;
|
|
91
|
+
/**
|
|
92
|
+
* 向上舍入
|
|
93
|
+
* @param value 数值
|
|
94
|
+
* @param decimals 保留小数位数
|
|
95
|
+
* @returns 舍入后的数值
|
|
96
|
+
* @example
|
|
97
|
+
* ceil(1.2345, 2) // 1.24
|
|
98
|
+
*/
|
|
99
|
+
export declare function ceil(value: number, decimals?: number): number;
|
|
100
|
+
/**
|
|
101
|
+
* 向下舍入
|
|
102
|
+
* @param value 数值
|
|
103
|
+
* @param decimals 保留小数位数
|
|
104
|
+
* @returns 舍入后的数值
|
|
105
|
+
* @example
|
|
106
|
+
* floor(1.2345, 2) // 1.23
|
|
107
|
+
*/
|
|
108
|
+
export declare function floor(value: number, decimals?: number): number;
|
|
109
|
+
/**
|
|
110
|
+
* 修复 toFixed 的问题(银行家舍入法)
|
|
111
|
+
* @param value 数值
|
|
112
|
+
* @param decimals 保留小数位数
|
|
113
|
+
* @returns 格式化后的字符串
|
|
114
|
+
* @example
|
|
115
|
+
* toFixed(1.2345, 2) // '1.23'
|
|
116
|
+
*/
|
|
117
|
+
export declare function toFixed(value: number, decimals?: number): string;
|
|
118
|
+
/**
|
|
119
|
+
* 计算百分比
|
|
120
|
+
* @param value 当前值
|
|
121
|
+
* @param total 总值
|
|
122
|
+
* @param decimals 小数位数,默认 2
|
|
123
|
+
* @returns 百分比数值(0-100)
|
|
124
|
+
* @example
|
|
125
|
+
* percentage(25, 100) // 25
|
|
126
|
+
* percentage(1, 3, 2) // 33.33
|
|
127
|
+
*/
|
|
128
|
+
export declare function percentage(value: number, total: number, decimals?: number): number;
|
|
129
|
+
/**
|
|
130
|
+
* 百分比转小数
|
|
131
|
+
* @param percent 百分比(0-100)
|
|
132
|
+
* @returns 小数(0-1)
|
|
133
|
+
* @example
|
|
134
|
+
* percentToDecimal(25) // 0.25
|
|
135
|
+
*/
|
|
136
|
+
export declare function percentToDecimal(percent: number): number;
|
|
137
|
+
/**
|
|
138
|
+
* 小数转百分比
|
|
139
|
+
* @param decimal 小数(0-1)
|
|
140
|
+
* @returns 百分比(0-100)
|
|
141
|
+
* @example
|
|
142
|
+
* decimalToPercent(0.25) // 25
|
|
143
|
+
*/
|
|
144
|
+
export declare function decimalToPercent(decimal: number): number;
|
|
145
|
+
/**
|
|
146
|
+
* 数字补零
|
|
147
|
+
* @param num 数字
|
|
148
|
+
* @param length 总长度
|
|
149
|
+
* @returns 补零后的字符串
|
|
150
|
+
* @example
|
|
151
|
+
* padZero(5, 3) // '005'
|
|
152
|
+
*/
|
|
153
|
+
export declare function padZero(num: number, length?: number): string;
|
|
154
|
+
/**
|
|
155
|
+
* 数字转中文
|
|
156
|
+
* @param num 数字(0-99999)
|
|
157
|
+
* @returns 中文数字
|
|
158
|
+
* @example
|
|
159
|
+
* toChineseNumber(12345) // '一万二千三百四十五'
|
|
160
|
+
*/
|
|
161
|
+
export declare function toChineseNumber(num: number): string;
|
|
162
|
+
/**
|
|
163
|
+
* 数字转大写金额
|
|
164
|
+
* @param num 金额
|
|
165
|
+
* @returns 大写金额
|
|
166
|
+
* @example
|
|
167
|
+
* toChineseMoney(123.45) // '壹佰贰拾叁元肆角伍分'
|
|
168
|
+
*/
|
|
169
|
+
export declare function toChineseMoney(num: number): string;
|
|
170
|
+
/**
|
|
171
|
+
* 判断是否为偶数
|
|
172
|
+
* @param num 数字
|
|
173
|
+
* @returns 是否为偶数
|
|
174
|
+
* @example
|
|
175
|
+
* isEven(4) // true
|
|
176
|
+
*/
|
|
177
|
+
export declare function isEven(num: number): boolean;
|
|
178
|
+
/**
|
|
179
|
+
* 判断是否为奇数
|
|
180
|
+
* @param num 数字
|
|
181
|
+
* @returns 是否为奇数
|
|
182
|
+
* @example
|
|
183
|
+
* isOdd(3) // true
|
|
184
|
+
*/
|
|
185
|
+
export declare function isOdd(num: number): boolean;
|
|
186
|
+
/**
|
|
187
|
+
* 判断是否为质数
|
|
188
|
+
* @param num 数字
|
|
189
|
+
* @returns 是否为质数
|
|
190
|
+
* @example
|
|
191
|
+
* isPrime(7) // true
|
|
192
|
+
*/
|
|
193
|
+
export declare function isPrime(num: number): boolean;
|
|
194
|
+
/**
|
|
195
|
+
* 判断是否在范围内
|
|
196
|
+
* @param value 数值
|
|
197
|
+
* @param min 最小值
|
|
198
|
+
* @param max 最大值
|
|
199
|
+
* @returns 是否在范围内
|
|
200
|
+
* @example
|
|
201
|
+
* inRange(5, 0, 10) // true
|
|
202
|
+
*/
|
|
203
|
+
export declare function inRange(value: number, min: number, max: number): boolean;
|
|
204
|
+
/**
|
|
205
|
+
* 求绝对值
|
|
206
|
+
* @param num 数字
|
|
207
|
+
* @returns 绝对值
|
|
208
|
+
*/
|
|
209
|
+
export declare function abs(num: number): number;
|
|
210
|
+
/**
|
|
211
|
+
* 求平方根
|
|
212
|
+
* @param num 数字
|
|
213
|
+
* @returns 平方根
|
|
214
|
+
*/
|
|
215
|
+
export declare function sqrt(num: number): number;
|
|
216
|
+
/**
|
|
217
|
+
* 求幂
|
|
218
|
+
* @param base 底数
|
|
219
|
+
* @param exponent 指数
|
|
220
|
+
* @returns 幂
|
|
221
|
+
* @example
|
|
222
|
+
* pow(2, 3) // 8
|
|
223
|
+
*/
|
|
224
|
+
export declare function pow(base: number, exponent: number): number;
|
|
225
|
+
/**
|
|
226
|
+
* 求阶乘
|
|
227
|
+
* @param num 数字
|
|
228
|
+
* @returns 阶乘
|
|
229
|
+
* @example
|
|
230
|
+
* factorial(5) // 120
|
|
231
|
+
*/
|
|
232
|
+
export declare function factorial(num: number): number;
|
|
233
|
+
/**
|
|
234
|
+
* 求最大公约数
|
|
235
|
+
* @param a 数字a
|
|
236
|
+
* @param b 数字b
|
|
237
|
+
* @returns 最大公约数
|
|
238
|
+
* @example
|
|
239
|
+
* gcd(12, 8) // 4
|
|
240
|
+
*/
|
|
241
|
+
export declare function gcd(a: number, b: number): number;
|
|
242
|
+
/**
|
|
243
|
+
* 求最小公倍数
|
|
244
|
+
* @param a 数字a
|
|
245
|
+
* @param b 数字b
|
|
246
|
+
* @returns 最小公倍数
|
|
247
|
+
* @example
|
|
248
|
+
* lcm(12, 8) // 24
|
|
249
|
+
*/
|
|
250
|
+
export declare function lcm(a: number, b: number): number;
|
|
@@ -0,0 +1,240 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 对象处理工具函数
|
|
3
|
+
*/
|
|
4
|
+
/**
|
|
5
|
+
* 深拷贝对象
|
|
6
|
+
* @param obj 要拷贝的对象
|
|
7
|
+
* @returns 深拷贝后的对象
|
|
8
|
+
* @example
|
|
9
|
+
* const obj = { a: 1, b: { c: 2 } };
|
|
10
|
+
* const copied = deepClone(obj);
|
|
11
|
+
* copied.b.c = 3; // 不影响原对象
|
|
12
|
+
*/
|
|
13
|
+
export declare function deepClone<T>(obj: T): T;
|
|
14
|
+
/**
|
|
15
|
+
* 浅拷贝对象
|
|
16
|
+
* @param obj 要拷贝的对象
|
|
17
|
+
* @returns 浅拷贝后的对象
|
|
18
|
+
* @example
|
|
19
|
+
* const obj = { a: 1, b: { c: 2 } };
|
|
20
|
+
* const copied = shallowClone(obj);
|
|
21
|
+
*/
|
|
22
|
+
export declare function shallowClone<T>(obj: T): T;
|
|
23
|
+
/**
|
|
24
|
+
* 深度合并对象
|
|
25
|
+
* @param target 目标对象
|
|
26
|
+
* @param sources 源对象数组
|
|
27
|
+
* @returns 合并后的对象
|
|
28
|
+
* @example
|
|
29
|
+
* deepMerge({ a: 1, b: { c: 2 } }, { b: { d: 3 }, e: 4 })
|
|
30
|
+
* // { a: 1, b: { c: 2, d: 3 }, e: 4 }
|
|
31
|
+
*/
|
|
32
|
+
export declare function deepMerge<T extends Record<string, any>>(...sources: Partial<T>[]): T;
|
|
33
|
+
/**
|
|
34
|
+
* 浅合并对象
|
|
35
|
+
* @param target 目标对象
|
|
36
|
+
* @param sources 源对象数组
|
|
37
|
+
* @returns 合并后的对象
|
|
38
|
+
* @example
|
|
39
|
+
* shallowMerge({ a: 1 }, { b: 2 }, { c: 3 })
|
|
40
|
+
* // { a: 1, b: 2, c: 3 }
|
|
41
|
+
*/
|
|
42
|
+
export declare function shallowMerge<T extends Record<string, any>>(...sources: Partial<T>[]): T;
|
|
43
|
+
/**
|
|
44
|
+
* 选取对象的指定属性
|
|
45
|
+
* @param obj 源对象
|
|
46
|
+
* @param keys 要选取的属性名数组
|
|
47
|
+
* @returns 只包含指定属性的新对象
|
|
48
|
+
* @example
|
|
49
|
+
* pick({ a: 1, b: 2, c: 3 }, ['a', 'c'])
|
|
50
|
+
* // { a: 1, c: 3 }
|
|
51
|
+
*/
|
|
52
|
+
export declare function pick<T extends Record<string, any>, K extends keyof T>(obj: T, keys: K[]): Pick<T, K>;
|
|
53
|
+
/**
|
|
54
|
+
* 排除对象的指定属性
|
|
55
|
+
* @param obj 源对象
|
|
56
|
+
* @param keys 要排除的属性名数组
|
|
57
|
+
* @returns 排除指定属性后的新对象
|
|
58
|
+
* @example
|
|
59
|
+
* omit({ a: 1, b: 2, c: 3 }, ['b'])
|
|
60
|
+
* // { a: 1, c: 3 }
|
|
61
|
+
*/
|
|
62
|
+
export declare function omit<T extends Record<string, any>, K extends keyof T>(obj: T, keys: K[]): Omit<T, K>;
|
|
63
|
+
/**
|
|
64
|
+
* 获取对象深层属性
|
|
65
|
+
* @param obj 对象
|
|
66
|
+
* @param path 属性路径,如 'user.info.name'
|
|
67
|
+
* @param defaultValue 默认值
|
|
68
|
+
* @returns 属性值或默认值
|
|
69
|
+
* @example
|
|
70
|
+
* get({ user: { info: { name: 'Tom' } } }, 'user.info.name')
|
|
71
|
+
* // 'Tom'
|
|
72
|
+
* get({ user: {} }, 'user.info.name', 'Unknown')
|
|
73
|
+
* // 'Unknown'
|
|
74
|
+
*/
|
|
75
|
+
export declare function get<T = any>(obj: any, path: string, defaultValue?: T): T;
|
|
76
|
+
/**
|
|
77
|
+
* 设置对象深层属性
|
|
78
|
+
* @param obj 对象
|
|
79
|
+
* @param path 属性路径,如 'user.info.name'
|
|
80
|
+
* @param value 要设置的值
|
|
81
|
+
* @example
|
|
82
|
+
* const obj = {};
|
|
83
|
+
* set(obj, 'user.info.name', 'Tom');
|
|
84
|
+
* // obj 变为 { user: { info: { name: 'Tom' } } }
|
|
85
|
+
*/
|
|
86
|
+
export declare function set(obj: any, path: string, value: any): void;
|
|
87
|
+
/**
|
|
88
|
+
* 判断对象是否有指定属性(支持深层路径)
|
|
89
|
+
* @param obj 对象
|
|
90
|
+
* @param path 属性路径,如 'user.info.name'
|
|
91
|
+
* @returns 是否存在该属性
|
|
92
|
+
* @example
|
|
93
|
+
* has({ user: { info: { name: 'Tom' } } }, 'user.info.name')
|
|
94
|
+
* // true
|
|
95
|
+
* has({ user: {} }, 'user.info.name')
|
|
96
|
+
* // false
|
|
97
|
+
*/
|
|
98
|
+
export declare function has(obj: any, path: string): boolean;
|
|
99
|
+
/**
|
|
100
|
+
* 判断对象是否为空
|
|
101
|
+
* @param obj 对象
|
|
102
|
+
* @returns 是否为空
|
|
103
|
+
* @example
|
|
104
|
+
* isEmpty({}) // true
|
|
105
|
+
* isEmpty({ a: 1 }) // false
|
|
106
|
+
* isEmpty(null) // true
|
|
107
|
+
* isEmpty([]) // true
|
|
108
|
+
*/
|
|
109
|
+
export declare function isEmpty(obj: any): boolean;
|
|
110
|
+
/**
|
|
111
|
+
* 判断是否为普通对象
|
|
112
|
+
* @param obj 任意值
|
|
113
|
+
* @returns 是否为普通对象
|
|
114
|
+
* @example
|
|
115
|
+
* isPlainObject({}) // true
|
|
116
|
+
* isPlainObject(new Date()) // false
|
|
117
|
+
* isPlainObject([]) // false
|
|
118
|
+
*/
|
|
119
|
+
export declare function isPlainObject(obj: any): obj is Record<string, any>;
|
|
120
|
+
/**
|
|
121
|
+
* 判断两个对象是否相等(深度比较)
|
|
122
|
+
* @param obj1 对象1
|
|
123
|
+
* @param obj2 对象2
|
|
124
|
+
* @returns 是否相等
|
|
125
|
+
* @example
|
|
126
|
+
* isEqual({ a: 1, b: { c: 2 } }, { a: 1, b: { c: 2 } })
|
|
127
|
+
* // true
|
|
128
|
+
*/
|
|
129
|
+
export declare function isEqual(obj1: any, obj2: any): boolean;
|
|
130
|
+
/**
|
|
131
|
+
* 对象键名转驼峰
|
|
132
|
+
* @param obj 对象
|
|
133
|
+
* @returns 键名转为驼峰的新对象
|
|
134
|
+
* @example
|
|
135
|
+
* keysToCamelCase({ user_name: 'Tom', user_age: 20 })
|
|
136
|
+
* // { userName: 'Tom', userAge: 20 }
|
|
137
|
+
*/
|
|
138
|
+
export declare function keysToCamelCase<T extends Record<string, any>>(obj: T): any;
|
|
139
|
+
/**
|
|
140
|
+
* 对象键名转下划线
|
|
141
|
+
* @param obj 对象
|
|
142
|
+
* @returns 键名转为下划线的新对象
|
|
143
|
+
* @example
|
|
144
|
+
* keysToUnderlineCase({ userName: 'Tom', userAge: 20 })
|
|
145
|
+
* // { user_name: 'Tom', user_age: 20 }
|
|
146
|
+
*/
|
|
147
|
+
export declare function keysToUnderlineCase<T extends Record<string, any>>(obj: T): any;
|
|
148
|
+
/**
|
|
149
|
+
* 对象转 URL 查询字符串
|
|
150
|
+
* @param obj 对象
|
|
151
|
+
* @returns URL 查询字符串
|
|
152
|
+
* @example
|
|
153
|
+
* toQueryString({ name: 'Tom', age: 20 })
|
|
154
|
+
* // 'name=Tom&age=20'
|
|
155
|
+
*/
|
|
156
|
+
export declare function toQueryString(obj: Record<string, any>): string;
|
|
157
|
+
/**
|
|
158
|
+
* URL 查询字符串转对象
|
|
159
|
+
* @param queryString URL 查询字符串
|
|
160
|
+
* @returns 对象
|
|
161
|
+
* @example
|
|
162
|
+
* fromQueryString('name=Tom&age=20')
|
|
163
|
+
* // { name: 'Tom', age: '20' }
|
|
164
|
+
*/
|
|
165
|
+
export declare function fromQueryString(queryString: string): Record<string, any>;
|
|
166
|
+
/**
|
|
167
|
+
* 过滤对象的属性(根据值)
|
|
168
|
+
* @param obj 对象
|
|
169
|
+
* @param predicate 判断函数
|
|
170
|
+
* @returns 过滤后的新对象
|
|
171
|
+
* @example
|
|
172
|
+
* filterValues({ a: 1, b: 2, c: 3 }, value => value > 1)
|
|
173
|
+
* // { b: 2, c: 3 }
|
|
174
|
+
*/
|
|
175
|
+
export declare function filterValues<T extends Record<string, any>>(obj: T, predicate: (value: any, key: string) => boolean): Partial<T>;
|
|
176
|
+
/**
|
|
177
|
+
* 移除对象中的空值(null、undefined、空字符串)
|
|
178
|
+
* @param obj 对象
|
|
179
|
+
* @param removeEmptyString 是否移除空字符串,默认 true
|
|
180
|
+
* @returns 移除空值后的新对象
|
|
181
|
+
* @example
|
|
182
|
+
* removeEmpty({ a: 1, b: null, c: undefined, d: '', e: 0 })
|
|
183
|
+
* // { a: 1, e: 0 }
|
|
184
|
+
*/
|
|
185
|
+
export declare function removeEmpty<T extends Record<string, any>>(obj: T, removeEmptyString?: boolean): Partial<T>;
|
|
186
|
+
/**
|
|
187
|
+
* 扁平化嵌套对象
|
|
188
|
+
* @param obj 嵌套对象
|
|
189
|
+
* @param prefix 前缀
|
|
190
|
+
* @returns 扁平化后的对象
|
|
191
|
+
* @example
|
|
192
|
+
* flattenObject({ a: 1, b: { c: 2, d: { e: 3 } } })
|
|
193
|
+
* // { a: 1, 'b.c': 2, 'b.d.e': 3 }
|
|
194
|
+
*/
|
|
195
|
+
export declare function flattenObject(obj: Record<string, any>, prefix?: string): Record<string, any>;
|
|
196
|
+
/**
|
|
197
|
+
* 反扁平化对象
|
|
198
|
+
* @param obj 扁平对象
|
|
199
|
+
* @returns 嵌套对象
|
|
200
|
+
* @example
|
|
201
|
+
* unflattenObject({ a: 1, 'b.c': 2, 'b.d.e': 3 })
|
|
202
|
+
* // { a: 1, b: { c: 2, d: { e: 3 } } }
|
|
203
|
+
*/
|
|
204
|
+
export declare function unflattenObject(obj: Record<string, any>): Record<string, any>;
|
|
205
|
+
/**
|
|
206
|
+
* 反转对象的键值
|
|
207
|
+
* @param obj 对象
|
|
208
|
+
* @returns 键值反转后的对象
|
|
209
|
+
* @example
|
|
210
|
+
* invert({ a: 1, b: 2, c: 3 })
|
|
211
|
+
* // { '1': 'a', '2': 'b', '3': 'c' }
|
|
212
|
+
*/
|
|
213
|
+
export declare function invert<T extends Record<string, string | number>>(obj: T): Record<string, string>;
|
|
214
|
+
/**
|
|
215
|
+
* 获取对象所有值的数组
|
|
216
|
+
* @param obj 对象
|
|
217
|
+
* @returns 值的数组
|
|
218
|
+
* @example
|
|
219
|
+
* values({ a: 1, b: 2, c: 3 })
|
|
220
|
+
* // [1, 2, 3]
|
|
221
|
+
*/
|
|
222
|
+
export declare function values<T>(obj: Record<string, T>): T[];
|
|
223
|
+
/**
|
|
224
|
+
* 获取对象所有键的数组
|
|
225
|
+
* @param obj 对象
|
|
226
|
+
* @returns 键的数组
|
|
227
|
+
* @example
|
|
228
|
+
* keys({ a: 1, b: 2, c: 3 })
|
|
229
|
+
* // ['a', 'b', 'c']
|
|
230
|
+
*/
|
|
231
|
+
export declare function keys<T extends Record<string, any>>(obj: T): Array<keyof T>;
|
|
232
|
+
/**
|
|
233
|
+
* 获取对象所有键值对的数组
|
|
234
|
+
* @param obj 对象
|
|
235
|
+
* @returns 键值对数组
|
|
236
|
+
* @example
|
|
237
|
+
* entries({ a: 1, b: 2, c: 3 })
|
|
238
|
+
* // [['a', 1], ['b', 2], ['c', 3]]
|
|
239
|
+
*/
|
|
240
|
+
export declare function entries<T extends Record<string, any>>(obj: T): Array<[keyof T, T[keyof T]]>;
|
|
@@ -0,0 +1,231 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 性能优化工具函数
|
|
3
|
+
*/
|
|
4
|
+
/**
|
|
5
|
+
* 防抖函数:延迟执行,在规定时间内如果再次触发则重新计时
|
|
6
|
+
* @param fn 要执行的函数
|
|
7
|
+
* @param delay 延迟时间(毫秒)
|
|
8
|
+
* @param immediate 是否立即执行,默认 false
|
|
9
|
+
* @returns 防抖后的函数
|
|
10
|
+
* @example
|
|
11
|
+
* // 搜索框输入
|
|
12
|
+
* const handleSearch = debounce((keyword) => {
|
|
13
|
+
* console.log('搜索:', keyword);
|
|
14
|
+
* }, 300);
|
|
15
|
+
*
|
|
16
|
+
* // 自动保存
|
|
17
|
+
* const autoSave = debounce(() => {
|
|
18
|
+
* console.log('保存数据');
|
|
19
|
+
* }, 1000);
|
|
20
|
+
*/
|
|
21
|
+
export declare function debounce<T extends (...args: any[]) => any>(fn: T, delay?: number, immediate?: boolean): {
|
|
22
|
+
(...args: Parameters<T>): void;
|
|
23
|
+
cancel: () => void;
|
|
24
|
+
};
|
|
25
|
+
/**
|
|
26
|
+
* 节流函数:在规定时间内只执行一次
|
|
27
|
+
* @param fn 要执行的函数
|
|
28
|
+
* @param delay 间隔时间(毫秒)
|
|
29
|
+
* @param options 配置选项
|
|
30
|
+
* @returns 节流后的函数
|
|
31
|
+
* @example
|
|
32
|
+
* // 滚动事件
|
|
33
|
+
* const handleScroll = throttle(() => {
|
|
34
|
+
* console.log('滚动位置:', window.pageYOffset);
|
|
35
|
+
* }, 100);
|
|
36
|
+
*
|
|
37
|
+
* // 窗口resize
|
|
38
|
+
* const handleResize = throttle(() => {
|
|
39
|
+
* console.log('窗口大小:', window.innerWidth);
|
|
40
|
+
* }, 200);
|
|
41
|
+
*/
|
|
42
|
+
export declare function throttle<T extends (...args: any[]) => any>(fn: T, delay?: number, options?: {
|
|
43
|
+
leading?: boolean;
|
|
44
|
+
trailing?: boolean;
|
|
45
|
+
}): {
|
|
46
|
+
(...args: Parameters<T>): void;
|
|
47
|
+
cancel: () => void;
|
|
48
|
+
};
|
|
49
|
+
/**
|
|
50
|
+
* 只执行一次的函数
|
|
51
|
+
* @param fn 要执行的函数
|
|
52
|
+
* @returns 包装后的函数
|
|
53
|
+
* @example
|
|
54
|
+
* const initialize = once(() => {
|
|
55
|
+
* console.log('初始化,只执行一次');
|
|
56
|
+
* });
|
|
57
|
+
*
|
|
58
|
+
* initialize(); // 输出:初始化,只执行一次
|
|
59
|
+
* initialize(); // 不会执行
|
|
60
|
+
*/
|
|
61
|
+
export declare function once<T extends (...args: any[]) => any>(fn: T): (...args: Parameters<T>) => ReturnType<T> | undefined;
|
|
62
|
+
/**
|
|
63
|
+
* 延迟执行函数
|
|
64
|
+
* @param fn 要执行的函数
|
|
65
|
+
* @param delay 延迟时间(毫秒)
|
|
66
|
+
* @returns Promise
|
|
67
|
+
* @example
|
|
68
|
+
* await delay(1000); // 延迟1秒
|
|
69
|
+
* console.log('1秒后执行');
|
|
70
|
+
*/
|
|
71
|
+
export declare function delay(ms: number): Promise<void>;
|
|
72
|
+
/**
|
|
73
|
+
* 睡眠函数(async/await 友好)
|
|
74
|
+
* @param ms 睡眠时间(毫秒)
|
|
75
|
+
* @returns Promise
|
|
76
|
+
* @example
|
|
77
|
+
* await sleep(1000);
|
|
78
|
+
* console.log('1秒后执行');
|
|
79
|
+
*/
|
|
80
|
+
export declare function sleep(ms: number): Promise<void>;
|
|
81
|
+
/**
|
|
82
|
+
* 重试函数
|
|
83
|
+
* @param fn 要执行的异步函数
|
|
84
|
+
* @param options 重试配置
|
|
85
|
+
* @returns Promise
|
|
86
|
+
* @example
|
|
87
|
+
* const result = await retry(
|
|
88
|
+
* async () => await fetch('/api/data'),
|
|
89
|
+
* { times: 3, delay: 1000 }
|
|
90
|
+
* );
|
|
91
|
+
*/
|
|
92
|
+
export declare function retry<T>(fn: () => Promise<T>, options?: {
|
|
93
|
+
times?: number;
|
|
94
|
+
delay?: number;
|
|
95
|
+
onRetry?: (error: any, attempt: number) => void;
|
|
96
|
+
}): Promise<T>;
|
|
97
|
+
/**
|
|
98
|
+
* 超时控制
|
|
99
|
+
* @param fn 要执行的异步函数
|
|
100
|
+
* @param timeout 超时时间(毫秒)
|
|
101
|
+
* @returns Promise
|
|
102
|
+
* @example
|
|
103
|
+
* try {
|
|
104
|
+
* const result = await withTimeout(fetchData(), 5000);
|
|
105
|
+
* } catch (error) {
|
|
106
|
+
* console.log('请求超时');
|
|
107
|
+
* }
|
|
108
|
+
*/
|
|
109
|
+
export declare function withTimeout<T>(promise: Promise<T>, timeout: number): Promise<T>;
|
|
110
|
+
/**
|
|
111
|
+
* 函数结果缓存(记忆化)
|
|
112
|
+
* @param fn 要缓存的函数
|
|
113
|
+
* @param resolver 生成缓存 key 的函数
|
|
114
|
+
* @returns 缓存后的函数
|
|
115
|
+
* @example
|
|
116
|
+
* const fibonacci = memoize((n: number): number => {
|
|
117
|
+
* if (n <= 1) return n;
|
|
118
|
+
* return fibonacci(n - 1) + fibonacci(n - 2);
|
|
119
|
+
* });
|
|
120
|
+
*
|
|
121
|
+
* fibonacci(40); // 第一次计算
|
|
122
|
+
* fibonacci(40); // 从缓存读取,速度快
|
|
123
|
+
*/
|
|
124
|
+
export declare function memoize<T extends (...args: any[]) => any>(fn: T, resolver?: (...args: Parameters<T>) => string): T & {
|
|
125
|
+
cache: Map<string, ReturnType<T>>;
|
|
126
|
+
};
|
|
127
|
+
/**
|
|
128
|
+
* 清除缓存
|
|
129
|
+
* @param fn 缓存函数
|
|
130
|
+
* @example
|
|
131
|
+
* const cachedFn = memoize(expensiveFunction);
|
|
132
|
+
* clearCache(cachedFn); // 清除所有缓存
|
|
133
|
+
*/
|
|
134
|
+
export declare function clearCache<T extends (...args: any[]) => any>(fn: T & {
|
|
135
|
+
cache?: Map<any, any>;
|
|
136
|
+
}): void;
|
|
137
|
+
/**
|
|
138
|
+
* 批处理函数:将多次调用合并为一次
|
|
139
|
+
* @param fn 要批处理的函数
|
|
140
|
+
* @param delay 等待时间(毫秒)
|
|
141
|
+
* @returns 批处理后的函数
|
|
142
|
+
* @example
|
|
143
|
+
* const batchSave = batch((items) => {
|
|
144
|
+
* console.log('保存', items.length, '条数据');
|
|
145
|
+
* }, 100);
|
|
146
|
+
*
|
|
147
|
+
* batchSave('item1');
|
|
148
|
+
* batchSave('item2');
|
|
149
|
+
* batchSave('item3');
|
|
150
|
+
* // 100ms 后一次性保存 ['item1', 'item2', 'item3']
|
|
151
|
+
*/
|
|
152
|
+
export declare function batch<T>(fn: (items: T[]) => void, delay?: number): (item: T) => void;
|
|
153
|
+
/**
|
|
154
|
+
* 并发控制:限制同时执行的 Promise 数量
|
|
155
|
+
* @param tasks 任务数组
|
|
156
|
+
* @param limit 并发限制数
|
|
157
|
+
* @returns Promise
|
|
158
|
+
* @example
|
|
159
|
+
* const urls = [...]; // 100个URL
|
|
160
|
+
* const results = await promiseLimit(
|
|
161
|
+
* urls.map(url => () => fetch(url)),
|
|
162
|
+
* 5 // 同时最多5个请求
|
|
163
|
+
* );
|
|
164
|
+
*/
|
|
165
|
+
export declare function promiseLimit<T>(tasks: (() => Promise<T>)[], limit: number): Promise<T[]>;
|
|
166
|
+
/**
|
|
167
|
+
* 串行执行 Promise
|
|
168
|
+
* @param tasks 任务数组
|
|
169
|
+
* @returns Promise
|
|
170
|
+
* @example
|
|
171
|
+
* await promiseSerial([
|
|
172
|
+
* () => fetch('/api/1'),
|
|
173
|
+
* () => fetch('/api/2'),
|
|
174
|
+
* () => fetch('/api/3')
|
|
175
|
+
* ]); // 依次执行,等待前一个完成后再执行下一个
|
|
176
|
+
*/
|
|
177
|
+
export declare function promiseSerial<T>(tasks: (() => Promise<T>)[]): Promise<T[]>;
|
|
178
|
+
/**
|
|
179
|
+
* 测量函数执行时间
|
|
180
|
+
* @param fn 要测量的函数
|
|
181
|
+
* @param label 标签
|
|
182
|
+
* @returns 包装后的函数
|
|
183
|
+
* @example
|
|
184
|
+
* const slowFunction = measureTime(() => {
|
|
185
|
+
* // 耗时操作
|
|
186
|
+
* }, 'slowFunction');
|
|
187
|
+
*
|
|
188
|
+
* slowFunction(); // 控制台输出:slowFunction: 123.45ms
|
|
189
|
+
*/
|
|
190
|
+
export declare function measureTime<T extends (...args: any[]) => any>(fn: T, label?: string): T;
|
|
191
|
+
/**
|
|
192
|
+
* 测量异步函数执行时间
|
|
193
|
+
* @param fn 要测量的异步函数
|
|
194
|
+
* @param label 标签
|
|
195
|
+
* @returns 包装后的函数
|
|
196
|
+
* @example
|
|
197
|
+
* const asyncFn = measureTimeAsync(async () => {
|
|
198
|
+
* await fetchData();
|
|
199
|
+
* }, 'fetchData');
|
|
200
|
+
*/
|
|
201
|
+
export declare function measureTimeAsync<T extends (...args: any[]) => Promise<any>>(fn: T, label?: string): T;
|
|
202
|
+
/**
|
|
203
|
+
* 请求去重:相同参数的请求只发一次
|
|
204
|
+
* @param fn 请求函数
|
|
205
|
+
* @returns 去重后的函数
|
|
206
|
+
* @example
|
|
207
|
+
* const fetchUser = dedupe(async (id) => {
|
|
208
|
+
* return await fetch(`/api/user/${id}`);
|
|
209
|
+
* });
|
|
210
|
+
*
|
|
211
|
+
* // 同时发起多个相同请求,实际只会发一次
|
|
212
|
+
* Promise.all([
|
|
213
|
+
* fetchUser(1),
|
|
214
|
+
* fetchUser(1),
|
|
215
|
+
* fetchUser(1)
|
|
216
|
+
* ]);
|
|
217
|
+
*/
|
|
218
|
+
export declare function dedupe<T extends (...args: any[]) => Promise<any>>(fn: T): T;
|
|
219
|
+
/**
|
|
220
|
+
* RAF 节流:使用 requestAnimationFrame 实现的节流
|
|
221
|
+
* @param fn 要执行的函数
|
|
222
|
+
* @returns 节流后的函数
|
|
223
|
+
* @example
|
|
224
|
+
* const handleScroll = rafThrottle(() => {
|
|
225
|
+
* // 滚动处理,与浏览器刷新率同步
|
|
226
|
+
* });
|
|
227
|
+
*/
|
|
228
|
+
export declare function rafThrottle<T extends (...args: any[]) => any>(fn: T): {
|
|
229
|
+
(...args: Parameters<T>): void;
|
|
230
|
+
cancel: () => void;
|
|
231
|
+
};
|