@lee-zg/melange 1.0.0

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.
Files changed (55) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +256 -0
  3. package/dist/chunk-2PXWQDZC.js +659 -0
  4. package/dist/chunk-2PXWQDZC.js.map +1 -0
  5. package/dist/chunk-352XNR3C.js +716 -0
  6. package/dist/chunk-352XNR3C.js.map +1 -0
  7. package/dist/chunk-7QVYU63E.js +6 -0
  8. package/dist/chunk-7QVYU63E.js.map +1 -0
  9. package/dist/chunk-ALBD5XC5.js +285 -0
  10. package/dist/chunk-ALBD5XC5.js.map +1 -0
  11. package/dist/chunk-O7K662J5.cjs +842 -0
  12. package/dist/chunk-O7K662J5.cjs.map +1 -0
  13. package/dist/chunk-PK6SKIKE.cjs +8 -0
  14. package/dist/chunk-PK6SKIKE.cjs.map +1 -0
  15. package/dist/chunk-Q73NOVWX.cjs +789 -0
  16. package/dist/chunk-Q73NOVWX.cjs.map +1 -0
  17. package/dist/chunk-Q7XG6YN6.cjs +682 -0
  18. package/dist/chunk-Q7XG6YN6.cjs.map +1 -0
  19. package/dist/chunk-YGMBCZJQ.js +833 -0
  20. package/dist/chunk-YGMBCZJQ.js.map +1 -0
  21. package/dist/chunk-ZT6HVG4G.cjs +330 -0
  22. package/dist/chunk-ZT6HVG4G.cjs.map +1 -0
  23. package/dist/core/index.cjs +97 -0
  24. package/dist/core/index.cjs.map +1 -0
  25. package/dist/core/index.d.cts +718 -0
  26. package/dist/core/index.d.ts +718 -0
  27. package/dist/core/index.js +4 -0
  28. package/dist/core/index.js.map +1 -0
  29. package/dist/fp/index.cjs +185 -0
  30. package/dist/fp/index.cjs.map +1 -0
  31. package/dist/fp/index.d.cts +913 -0
  32. package/dist/fp/index.d.ts +913 -0
  33. package/dist/fp/index.js +4 -0
  34. package/dist/fp/index.js.map +1 -0
  35. package/dist/index.cjs +608 -0
  36. package/dist/index.cjs.map +1 -0
  37. package/dist/index.d.cts +39 -0
  38. package/dist/index.d.ts +39 -0
  39. package/dist/index.js +33 -0
  40. package/dist/index.js.map +1 -0
  41. package/dist/plugins/index.cjs +41 -0
  42. package/dist/plugins/index.cjs.map +1 -0
  43. package/dist/plugins/index.d.cts +643 -0
  44. package/dist/plugins/index.d.ts +643 -0
  45. package/dist/plugins/index.js +4 -0
  46. package/dist/plugins/index.js.map +1 -0
  47. package/dist/types-BtOUCLB-.d.cts +293 -0
  48. package/dist/types-BtOUCLB-.d.ts +293 -0
  49. package/dist/utils/index.cjs +297 -0
  50. package/dist/utils/index.cjs.map +1 -0
  51. package/dist/utils/index.d.cts +1179 -0
  52. package/dist/utils/index.d.ts +1179 -0
  53. package/dist/utils/index.js +4 -0
  54. package/dist/utils/index.js.map +1 -0
  55. package/package.json +132 -0
@@ -0,0 +1,1179 @@
1
+ import { A as AnyFunction } from '../types-BtOUCLB-.js';
2
+
3
+ /**
4
+ * @fileoverview 对象操作工具
5
+ * @module melange/utils/object
6
+ * @description 提供深度克隆、合并和
7
+ * 安全操作 JavaScript 对象的工具。
8
+ */
9
+ /**
10
+ * 创建对象的深度克隆。
11
+ * 处理嵌套对象、数组、日期和其他内置类型。
12
+ *
13
+ * @description
14
+ * 此函数创建输入的真实深度副本,意味着嵌套
15
+ * 对象也会被克隆而不是引用。它处理循环
16
+ * 引用和特殊对象类型。
17
+ *
18
+ * @example
19
+ * ```typescript
20
+ * const original = { a: 1, b: { c: 2 } };
21
+ * const clone = deepClone(original);
22
+ *
23
+ * clone.b.c = 3;
24
+ * console.log(original.b.c); // 2 (未改变)
25
+ * ```
26
+ *
27
+ * @template T - 要克隆的值的类型
28
+ * @param value - 要克隆的值
29
+ * @returns 值的深度副本
30
+ */
31
+ declare function deepClone<T>(value: T): T;
32
+ /**
33
+ * 深度合并多个对象为一个。
34
+ * 后面的对象会覆盖前面对象的冲突键。
35
+ *
36
+ * @example
37
+ * ```typescript
38
+ * const base = { a: 1, b: { c: 2, d: 3 } };
39
+ * const override = { b: { c: 4 } };
40
+ * const merged = deepMerge(base, override);
41
+ * // { a: 1, b: { c: 4, d: 3 } }
42
+ * ```
43
+ *
44
+ * @template T - 合并对象的类型
45
+ * @param objects - 要合并的对象
46
+ * @returns 合并后的对象
47
+ */
48
+ declare function deepMerge<T extends object>(...objects: Partial<T>[]): T;
49
+ /**
50
+ * 从对象中选择指定属性。
51
+ *
52
+ * @example
53
+ * ```typescript
54
+ * const user = { id: 1, name: 'John', email: 'john@example.com' };
55
+ * const partial = pick(user, ['id', 'name']);
56
+ * // { id: 1, name: 'John' }
57
+ * ```
58
+ *
59
+ * @template T - 对象类型
60
+ * @template K - 要选择的键
61
+ * @param obj - 源对象
62
+ * @param keys - 要选择的键
63
+ * @returns 只包含指定键的新对象
64
+ */
65
+ declare function pick<T extends object, K extends keyof T>(obj: T, keys: readonly K[]): Pick<T, K>;
66
+ /**
67
+ * 从对象中省略指定属性。
68
+ *
69
+ * @example
70
+ * ```typescript
71
+ * const user = { id: 1, name: 'John', password: 'secret' };
72
+ * const safe = omit(user, ['password']);
73
+ * // { id: 1, name: 'John' }
74
+ * ```
75
+ *
76
+ * @template T - 对象类型
77
+ * @template K - 要省略的键
78
+ * @param obj - 源对象
79
+ * @param keys - 要省略的键
80
+ * @returns 不包含指定键的新对象
81
+ */
82
+ declare function omit<T extends object, K extends keyof T>(obj: T, keys: readonly K[]): Omit<T, K>;
83
+ /**
84
+ * 使用路径字符串或数组获取嵌套属性值。
85
+ *
86
+ * @example
87
+ * ```typescript
88
+ * const obj = { a: { b: { c: 42 } } };
89
+ * get(obj, 'a.b.c'); // 42
90
+ * get(obj, ['a', 'b', 'c']); // 42
91
+ * get(obj, 'a.x.y', 'default'); // 'default'
92
+ * ```
93
+ *
94
+ * @template T - 默认值类型
95
+ * @param obj - 要获取的对象
96
+ * @param path - 属性路径
97
+ * @param defaultValue - 路径不存在时的默认值
98
+ * @returns 路径处的值或默认值
99
+ */
100
+ declare function get<T = unknown>(obj: unknown, path: string | readonly string[], defaultValue?: T): T;
101
+ /**
102
+ * 使用路径字符串或数组设置嵌套属性值。
103
+ * 根据需要创建中间对象。
104
+ *
105
+ * @example
106
+ * ```typescript
107
+ * const obj = { a: { b: 1 } };
108
+ * set(obj, 'a.c.d', 42);
109
+ * // { a: { b: 1, c: { d: 42 } } }
110
+ * ```
111
+ *
112
+ * @template T - 对象类型
113
+ * @param obj - 要修改的对象
114
+ * @param path - 属性路径
115
+ * @param value - 要设置的值
116
+ * @returns 修改后的对象
117
+ */
118
+ declare function set<T extends object>(obj: T, path: string | readonly string[], value: unknown): T;
119
+ /**
120
+ * 检查对象是否具有嵌套属性。
121
+ *
122
+ * @example
123
+ * ```typescript
124
+ * const obj = { a: { b: { c: 42 } } };
125
+ * has(obj, 'a.b.c'); // true
126
+ * has(obj, 'a.x'); // false
127
+ * ```
128
+ *
129
+ * @param obj - 要检查的对象
130
+ * @param path - 要检查的路径
131
+ * @returns 如果路径存在则返回 true
132
+ */
133
+ declare function has(obj: unknown, path: string | readonly string[]): boolean;
134
+ /**
135
+ * 检查值是否为普通对象(非数组、null 或其他类型)。
136
+ *
137
+ * @param value - 要检查的值
138
+ * @returns 如果值是普通对象则返回 true
139
+ */
140
+ declare function isPlainObject(value: unknown): value is Record<string, unknown>;
141
+ /**
142
+ * 从键值对创建对象。
143
+ *
144
+ * @example
145
+ * ```typescript
146
+ * fromEntries([['a', 1], ['b', 2]]);
147
+ * // { a: 1, b: 2 }
148
+ * ```
149
+ *
150
+ * @template K - 键类型
151
+ * @template V - 值类型
152
+ * @param entries - 键值对数组
153
+ * @returns 从条目创建的对象
154
+ */
155
+ declare function fromEntries<K extends string | number | symbol, V>(entries: readonly (readonly [K, V])[]): Record<K, V>;
156
+ /**
157
+ * 映射对象值同时保留键。
158
+ *
159
+ * @example
160
+ * ```typescript
161
+ * const prices = { apple: 1, banana: 2 };
162
+ * const doubled = mapValues(prices, v => v * 2);
163
+ * // { apple: 2, banana: 4 }
164
+ * ```
165
+ *
166
+ * @template T - 原始值类型
167
+ * @template U - 映射值类型
168
+ * @param obj - 要映射的对象
169
+ * @param fn - 映射函数
170
+ * @returns 具有映射值的对象
171
+ */
172
+ declare function mapValues<T, U>(obj: Record<string, T>, fn: (value: T, key: string) => U): Record<string, U>;
173
+ /**
174
+ * 基于谓词过滤对象条目。
175
+ *
176
+ * @example
177
+ * ```typescript
178
+ * const prices = { apple: 1, banana: 2, cherry: 3 };
179
+ * const expensive = filterObject(prices, v => v > 1);
180
+ * // { banana: 2, cherry: 3 }
181
+ * ```
182
+ *
183
+ * @template T - 值类型
184
+ * @param obj - 要过滤的对象
185
+ * @param predicate - 过滤函数
186
+ * @returns 过滤后的对象
187
+ */
188
+ declare function filterObject<T>(obj: Record<string, T>, predicate: (value: T, key: string) => boolean): Record<string, T>;
189
+
190
+ /**
191
+ * @fileoverview 数组操作工具
192
+ * @module melange/utils/array
193
+ * @description 提供操作数组的工具,包括
194
+ * 分块、扁平化、分组和排序。
195
+ */
196
+ /**
197
+ * 将数组拆分为指定大小的块。
198
+ *
199
+ * @example
200
+ * ```typescript
201
+ * chunk([1, 2, 3, 4, 5], 2);
202
+ * // [[1, 2], [3, 4], [5]]
203
+ * ```
204
+ *
205
+ * @template T - 数组元素类型
206
+ * @param array - 要分块的数组
207
+ * @param size - 块大小
208
+ * @returns 块数组
209
+ */
210
+ declare function chunk<T>(array: readonly T[], size: number): T[][];
211
+ /**
212
+ * 将嵌套数组扁平化一级。
213
+ *
214
+ * @example
215
+ * ```typescript
216
+ * flatten([[1, 2], [3, 4], [5]]);
217
+ * // [1, 2, 3, 4, 5]
218
+ * ```
219
+ *
220
+ * @template T - 数组元素类型
221
+ * @param array - 要扁平化的嵌套数组
222
+ * @returns 扁平化后的数组
223
+ */
224
+ declare function flatten<T>(array: readonly (T | readonly T[])[]): T[];
225
+ /**
226
+ * 深度扁平化嵌套数组。
227
+ *
228
+ * @example
229
+ * ```typescript
230
+ * flattenDeep([1, [2, [3, [4, 5]]]]);
231
+ * // [1, 2, 3, 4, 5]
232
+ * ```
233
+ *
234
+ * @template T - 数组元素类型
235
+ * @param array - 要扁平化的嵌套数组
236
+ * @returns 深度扁平化后的数组
237
+ */
238
+ declare function flattenDeep<T>(array: readonly unknown[]): T[];
239
+ /**
240
+ * 返回数组中的唯一元素。
241
+ *
242
+ * @example
243
+ * ```typescript
244
+ * unique([1, 2, 2, 3, 3, 3]);
245
+ * // [1, 2, 3]
246
+ *
247
+ * const users = [{ id: 1 }, { id: 2 }, { id: 1 }];
248
+ * unique(users, u => u.id);
249
+ * // [{ id: 1 }, { id: 2 }]
250
+ * ```
251
+ *
252
+ * @template T - 数组元素类型
253
+ * @template K - 唯一性的键类型
254
+ * @param array - 要去重的数组
255
+ * @param keyFn - 提取比较键的可选函数
256
+ * @returns 包含唯一元素的数组
257
+ */
258
+ declare function unique<T, K = T>(array: readonly T[], keyFn?: (item: T) => K): T[];
259
+ /**
260
+ * 按键对数组元素进行分组。
261
+ *
262
+ * @example
263
+ * ```typescript
264
+ * const users = [
265
+ * { name: 'Alice', age: 25 },
266
+ * { name: 'Bob', age: 30 },
267
+ * { name: 'Charlie', age: 25 }
268
+ * ];
269
+ *
270
+ * groupBy(users, u => u.age);
271
+ * // {
272
+ * // 25: [{ name: 'Alice', age: 25 }, { name: 'Charlie', age: 25 }],
273
+ * // 30: [{ name: 'Bob', age: 30 }]
274
+ * // }
275
+ * ```
276
+ *
277
+ * @template T - 数组元素类型
278
+ * @template K - 分组键类型
279
+ * @param array - 要分组的数组
280
+ * @param keyFn - 提取分组键的函数
281
+ * @returns 包含分组元素的对象
282
+ */
283
+ declare function groupBy<T, K extends string | number | symbol>(array: readonly T[], keyFn: (item: T) => K): Record<K, T[]>;
284
+ /**
285
+ * 按键对数组进行排序,返回新的排序数组。
286
+ *
287
+ * @example
288
+ * ```typescript
289
+ * const users = [
290
+ * { name: 'Charlie', age: 25 },
291
+ * { name: 'Alice', age: 30 },
292
+ * { name: 'Bob', age: 20 }
293
+ * ];
294
+ *
295
+ * sortBy(users, u => u.age);
296
+ * // 按年龄升序排列
297
+ *
298
+ * sortBy(users, u => u.name, 'desc');
299
+ * // 按姓名降序排列
300
+ * ```
301
+ *
302
+ * @template T - 数组元素类型
303
+ * @template K - 排序键类型
304
+ * @param array - 要排序的数组
305
+ * @param keyFn - 提取排序键的函数
306
+ * @param order - 排序顺序: 'asc' 或 'desc'
307
+ * @returns 新的排序数组
308
+ */
309
+ declare function sortBy<T, K extends string | number>(array: readonly T[], keyFn: (item: T) => K, order?: 'asc' | 'desc'): T[];
310
+ /**
311
+ * 根据谓词将数组分区为两个数组。
312
+ *
313
+ * @example
314
+ * ```typescript
315
+ * const [evens, odds] = partition([1, 2, 3, 4, 5], n => n % 2 === 0);
316
+ * // evens = [2, 4], odds = [1, 3, 5]
317
+ * ```
318
+ *
319
+ * @template T - 数组元素类型
320
+ * @param array - 要分区的数组
321
+ * @param predicate - 分区谓词
322
+ * @returns [匹配, 不匹配] 数组的元组
323
+ */
324
+ declare function partition<T>(array: readonly T[], predicate: (item: T) => boolean): [T[], T[]];
325
+ /**
326
+ * 将多个数组压缩在一起。
327
+ *
328
+ * @example
329
+ * ```typescript
330
+ * zip([1, 2, 3], ['a', 'b', 'c']);
331
+ * // [[1, 'a'], [2, 'b'], [3, 'c']]
332
+ *
333
+ * zip([1, 2], ['a', 'b', 'c']);
334
+ * // [[1, 'a'], [2, 'b']]
335
+ * ```
336
+ *
337
+ * @template T - 第一个数组元素类型
338
+ * @template U - 第二个数组元素类型
339
+ * @param arr1 - 第一个数组
340
+ * @param arr2 - 第二个数组
341
+ * @returns 元组数组
342
+ */
343
+ declare function zip<T, U>(arr1: readonly T[], arr2: readonly U[]): Array<[T, U]>;
344
+ /**
345
+ * 返回数组的第一个元素,如果为空则返回 undefined。
346
+ *
347
+ * @example
348
+ * ```typescript
349
+ * first([1, 2, 3]); // 1
350
+ * first([]); // undefined
351
+ * ```
352
+ *
353
+ * @template T - 数组元素类型
354
+ * @param array - 数组
355
+ * @returns 第一个元素或 undefined
356
+ */
357
+ declare function first<T>(array: readonly T[]): T | undefined;
358
+ /**
359
+ * 返回数组的最后一个元素,如果为空则返回 undefined。
360
+ *
361
+ * @example
362
+ * ```typescript
363
+ * last([1, 2, 3]); // 3
364
+ * last([]); // undefined
365
+ * ```
366
+ *
367
+ * @template T - 数组元素类型
368
+ * @param array - 数组
369
+ * @returns 最后一个元素或 undefined
370
+ */
371
+ declare function last<T>(array: readonly T[]): T | undefined;
372
+ /**
373
+ * 从数组中返回随机元素。
374
+ *
375
+ * @example
376
+ * ```typescript
377
+ * sample([1, 2, 3, 4, 5]); // 随机元素
378
+ * ```
379
+ *
380
+ * @template T - 数组元素类型
381
+ * @param array - 数组
382
+ * @returns 随机元素,如果为空则返回 undefined
383
+ */
384
+ declare function sample<T>(array: readonly T[]): T | undefined;
385
+ /**
386
+ * 随机打乱数组(Fisher-Yates 算法),返回新数组。
387
+ *
388
+ * @example
389
+ * ```typescript
390
+ * shuffle([1, 2, 3, 4, 5]); // 随机打乱
391
+ * ```
392
+ *
393
+ * @template T - 数组元素类型
394
+ * @param array - 要打乱的数组
395
+ * @returns 新的打乱数组
396
+ */
397
+ declare function shuffle<T>(array: readonly T[]): T[];
398
+ /**
399
+ * 创建从开始到结束(不包含)的数字数组。
400
+ *
401
+ * @example
402
+ * ```typescript
403
+ * range(5); // [0, 1, 2, 3, 4]
404
+ * range(1, 5); // [1, 2, 3, 4]
405
+ * range(0, 10, 2); // [0, 2, 4, 6, 8]
406
+ * ```
407
+ *
408
+ * @param startOrEnd - 如果只有这一个参数,则为结束值;否则为开始值
409
+ * @param end - 结束值(不包含)
410
+ * @param step - 数字之间的步长
411
+ * @returns 数字数组
412
+ */
413
+ declare function range(startOrEnd: number, end?: number, step?: number): number[];
414
+ /**
415
+ * 返回两个数组的交集。
416
+ *
417
+ * @example
418
+ * ```typescript
419
+ * intersection([1, 2, 3], [2, 3, 4]); // [2, 3]
420
+ * ```
421
+ *
422
+ * @template T - 数组元素类型
423
+ * @param arr1 - 第一个数组
424
+ * @param arr2 - 第二个数组
425
+ * @returns 两个数组中存在的元素
426
+ */
427
+ declare function intersection<T>(arr1: readonly T[], arr2: readonly T[]): T[];
428
+ /**
429
+ * 返回两个数组的差集(在 arr1 中但不在 arr2 中的元素)。
430
+ *
431
+ * @example
432
+ * ```typescript
433
+ * difference([1, 2, 3], [2, 3, 4]); // [1]
434
+ * ```
435
+ *
436
+ * @template T - 数组元素类型
437
+ * @param arr1 - 第一个数组
438
+ * @param arr2 - 第二个数组
439
+ * @returns 在 arr1 中但不在 arr2 中的元素
440
+ */
441
+ declare function difference<T>(arr1: readonly T[], arr2: readonly T[]): T[];
442
+
443
+ /**
444
+ * @fileoverview 字符串操作工具
445
+ * @module melange/utils/string
446
+ * @description 提供字符串操作工具,包括
447
+ * 大小写转换、截断和格式化。
448
+ */
449
+ /**
450
+ * 将字符串的第一个字母大写。
451
+ *
452
+ * @example
453
+ * ```typescript
454
+ * capitalize('hello'); // 'Hello'
455
+ * capitalize('HELLO'); // 'HELLO'
456
+ * ```
457
+ *
458
+ * @param str - 要大写的字符串
459
+ * @returns 大写后的字符串
460
+ */
461
+ declare function capitalize(str: string): string;
462
+ /**
463
+ * 将字符串转换为驼峰命名法。
464
+ *
465
+ * @example
466
+ * ```typescript
467
+ * camelCase('hello world'); // 'helloWorld'
468
+ * camelCase('hello-world'); // 'helloWorld'
469
+ * camelCase('hello_world'); // 'helloWorld'
470
+ * camelCase('HelloWorld'); // 'helloWorld'
471
+ * ```
472
+ *
473
+ * @param str - 要转换的字符串
474
+ * @returns 驼峰命名法字符串
475
+ */
476
+ declare function camelCase(str: string): string;
477
+ /**
478
+ * 将字符串转换为帕斯卡命名法。
479
+ *
480
+ * @example
481
+ * ```typescript
482
+ * pascalCase('hello world'); // 'HelloWorld'
483
+ * pascalCase('hello-world'); // 'HelloWorld'
484
+ * pascalCase('hello_world'); // 'HelloWorld'
485
+ * ```
486
+ *
487
+ * @param str - 要转换的字符串
488
+ * @returns 帕斯卡命名法字符串
489
+ */
490
+ declare function pascalCase(str: string): string;
491
+ /**
492
+ * 将字符串转换为蛇形命名法。
493
+ *
494
+ * @example
495
+ * ```typescript
496
+ * snakeCase('helloWorld'); // 'hello_world'
497
+ * snakeCase('HelloWorld'); // 'hello_world'
498
+ * snakeCase('hello-world'); // 'hello_world'
499
+ * ```
500
+ *
501
+ * @param str - 要转换的字符串
502
+ * @returns 蛇形命名法字符串
503
+ */
504
+ declare function snakeCase(str: string): string;
505
+ /**
506
+ * 将字符串转换为烤肉串命名法。
507
+ *
508
+ * @example
509
+ * ```typescript
510
+ * kebabCase('helloWorld'); // 'hello-world'
511
+ * kebabCase('HelloWorld'); // 'hello-world'
512
+ * kebabCase('hello_world'); // 'hello-world'
513
+ * ```
514
+ *
515
+ * @param str - 要转换的字符串
516
+ * @returns 烤肉串命名法字符串
517
+ */
518
+ declare function kebabCase(str: string): string;
519
+ /**
520
+ * 将字符串转换为常量命名法。
521
+ *
522
+ * @example
523
+ * ```typescript
524
+ * constantCase('helloWorld'); // 'HELLO_WORLD'
525
+ * constantCase('hello-world'); // 'HELLO_WORLD'
526
+ * ```
527
+ *
528
+ * @param str - 要转换的字符串
529
+ * @returns 常量命名法字符串
530
+ */
531
+ declare function constantCase(str: string): string;
532
+ /**
533
+ * 将字符串截断到最大长度,必要时添加省略号。
534
+ *
535
+ * @example
536
+ * ```typescript
537
+ * truncate('Hello, World!', 10); // 'Hello, ...'
538
+ * truncate('Hello', 10); // 'Hello'
539
+ * truncate('Hello, World!', 10, '…'); // 'Hello, Wo…'
540
+ * ```
541
+ *
542
+ * @param str - 要截断的字符串
543
+ * @param maxLength - 最大长度
544
+ * @param suffix - 要添加的后缀(默认:'...')
545
+ * @returns 截断后的字符串
546
+ */
547
+ declare function truncate(str: string, maxLength: number, suffix?: string): string;
548
+ /**
549
+ * 在字符串左侧填充到最小长度。
550
+ *
551
+ * @example
552
+ * ```typescript
553
+ * padStart('5', 3, '0'); // '005'
554
+ * padStart('42', 3, '0'); // '042'
555
+ * ```
556
+ *
557
+ * @param str - 要填充的字符串
558
+ * @param length - 目标长度
559
+ * @param padChar - 填充字符
560
+ * @returns 填充后的字符串
561
+ */
562
+ declare function padStart(str: string, length: number, padChar?: string): string;
563
+ /**
564
+ * 在字符串右侧填充到最小长度。
565
+ *
566
+ * @example
567
+ * ```typescript
568
+ * padEnd('5', 3, '0'); // '500'
569
+ * padEnd('42', 3, '0'); // '420'
570
+ * ```
571
+ *
572
+ * @param str - 要填充的字符串
573
+ * @param length - 目标长度
574
+ * @param padChar - 填充字符
575
+ * @returns 填充后的字符串
576
+ */
577
+ declare function padEnd(str: string, length: number, padChar?: string): string;
578
+ /**
579
+ * 删除前导和尾随空白并折叠多个空格。
580
+ *
581
+ * @example
582
+ * ```typescript
583
+ * collapseWhitespace(' hello world '); // 'hello world'
584
+ * ```
585
+ *
586
+ * @param str - 要清理的字符串
587
+ * @returns 清理后的字符串
588
+ */
589
+ declare function collapseWhitespace(str: string): string;
590
+ /**
591
+ * 转义特殊字符以在 HTML 中使用。
592
+ *
593
+ * @example
594
+ * ```typescript
595
+ * escapeHtml('<script>alert("xss")</script>');
596
+ * // '&lt;script&gt;alert(&quot;xss&quot;)&lt;/script&gt;'
597
+ * ```
598
+ *
599
+ * @param str - 要转义的字符串
600
+ * @returns 转义后的字符串
601
+ */
602
+ declare function escapeHtml(str: string): string;
603
+ /**
604
+ * 反转义 HTML 实体。
605
+ *
606
+ * @example
607
+ * ```typescript
608
+ * unescapeHtml('&lt;div&gt;Hello&lt;/div&gt;');
609
+ * // '<div>Hello</div>'
610
+ * ```
611
+ *
612
+ * @param str - 要反转义的字符串
613
+ * @returns 反转义后的字符串
614
+ */
615
+ declare function unescapeHtml(str: string): string;
616
+ /**
617
+ * 生成指定长度的随机字符串。
618
+ *
619
+ * @example
620
+ * ```typescript
621
+ * randomString(10); // 'a1b2c3d4e5'
622
+ * randomString(8, 'abc'); // 'abcabcab'
623
+ * ```
624
+ *
625
+ * @param length - 要生成的字符串长度
626
+ * @param chars - 要使用的字符(默认:字母数字)
627
+ * @returns 随机字符串
628
+ */
629
+ declare function randomString(length: number, chars?: string): string;
630
+ /**
631
+ * 从字符串中提取单词。
632
+ *
633
+ * @example
634
+ * ```typescript
635
+ * words('helloWorld'); // ['hello', 'World']
636
+ * words('hello-world'); // ['hello', 'world']
637
+ * words('hello_world'); // ['hello', 'world']
638
+ * ```
639
+ *
640
+ * @param str - 要提取单词的字符串
641
+ * @returns 单词数组
642
+ */
643
+ declare function words(str: string): string[];
644
+ /**
645
+ * 将字符串转换为标题大小写。
646
+ *
647
+ * @example
648
+ * ```typescript
649
+ * titleCase('hello world'); // 'Hello World'
650
+ * titleCase('the quick brown fox'); // 'The Quick Brown Fox'
651
+ * ```
652
+ *
653
+ * @param str - 要转换的字符串
654
+ * @returns 标题大小写字符串
655
+ */
656
+ declare function titleCase(str: string): string;
657
+ /**
658
+ * 反转字符串。
659
+ *
660
+ * @example
661
+ * ```typescript
662
+ * reverse('hello'); // 'olleh'
663
+ * ```
664
+ *
665
+ * @param str - 要反转的字符串
666
+ * @returns 反转后的字符串
667
+ */
668
+ declare function reverse(str: string): string;
669
+ /**
670
+ * 计算子字符串的出现次数。
671
+ *
672
+ * @example
673
+ * ```typescript
674
+ * countOccurrences('hello hello hello', 'hello'); // 3
675
+ * ```
676
+ *
677
+ * @param str - 要搜索的字符串
678
+ * @param substr - 要计数的子字符串
679
+ * @returns 出现次数
680
+ */
681
+ declare function countOccurrences(str: string, substr: string): number;
682
+
683
+ /**
684
+ * @fileoverview 计时和速率限制工具
685
+ * @module melange/utils/timing
686
+ * @description 提供防抖、节流、延迟、
687
+ * 重试和超时处理的工具。
688
+ */
689
+
690
+ /**
691
+ * 创建函数的防抖版本。
692
+ * 防抖函数会延迟调用,直到自上次调用防抖函数以来经过了 `wait` 毫秒。
693
+ *
694
+ * @description
695
+ * 防抖对于限制用户输入事件(如在搜索框中打字)的速率很有用,
696
+ * 您希望等到用户停止打字后再进行 API 调用。
697
+ *
698
+ * @example
699
+ * ```typescript
700
+ * const search = debounce((query: string) => {
701
+ * console.log('搜索中:', query);
702
+ * }, 300);
703
+ *
704
+ * search('h');
705
+ * search('he');
706
+ * search('hel');
707
+ * search('hell');
708
+ * search('hello');
709
+ * // 只在 300ms 无调用后记录 '搜索中: hello'
710
+ * ```
711
+ *
712
+ * @template T - 函数类型
713
+ * @param fn - 要防抖的函数
714
+ * @param wait - 等待的毫秒数
715
+ * @param options - 可选配置
716
+ * @returns 函数的防抖版本
717
+ */
718
+ declare function debounce<T extends AnyFunction>(fn: T, wait: number, options?: {
719
+ leading?: boolean;
720
+ trailing?: boolean;
721
+ }): T & {
722
+ cancel: () => void;
723
+ flush: () => void;
724
+ };
725
+ /**
726
+ * 创建函数的节流版本。
727
+ * 节流函数每 `limit` 毫秒最多调用一次。
728
+ *
729
+ * @description
730
+ * 节流对于限制频繁触发的事件(如滚动或调整大小事件)的速率很有用,
731
+ * 您希望确保处理程序以一致的速率运行。
732
+ *
733
+ * @example
734
+ * ```typescript
735
+ * const onScroll = throttle(() => {
736
+ * console.log('滚动位置:', window.scrollY);
737
+ * }, 100);
738
+ *
739
+ * window.addEventListener('scroll', onScroll);
740
+ * // 滚动期间最多每 100ms 记录一次
741
+ * ```
742
+ *
743
+ * @template T - 函数类型
744
+ * @param fn - 要节流的函数
745
+ * @param limit - 调用之间的最小时间
746
+ * @returns 函数的节流版本
747
+ */
748
+ declare function throttle<T extends AnyFunction>(fn: T, limit: number): T & {
749
+ cancel: () => void;
750
+ };
751
+ /**
752
+ * 创建在指定延迟后解决的承诺。
753
+ *
754
+ * @example
755
+ * ```typescript
756
+ * console.log('开始');
757
+ * await delay(1000);
758
+ * console.log('1秒后');
759
+ *
760
+ * // 带值
761
+ * const result = await delay(1000, 'hello');
762
+ * console.log(result); // 'hello'
763
+ * ```
764
+ *
765
+ * @template T - 值类型
766
+ * @param ms - 延迟毫秒数
767
+ * @param value - 解决时的可选值
768
+ * @returns 在延迟后解决的承诺
769
+ */
770
+ declare function delay<T = void>(ms: number, value?: T): Promise<T>;
771
+ /**
772
+ * 重试异步函数直到成功或达到最大重试次数。
773
+ *
774
+ * @example
775
+ * ```typescript
776
+ * const result = await retry(
777
+ * () => fetch('/api/data').then(r => r.json()),
778
+ * {
779
+ * maxRetries: 3,
780
+ * delay: 1000,
781
+ * backoff: 'exponential'
782
+ * }
783
+ * );
784
+ * ```
785
+ *
786
+ * @template T - 返回类型
787
+ * @param fn - 要重试的异步函数
788
+ * @param options - 重试选项
789
+ * @returns 函数的结果
790
+ * @throws 如果所有重试都失败,则抛出最后一个错误
791
+ */
792
+ declare function retry<T>(fn: () => Promise<T>, options?: {
793
+ maxRetries?: number;
794
+ delay?: number;
795
+ backoff?: 'none' | 'linear' | 'exponential';
796
+ onRetry?: (error: unknown, attempt: number) => void;
797
+ }): Promise<T>;
798
+ /**
799
+ * 用超时包装承诺。
800
+ *
801
+ * @example
802
+ * ```typescript
803
+ * try {
804
+ * const result = await timeout(fetch('/api/data'), 5000);
805
+ * } catch (error) {
806
+ * if (error.message.includes('timed out')) {
807
+ * console.log('请求耗时太长');
808
+ * }
809
+ * }
810
+ * ```
811
+ *
812
+ * @template T - 承诺结果类型
813
+ * @param promise - 要包装的承诺
814
+ * @param ms - 超时毫秒数
815
+ * @param errorMessage - 可选的自定义错误消息
816
+ * @returns 如果超过超时时间则拒绝的承诺
817
+ */
818
+ declare function timeout<T>(promise: Promise<T>, ms: number, errorMessage?: string): Promise<T>;
819
+ /**
820
+ * 创建可以从外部解决或拒绝的延迟承诺。
821
+ *
822
+ * @example
823
+ * ```typescript
824
+ * const deferred = createDeferred<string>();
825
+ *
826
+ * setTimeout(() => {
827
+ * deferred.resolve('Hello!');
828
+ * }, 1000);
829
+ *
830
+ * const result = await deferred.promise; // 'Hello!'
831
+ * ```
832
+ *
833
+ * @template T - 承诺结果类型
834
+ * @returns 带有 promise、resolve 和 reject 的延迟对象
835
+ */
836
+ declare function createDeferred<T>(): {
837
+ promise: Promise<T>;
838
+ resolve: (value: T) => void;
839
+ reject: (reason?: unknown) => void;
840
+ };
841
+ /**
842
+ * 以并发限制运行多个异步函数。
843
+ *
844
+ * @example
845
+ * ```typescript
846
+ * const urls = ['url1', 'url2', 'url3', 'url4', 'url5'];
847
+ *
848
+ * const results = await parallel(
849
+ * urls.map(url => () => fetch(url)),
850
+ * { concurrency: 2 }
851
+ * );
852
+ * ```
853
+ *
854
+ * @template T - 结果类型
855
+ * @param tasks - 异步任务函数数组
856
+ * @param options - 配置选项
857
+ * @returns 结果数组
858
+ */
859
+ declare function parallel<T>(tasks: Array<() => Promise<T>>, options?: {
860
+ concurrency?: number;
861
+ }): Promise<T[]>;
862
+ /**
863
+ * 顺序运行任务,一个接一个。
864
+ *
865
+ * @example
866
+ * ```typescript
867
+ * const results = await sequence([
868
+ * () => fetch('/api/step1'),
869
+ * () => fetch('/api/step2'),
870
+ * () => fetch('/api/step3'),
871
+ * ]);
872
+ * ```
873
+ *
874
+ * @template T - 结果类型
875
+ * @param tasks - 异步任务函数数组
876
+ * @returns 结果数组
877
+ */
878
+ declare function sequence<T>(tasks: Array<() => Promise<T>>): Promise<T[]>;
879
+
880
+ /**
881
+ * @fileoverview 类型检查和验证工具
882
+ * @module melange/utils/guards
883
+ * @description 提供类型守卫和验证函数用于
884
+ * 运行时类型检查。
885
+ */
886
+ /**
887
+ * 检查值是否为字符串。
888
+ *
889
+ * @example
890
+ * ```typescript
891
+ * isString('hello'); // true
892
+ * isString(123); // false
893
+ * ```
894
+ *
895
+ * @param value - 要检查的值
896
+ * @returns 如果值是字符串则返回 true
897
+ */
898
+ declare function isString(value: unknown): value is string;
899
+ /**
900
+ * 检查值是否为数字(且不是 NaN)。
901
+ *
902
+ * @example
903
+ * ```typescript
904
+ * isNumber(123); // true
905
+ * isNumber(NaN); // false
906
+ * isNumber('123'); // false
907
+ * ```
908
+ *
909
+ * @param value - 要检查的值
910
+ * @returns 如果值是有限数字则返回 true
911
+ */
912
+ declare function isNumber(value: unknown): value is number;
913
+ /**
914
+ * 检查值是否为布尔值。
915
+ *
916
+ * @example
917
+ * ```typescript
918
+ * isBoolean(true); // true
919
+ * isBoolean(false); // true
920
+ * isBoolean(0); // false
921
+ * ```
922
+ *
923
+ * @param value - 要检查的值
924
+ * @returns 如果值是布尔值则返回 true
925
+ */
926
+ declare function isBoolean(value: unknown): value is boolean;
927
+ /**
928
+ * 检查值是否为对象(非 null 或数组)。
929
+ *
930
+ * @example
931
+ * ```typescript
932
+ * isObject({}); // true
933
+ * isObject([]); // false
934
+ * isObject(null); // false
935
+ * ```
936
+ *
937
+ * @param value - 要检查的值
938
+ * @returns 如果值是普通对象则返回 true
939
+ */
940
+ declare function isObject(value: unknown): value is Record<string, unknown>;
941
+ /**
942
+ * 检查值是否为数组。
943
+ *
944
+ * @example
945
+ * ```typescript
946
+ * isArray([]); // true
947
+ * isArray([1, 2]); // true
948
+ * isArray({}); // false
949
+ * ```
950
+ *
951
+ * @param value - 要检查的值
952
+ * @returns 如果值是数组则返回 true
953
+ */
954
+ declare function isArray(value: unknown): value is unknown[];
955
+ /**
956
+ * 检查值是否为函数。
957
+ *
958
+ * @example
959
+ * ```typescript
960
+ * isFunction(() => {}); // true
961
+ * isFunction(function() {}); // true
962
+ * isFunction(async () => {}); // true
963
+ * isFunction({}); // false
964
+ * ```
965
+ *
966
+ * @param value - 要检查的值
967
+ * @returns 如果值是函数则返回 true
968
+ */
969
+ declare function isFunction(value: unknown): value is (...args: unknown[]) => unknown;
970
+ /**
971
+ * 检查值是否为 null 或 undefined。
972
+ *
973
+ * @example
974
+ * ```typescript
975
+ * isNil(null); // true
976
+ * isNil(undefined); // true
977
+ * isNil(0); // false
978
+ * isNil(''); // false
979
+ * ```
980
+ *
981
+ * @param value - 要检查的值
982
+ * @returns 如果值是 null 或 undefined 则返回 true
983
+ */
984
+ declare function isNil(value: unknown): value is null | undefined;
985
+ /**
986
+ * 检查值是否不为 null 或 undefined。
987
+ *
988
+ * @example
989
+ * ```typescript
990
+ * isNotNil(0); // true
991
+ * isNotNil(''); // true
992
+ * isNotNil(null); // false
993
+ * isNotNil(undefined); // false
994
+ * ```
995
+ *
996
+ * @template T - 值类型
997
+ * @param value - 要检查的值
998
+ * @returns 如果值不是 null 或 undefined 则返回 true
999
+ */
1000
+ declare function isNotNil<T>(value: T | null | undefined): value is T;
1001
+ /**
1002
+ * 检查值是否为"空"。
1003
+ * 空值:null, undefined, '', [], {}, Map(0), Set(0)
1004
+ *
1005
+ * @example
1006
+ * ```typescript
1007
+ * isEmpty(''); // true
1008
+ * isEmpty([]); // true
1009
+ * isEmpty({}); // true
1010
+ * isEmpty(null); // true
1011
+ * isEmpty(0); // false
1012
+ * isEmpty('hello'); // false
1013
+ * isEmpty([1]); // false
1014
+ * ```
1015
+ *
1016
+ * @param value - 要检查的值
1017
+ * @returns 如果值为空则返回 true
1018
+ */
1019
+ declare function isEmpty(value: unknown): boolean;
1020
+ /**
1021
+ * 检查值是否不为"空"。
1022
+ *
1023
+ * @example
1024
+ * ```typescript
1025
+ * isNotEmpty('hello'); // true
1026
+ * isNotEmpty([1, 2]); // true
1027
+ * isNotEmpty(''); // false
1028
+ * isNotEmpty([]); // false
1029
+ * ```
1030
+ *
1031
+ * @param value - 要检查的值
1032
+ * @returns 如果值不为空则返回 true
1033
+ */
1034
+ declare function isNotEmpty(value: unknown): boolean;
1035
+ /**
1036
+ * 检查值是否为有效的 Date 对象。
1037
+ *
1038
+ * @example
1039
+ * ```typescript
1040
+ * isDate(new Date()); // true
1041
+ * isDate(new Date('invalid')); // false
1042
+ * isDate('2023-01-01'); // false
1043
+ * ```
1044
+ *
1045
+ * @param value - 要检查的值
1046
+ * @returns 如果值是有效日期则返回 true
1047
+ */
1048
+ declare function isDate(value: unknown): value is Date;
1049
+ /**
1050
+ * 检查值是否为 Promise。
1051
+ *
1052
+ * @example
1053
+ * ```typescript
1054
+ * isPromise(Promise.resolve()); // true
1055
+ * isPromise(async () => {}); // false (函数,不是 promise)
1056
+ * isPromise({ then: () => {} }); // true (thenable)
1057
+ * ```
1058
+ *
1059
+ * @param value - 要检查的值
1060
+ * @returns 如果值是 Promise 或 thenable 则返回 true
1061
+ */
1062
+ declare function isPromise<T = unknown>(value: unknown): value is Promise<T>;
1063
+ /**
1064
+ * 检查值是否为 Symbol。
1065
+ *
1066
+ * @param value - 要检查的值
1067
+ * @returns 如果值是 Symbol 则返回 true
1068
+ */
1069
+ declare function isSymbol(value: unknown): value is symbol;
1070
+ /**
1071
+ * 检查值是否为 BigInt。
1072
+ *
1073
+ * @param value - 要检查的值
1074
+ * @returns 如果值是 BigInt 则返回 true
1075
+ */
1076
+ declare function isBigInt(value: unknown): value is bigint;
1077
+ /**
1078
+ * 检查值是否为有效的电子邮件地址。
1079
+ *
1080
+ * @example
1081
+ * ```typescript
1082
+ * isEmail('user@example.com'); // true
1083
+ * isEmail('user@'); // false
1084
+ * isEmail('not-an-email'); // false
1085
+ * ```
1086
+ *
1087
+ * @param value - 要检查的值
1088
+ * @returns 如果值是有效邮箱则返回 true
1089
+ */
1090
+ declare function isEmail(value: unknown): boolean;
1091
+ /**
1092
+ * 检查值是否为有效的 UUID v4。
1093
+ *
1094
+ * @example
1095
+ * ```typescript
1096
+ * isUUID('550e8400-e29b-41d4-a716-446655440000'); // true
1097
+ * isUUID('not-a-uuid'); // false
1098
+ * ```
1099
+ *
1100
+ * @param value - 要检查的值
1101
+ * @returns 如果值是有效 UUID 则返回 true
1102
+ */
1103
+ declare function isUUID(value: unknown): boolean;
1104
+ /**
1105
+ * 检查值是否为有效的 URL。
1106
+ *
1107
+ * @example
1108
+ * ```typescript
1109
+ * isURL('https://example.com'); // true
1110
+ * isURL('http://localhost:3000'); // true
1111
+ * isURL('not-a-url'); // false
1112
+ * ```
1113
+ *
1114
+ * @param value - 要检查的值
1115
+ * @returns 如果值是有效 URL 则返回 true
1116
+ */
1117
+ declare function isURL(value: unknown): boolean;
1118
+ /**
1119
+ * 检查值是否为整数。
1120
+ *
1121
+ * @example
1122
+ * ```typescript
1123
+ * isInteger(42); // true
1124
+ * isInteger(42.0); // true
1125
+ * isInteger(42.5); // false
1126
+ * isInteger('42'); // false
1127
+ * ```
1128
+ *
1129
+ * @param value - 要检查的值
1130
+ * @returns 如果值是整数则返回 true
1131
+ */
1132
+ declare function isInteger(value: unknown): value is number;
1133
+ /**
1134
+ * 检查值是否为正数。
1135
+ *
1136
+ * @example
1137
+ * ```typescript
1138
+ * isPositive(1); // true
1139
+ * isPositive(0); // false
1140
+ * isPositive(-1); // false
1141
+ * ```
1142
+ *
1143
+ * @param value - 要检查的值
1144
+ * @returns 如果值是正数则返回 true
1145
+ */
1146
+ declare function isPositive(value: unknown): value is number;
1147
+ /**
1148
+ * 检查值是否为负数。
1149
+ *
1150
+ * @example
1151
+ * ```typescript
1152
+ * isNegative(-1); // true
1153
+ * isNegative(0); // false
1154
+ * isNegative(1); // false
1155
+ * ```
1156
+ *
1157
+ * @param value - 要检查的值
1158
+ * @returns 如果值是负数则返回 true
1159
+ */
1160
+ declare function isNegative(value: unknown): value is number;
1161
+ /**
1162
+ * 检查值是否在范围内(包含边界)。
1163
+ *
1164
+ * @example
1165
+ * ```typescript
1166
+ * isInRange(5, 1, 10); // true
1167
+ * isInRange(0, 1, 10); // false
1168
+ * isInRange(1, 1, 10); // true
1169
+ * isInRange(10, 1, 10); // true
1170
+ * ```
1171
+ *
1172
+ * @param value - 要检查的值
1173
+ * @param min - 最小值
1174
+ * @param max - 最大值
1175
+ * @returns 如果值在范围内则返回 true
1176
+ */
1177
+ declare function isInRange(value: unknown, min: number, max: number): value is number;
1178
+
1179
+ export { camelCase, capitalize, chunk, collapseWhitespace, constantCase, countOccurrences, createDeferred, debounce, deepClone, deepMerge, delay, difference, escapeHtml, filterObject, first, flatten, flattenDeep, fromEntries, get, groupBy, has, intersection, isArray, isBigInt, isBoolean, isDate, isEmail, isEmpty, isFunction, isInRange, isInteger, isNegative, isNil, isNotEmpty, isNotNil, isNumber, isObject, isPlainObject, isPositive, isPromise, isString, isSymbol, isURL, isUUID, kebabCase, last, mapValues, omit, padEnd, padStart, parallel, partition, pascalCase, pick, randomString, range, retry, reverse, sample, sequence, set, shuffle, snakeCase, sortBy, throttle, timeout, titleCase, truncate, unescapeHtml, unique, words, zip };