@pmun/utils 0.2.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.
@@ -0,0 +1,1201 @@
1
+ import { Dayjs, QUnitType, OpUnitType, ManipulateType } from 'dayjs';
2
+
3
+ /**
4
+ * @module 数组操作
5
+ * @description 提供各种数组处理函数,包括数组转换、查询、过滤等实用功能。这些函数都不会修改原始数组,而是返回新的数组或值。
6
+ */
7
+ /**
8
+ * 从数组中移除指定的项
9
+ * @param array 原始数组
10
+ * @param item 要移除的项
11
+ * @returns 移除指定项后的新数组,不会修改原始数组
12
+ * @example
13
+ * ```ts
14
+ * const numbers = [1, 2, 3, 4, 5]
15
+ * const newArray = remove(numbers, 3) // [1, 2, 4, 5]
16
+ * console.log(numbers) // 原数组不变: [1, 2, 3, 4, 5]
17
+ *
18
+ * // 移除字符串
19
+ * const fruits = ['苹果', '香蕉', '橙子']
20
+ * const newFruits = remove(fruits, '香蕉') // ['苹果', '橙子']
21
+ * ```
22
+ */
23
+ declare function remove<T>(array: T[], item: T): T[];
24
+ /**
25
+ * 数组去重,移除数组中的重复元素
26
+ * @param array 原始数组
27
+ * @returns 去重后的新数组,保持原始顺序
28
+ * @example
29
+ * ```ts
30
+ * const numbers = [1, 2, 2, 3, 3, 4, 5, 5]
31
+ * const uniqueArray = unique(numbers) // [1, 2, 3, 4, 5]
32
+ *
33
+ * // 对象数组会基于引用去重,内容相同但引用不同的对象会被视为不同元素
34
+ * const objArray = [{id: 1}, {id: 2}, {id: 1}]
35
+ * const uniqueObjArray = unique(objArray) // [{id: 1}, {id: 2}, {id: 1}]
36
+ * ```
37
+ */
38
+ declare function unique<T>(array: T[]): T[];
39
+ /**
40
+ * 将数组分成指定大小的块
41
+ * @param array 原始数组
42
+ * @param size 每个块的大小,必须为正整数
43
+ * @returns 二维数组,每个子数组最多包含 size 个元素
44
+ * @example
45
+ * ```ts
46
+ * const numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
47
+ * const chunks = chunk(numbers, 3)
48
+ * // 结果: [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10]]
49
+ *
50
+ * // 如果 size 小于等于 0,则返回包含原始数组的数组
51
+ * chunk(numbers, 0) // [[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]]
52
+ * ```
53
+ */
54
+ declare function chunk<T>(array: T[], size: number): T[][];
55
+ /**
56
+ * 获取数组中的最后一个元素
57
+ * @param array 数组
58
+ * @returns 最后一个元素,如果数组为空则返回 undefined
59
+ * @example
60
+ * ```ts
61
+ * const numbers = [1, 2, 3, 4, 5]
62
+ * const lastNumber = last(numbers) // 5
63
+ *
64
+ * const empty = []
65
+ * const lastEmpty = last(empty) // undefined
66
+ * ```
67
+ */
68
+ declare function last<T>(array: T[]): T | undefined;
69
+ /**
70
+ * 获取数组中的第一个元素
71
+ * @param array 数组
72
+ * @returns 第一个元素,如果数组为空则返回 undefined
73
+ * @example
74
+ * ```ts
75
+ * const numbers = [1, 2, 3, 4, 5]
76
+ * const firstNumber = first(numbers) // 1
77
+ *
78
+ * const empty = []
79
+ * const firstEmpty = first(empty) // undefined
80
+ * ```
81
+ */
82
+ declare function first<T>(array: T[]): T | undefined;
83
+ /**
84
+ * 随机打乱数组元素顺序
85
+ * @param array 原始数组
86
+ * @returns 打乱后的新数组,不会修改原始数组
87
+ * @example
88
+ * ```ts
89
+ * const numbers = [1, 2, 3, 4, 5]
90
+ * const shuffled = shuffle(numbers)
91
+ * // 可能的结果: [3, 1, 5, 2, 4]
92
+ * console.log(numbers) // 原数组不变: [1, 2, 3, 4, 5]
93
+ * ```
94
+ */
95
+ declare function shuffle<T>(array: T[]): T[];
96
+ /**
97
+ * 从数组中随机选择一个元素
98
+ * @param array 数组
99
+ * @returns 随机选择的元素,如果数组为空则返回 undefined
100
+ * @example
101
+ * ```ts
102
+ * const fruits = ['苹果', '香蕉', '橙子', '梨', '葡萄']
103
+ * const randomFruit = sample(fruits)
104
+ * // 可能返回: '香蕉'
105
+ *
106
+ * const empty = []
107
+ * const emptyResult = sample(empty) // undefined
108
+ * ```
109
+ */
110
+ declare function sample<T>(array: T[]): T | undefined;
111
+ /**
112
+ * 比较两个数组是否相等(元素顺序和值都相同)
113
+ * @param a 第一个数组
114
+ * @param b 第二个数组
115
+ * @returns 如果两个数组长度相同且对应位置的元素全部相等则返回 true,否则返回 false
116
+ * @example
117
+ * ```ts
118
+ * isEqual([1, 2, 3], [1, 2, 3]) // true
119
+ * isEqual([1, 2, 3], [1, 3, 2]) // false,顺序不同
120
+ * isEqual([1, 2, 3], [1, 2]) // false,长度不同
121
+ *
122
+ * // 对象比较是基于引用的
123
+ * const obj = { id: 1 }
124
+ * isEqual([obj], [obj]) // true,同一个对象引用
125
+ * isEqual([{ id: 1 }], [{ id: 1 }]) // false,不同对象引用
126
+ * ```
127
+ */
128
+ declare function isEqual<T>(a: T[], b: T[]): boolean;
129
+ /**
130
+ * 根据条件对数组中的元素进行分组
131
+ * @param array 原始数组
132
+ * @param keyFn 用于生成分组键的函数,接收数组元素并返回用作分组键的值
133
+ * @returns 分组后的对象,键为分组键,值为对应的元素数组
134
+ * @example
135
+ * ```ts
136
+ * const people = [
137
+ * { name: '张三', age: 25 },
138
+ * { name: '李四', age: 30 },
139
+ * { name: '王五', age: 25 },
140
+ * { name: '赵六', age: 30 }
141
+ * ]
142
+ *
143
+ * // 按年龄分组
144
+ * const groupedByAge = groupBy(people, person => person.age)
145
+ * // 结果:
146
+ * // {
147
+ * // 25: [{ name: '张三', age: 25 }, { name: '王五', age: 25 }],
148
+ * // 30: [{ name: '李四', age: 30 }, { name: '赵六', age: 30 }]
149
+ * // }
150
+ *
151
+ * // 使用字符串键
152
+ * const groupedByAgeRange = groupBy(people, person =>
153
+ * person.age < 30 ? '青年' : '成年')
154
+ * // 结果:
155
+ * // {
156
+ * // '青年': [{ name: '张三', age: 25 }, { name: '王五', age: 25 }],
157
+ * // '成年': [{ name: '李四', age: 30 }, { name: '赵六', age: 30 }]
158
+ * // }
159
+ * ```
160
+ */
161
+ declare function groupBy<T, K extends string | number | symbol>(array: T[], keyFn: (item: T) => K): Record<K, T[]>;
162
+ /**
163
+ * 在选项数组前添加一个"全部"选项
164
+ *
165
+ * @param options - 原始选项数组
166
+ * @param config - 配置选项
167
+ * @param config.name - 选项标签的字段名 (默认为 'label')
168
+ * @param config.value - "全部"选项的值 (默认为 '')
169
+ * @param config.valueKey - 选项值的字段名 (默认为 'value')
170
+ * @returns 添加了"全部"选项的新数组
171
+ *
172
+ * @example
173
+ * ```ts
174
+ * // 基本用法
175
+ * const options = [
176
+ * { label: '选项1', value: 1 },
177
+ * { label: '选项2', value: 2 }
178
+ * ]
179
+ * const result = appendUniversalOption(options)
180
+ * // 结果: [
181
+ * // { label: '全部', value: '' },
182
+ * // { label: '选项1', value: 1 },
183
+ * // { label: '选项2', value: 2 }
184
+ * // ]
185
+ *
186
+ * // 自定义字段名和值
187
+ * const customOptions = [
188
+ * { text: '选项1', id: 1 },
189
+ * { text: '选项2', id: 2 }
190
+ * ]
191
+ * const customResult = appendUniversalOption(customOptions, {
192
+ * name: 'text',
193
+ * valueKey: 'id',
194
+ * value: 0
195
+ * })
196
+ * // 结果: [
197
+ * // { text: '全部', id: 0 },
198
+ * // { text: '选项1', id: 1 },
199
+ * // { text: '选项2', id: 2 }
200
+ * // ]
201
+ * ```
202
+ */
203
+ declare function appendUniversalOption<T extends Record<string, any>, V = any>(options: T[], { name, value, valueKey, }?: {
204
+ name?: string;
205
+ value?: V;
206
+ valueKey?: string;
207
+ }): T[];
208
+
209
+ /**
210
+ * 创建 dayjs 对象
211
+ * @param date 日期参数,可以是日期对象、时间戳或日期字符串
212
+ * @returns dayjs 对象
213
+ * @example
214
+ * ```ts
215
+ * createDate() // 当前日期时间
216
+ * createDate('2023-05-15') // 指定日期
217
+ * createDate(1684123456000) // 时间戳
218
+ * ```
219
+ */
220
+ declare function createDate(date?: string | number | Date | Dayjs): Dayjs;
221
+ /**
222
+ * 格式化日期为指定格式的字符串
223
+ * @param date 日期对象、时间戳或日期字符串
224
+ * @param format 格式字符串,支持的占位符请参考 dayjs 文档
225
+ * @returns 格式化后的日期字符串
226
+ * @example
227
+ * ```ts
228
+ * formatDate(new Date(), 'YYYY-MM-DD HH:mm:ss') // "2023-05-16 14:30:45"
229
+ * formatDate('2023-05-15', 'YYYY年MM月DD日') // "2023年05月15日"
230
+ * formatDate(1684123456000, 'MM/DD/YYYY') // "05/15/2023"
231
+ * ```
232
+ */
233
+ declare function formatDate(date: Date | string | number, format?: string): string;
234
+ /**
235
+ * 获取当前日期时间的时间戳
236
+ * @returns 当前时间戳(毫秒)
237
+ * @example
238
+ * ```ts
239
+ * now() // 例如: 1684123456789
240
+ * ```
241
+ */
242
+ declare function now(): number;
243
+ /**
244
+ * 解析日期字符串为日期对象
245
+ * @param dateStr 日期字符串
246
+ * @returns 日期对象
247
+ * @throws 如果日期格式无效,抛出 TypeError
248
+ * @example
249
+ * ```ts
250
+ * parseDate('2023-05-15') // Date 对象: Mon May 15 2023 00:00:00
251
+ * parseDate('2023/05/15 14:30:45') // Date 对象: Mon May 15 2023 14:30:45
252
+ * parseDate('invalid date') // 抛出 TypeError: 无效的日期格式
253
+ * ```
254
+ */
255
+ declare function parseDate(dateStr: string): Date;
256
+ /**
257
+ * 计算两个日期之间的差异
258
+ * @param date1 第一个日期
259
+ * @param date2 第二个日期
260
+ * @param unit 计量单位,默认为 'day',可以是 'year', 'month', 'week', 'day', 'hour', 'minute', 'second', 'millisecond'
261
+ * @returns 两个日期之间的差值,正数表示 date1 晚于 date2,负数表示 date1 早于 date2
262
+ * @example
263
+ * ```ts
264
+ * diff('2023-05-15', '2023-05-10') // 5(相差5天)
265
+ * diff('2023-05-15', '2023-06-15', 'month') // -1(相差1个月,且第一个日期早于第二个)
266
+ * diff('2023-05-15 08:00', '2023-05-15 06:00', 'hour') // 2(相差2小时)
267
+ * ```
268
+ */
269
+ declare function diff(date1: Date | string | number, date2: Date | string | number, unit?: QUnitType | OpUnitType): number;
270
+ /**
271
+ * 向日期添加指定时间
272
+ * @param date 原始日期
273
+ * @param amount 要添加的数量,可以为负数
274
+ * @param unit 时间单位,默认为 'day',可以是 'year', 'month', 'week', 'day', 'hour', 'minute', 'second', 'millisecond'
275
+ * @returns 添加后的新日期对象
276
+ * @example
277
+ * ```ts
278
+ * add(new Date('2023-05-15'), 2, 'day') // Date 对象: Wed May 17 2023
279
+ * add('2023-05-15', -1, 'month') // Date 对象: Sat Apr 15 2023
280
+ * add('2023-05-15 12:00', 30, 'minute') // Date 对象: Mon May 15 2023 12:30:00
281
+ * ```
282
+ */
283
+ declare function add(date: Date | string | number, amount: number, unit?: ManipulateType): Date;
284
+ /**
285
+ * 向日期添加指定天数
286
+ * @param date 原始日期
287
+ * @param days 要添加的天数(可以为负数)
288
+ * @returns 添加天数后的新日期对象
289
+ * @example
290
+ * ```ts
291
+ * addDays(new Date('2023-05-15'), 5) // Date 对象: Sat May 20 2023
292
+ * addDays('2023-05-15', -3) // Date 对象: Fri May 12 2023
293
+ * ```
294
+ */
295
+ declare function addDays(date: Date | string | number, days: number): Date;
296
+ /**
297
+ * 向日期添加指定月数
298
+ * @param date 原始日期
299
+ * @param months 要添加的月数(可以为负数)
300
+ * @returns 添加月数后的新日期对象
301
+ * @example
302
+ * ```ts
303
+ * addMonths(new Date('2023-05-15'), 2) // Date 对象: Sat Jul 15 2023
304
+ * addMonths('2023-05-31', 1) // Date 对象: Fri Jun 30 2023(注意月份天数自动调整)
305
+ * addMonths('2023-01-15', -3) // Date 对象: Wed Oct 15 2022
306
+ * ```
307
+ */
308
+ declare function addMonths(date: Date | string | number, months: number): Date;
309
+ /**
310
+ * 向日期添加指定年数
311
+ * @param date 原始日期
312
+ * @param years 要添加的年数(可以为负数)
313
+ * @returns 添加年数后的新日期对象
314
+ * @example
315
+ * ```ts
316
+ * addYears(new Date('2023-05-15'), 1) // Date 对象: Wed May 15 2024
317
+ * addYears('2023-05-15', -3) // Date 对象: Fri May 15 2020
318
+ * addYears('2024-02-29', 1) // Date 对象: Fri Feb 28 2025(注意闰年自动调整)
319
+ * ```
320
+ */
321
+ declare function addYears(date: Date | string | number, years: number): Date;
322
+ /**
323
+ * 获取指定日期是一周中的第几天
324
+ * @param date 日期
325
+ * @param startOnMonday 是否从周一开始计算(默认为 false,即从周日开始)
326
+ * @returns 一周中的第几天(0-6),周日为 0,周六为 6;如果 startOnMonday 为 true,则周一为 0,周日为 6
327
+ * @example
328
+ * ```ts
329
+ * getDayOfWeek(new Date('2023-05-15')) // 1(周一,从周日开始算是第1天)
330
+ * getDayOfWeek('2023-05-15', true) // 0(周一,从周一开始算是第0天)
331
+ * getDayOfWeek('2023-05-14') // 0(周日,从周日开始算是第0天)
332
+ * getDayOfWeek('2023-05-14', true) // 6(周日,从周一开始算是第6天)
333
+ * ```
334
+ */
335
+ declare function getDayOfWeek(date: Date | string | number, startOnMonday?: boolean): number;
336
+ /**
337
+ * 检查日期是否在指定范围内
338
+ * @param date 要检查的日期
339
+ * @param startDate 范围起始日期
340
+ * @param endDate 范围结束日期
341
+ * @returns 如果日期在指定范围内(不包括边界)则返回 true,否则返回 false
342
+ * @example
343
+ * ```ts
344
+ * isDateInRange('2023-05-15', '2023-05-10', '2023-05-20') // true
345
+ * isDateInRange('2023-05-15', '2023-05-15', '2023-05-20') // false(不包括开始日期)
346
+ * isDateInRange('2023-05-15', '2023-05-10', '2023-05-15') // false(不包括结束日期)
347
+ * ```
348
+ */
349
+ declare function isDateInRange(date: Date | string | number, startDate: Date | string | number, endDate: Date | string | number): boolean;
350
+ /**
351
+ * 获取指定月份的天数
352
+ * @param year 年份
353
+ * @param month 月份(0-11),0 表示一月,11 表示十二月
354
+ * @returns 该月的天数
355
+ * @example
356
+ * ```ts
357
+ * getDaysInMonth(2023, 1) // 28(2023年2月有28天)
358
+ * getDaysInMonth(2024, 1) // 29(2024年2月有29天,闰年)
359
+ * getDaysInMonth(2023, 0) // 31(2023年1月有31天)
360
+ * ```
361
+ */
362
+ declare function getDaysInMonth(year: number, month: number): number;
363
+ /**
364
+ * 获取相对时间描述
365
+ * @param date 日期
366
+ * @param baseDate 基准日期,默认为当前时间
367
+ * @returns 相对时间描述,如"几分钟前"、"几天后"等
368
+ * @example
369
+ * ```ts
370
+ * fromNow(new Date(Date.now() - 5 * 60 * 1000)) // "5分钟前"
371
+ * fromNow(new Date(Date.now() + 24 * 60 * 60 * 1000)) // "1天后"
372
+ * fromNow('2023-01-01', '2023-01-05') // "4天前"
373
+ * ```
374
+ */
375
+ declare function fromNow(date: Date | string | number, baseDate?: Date | string | number): string;
376
+ /**
377
+ * 获取日期的开始时间
378
+ * @param date 日期
379
+ * @param unit 单位,可以是 'year', 'month', 'week', 'day', 'hour', 'minute', 'second'
380
+ * @returns 单位开始时间的日期对象
381
+ * @example
382
+ * ```ts
383
+ * startOf(new Date('2023-05-15 15:30:45'), 'day') // Date 对象: Mon May 15 2023 00:00:00
384
+ * startOf('2023-05-15 15:30:45', 'month') // Date 对象: Mon May 01 2023 00:00:00
385
+ * startOf('2023-05-15 15:30:45', 'hour') // Date 对象: Mon May 15 2023 15:00:00
386
+ * ```
387
+ */
388
+ declare function startOf(date: Date | string | number, unit: OpUnitType): Date;
389
+ /**
390
+ * 获取日期的结束时间
391
+ * @param date 日期
392
+ * @param unit 单位,可以是 'year', 'month', 'week', 'day', 'hour', 'minute', 'second'
393
+ * @returns 单位结束时间的日期对象
394
+ * @example
395
+ * ```ts
396
+ * endOf(new Date('2023-05-15 15:30:45'), 'day') // Date 对象: Mon May 15 2023 23:59:59.999
397
+ * endOf('2023-05-15 15:30:45', 'month') // Date 对象: Wed May 31 2023 23:59:59.999
398
+ * endOf('2023-05-15 15:30:45', 'hour') // Date 对象: Mon May 15 2023 15:59:59.999
399
+ * ```
400
+ */
401
+ declare function endOf(date: Date | string | number, unit: OpUnitType): Date;
402
+ /**
403
+ * 格式化日期为人类友好的格式
404
+ * @param date 日期
405
+ * @returns 人类友好的日期描述,如"今天 HH:mm"、"昨天 HH:mm"、"明天 HH:mm"或"YYYY-MM-DD HH:mm"
406
+ * @example
407
+ * ```ts
408
+ * formatHumanReadable(new Date()) // "今天 15:30"
409
+ * formatHumanReadable(new Date(Date.now() - 24 * 60 * 60 * 1000)) // "昨天 15:30"
410
+ * formatHumanReadable(new Date(Date.now() + 24 * 60 * 60 * 1000)) // "明天 15:30"
411
+ * formatHumanReadable('2023-01-01') // "2023-01-01 00:00"
412
+ * ```
413
+ */
414
+ declare function formatHumanReadable(date: Date | string | number): string;
415
+
416
+ /**
417
+ * @module 类型检查
418
+ * @description 提供各种数据类型检查函数,用于判断值的类型、判断对象特性等
419
+ */
420
+ /**
421
+ * 检查值是否为字符串类型
422
+ * @param val 要检查的值
423
+ * @returns 如果是字符串则返回 true,否则返回 false
424
+ * @example
425
+ * ```ts
426
+ * isString('hello') // true
427
+ * isString(123) // false
428
+ * isString(new String('hello')) // false(注意:包装对象不是原始字符串类型)
429
+ * ```
430
+ */
431
+ declare function isString(val: unknown): val is string;
432
+ /**
433
+ * 检查值是否为数字类型
434
+ * @param val 要检查的值
435
+ * @returns 如果是数字则返回 true,否则返回 false(NaN 会返回 false)
436
+ * @example
437
+ * ```ts
438
+ * isNumber(123) // true
439
+ * isNumber(-45.67) // true
440
+ * isNumber('123') // false
441
+ * isNumber(NaN) // false
442
+ * isNumber(Infinity) // true
443
+ * ```
444
+ */
445
+ declare function isNumber(val: unknown): val is number;
446
+ /**
447
+ * 检查值是否为布尔类型
448
+ * @param val 要检查的值
449
+ * @returns 如果是布尔值则返回 true,否则返回 false
450
+ * @example
451
+ * ```ts
452
+ * isBoolean(true) // true
453
+ * isBoolean(false) // true
454
+ * isBoolean(0) // false
455
+ * isBoolean('false') // false
456
+ * isBoolean(new Boolean(true)) // false(包装对象不是原始布尔类型)
457
+ * ```
458
+ */
459
+ declare function isBoolean(val: unknown): val is boolean;
460
+ /**
461
+ * 检查值是否为函数类型
462
+ * @param val 要检查的值
463
+ * @returns 如果是函数则返回 true,否则返回 false
464
+ * @example
465
+ * ```ts
466
+ * isFunction(() => {}) // true
467
+ * isFunction(function() {}) // true
468
+ * isFunction(class {}) // true
469
+ * isFunction(Math.sin) // true
470
+ * isFunction({}) // false
471
+ * ```
472
+ */
473
+ declare function isFunction(val: unknown): val is ((...args: any[]) => any);
474
+ /**
475
+ * 检查值是否为对象类型(不包括 null)
476
+ * @param val 要检查的值
477
+ * @returns 如果是对象则返回 true,否则返回 false
478
+ * @example
479
+ * ```ts
480
+ * isObject({}) // true
481
+ * isObject([]) // true(数组也是对象)
482
+ * isObject(new Date()) // true
483
+ * isObject(null) // false
484
+ * isObject(undefined) // false
485
+ * isObject(123) // false
486
+ * ```
487
+ */
488
+ declare function isObject(val: unknown): val is Record<any, any>;
489
+ /**
490
+ * 检查值是否为数组类型
491
+ * @param val 要检查的值
492
+ * @returns 如果是数组则返回 true,否则返回 false
493
+ * @example
494
+ * ```ts
495
+ * isArray([]) // true
496
+ * isArray([1, 2, 3]) // true
497
+ * isArray(new Array()) // true
498
+ * isArray({}) // false
499
+ * isArray('abc') // false
500
+ * ```
501
+ */
502
+ declare function isArray(val: unknown): val is any[];
503
+ /**
504
+ * 检查值是否为日期类型
505
+ * @param val 要检查的值
506
+ * @returns 如果是日期对象则返回 true,否则返回 false
507
+ * @example
508
+ * ```ts
509
+ * isDate(new Date()) // true
510
+ * isDate(Date.now()) // false(时间戳是数字,不是日期对象)
511
+ * isDate('2023-05-15') // false(日期字符串不是日期对象)
512
+ * ```
513
+ */
514
+ declare function isDate(val: unknown): val is Date;
515
+ /**
516
+ * 检查值是否为正则表达式
517
+ * @param val 要检查的值
518
+ * @returns 如果是正则表达式则返回 true,否则返回 false
519
+ * @example
520
+ * ```ts
521
+ * isRegExp(/abc/) // true
522
+ * isRegExp(new RegExp('abc')) // true
523
+ * isRegExp('/abc/') // false(字符串不是正则表达式)
524
+ * isRegExp({}) // false
525
+ * ```
526
+ */
527
+ declare function isRegExp(val: unknown): val is RegExp;
528
+ /**
529
+ * 检查值是否为 Promise
530
+ * @param val 要检查的值
531
+ * @returns 如果是 Promise 则返回 true,否则返回 false
532
+ * @example
533
+ * ```ts
534
+ * isPromise(Promise.resolve()) // true
535
+ * isPromise(new Promise(() => {})) // true
536
+ * isPromise({ then: () => {}, catch: () => {} }) // true(类 Promise 对象也会返回 true)
537
+ * isPromise({}) // false
538
+ * isPromise(null) // false
539
+ * ```
540
+ */
541
+ declare function isPromise<T = any>(val: unknown): val is Promise<T>;
542
+ /**
543
+ * 检查值是否为 Map
544
+ * @param val 要检查的值
545
+ * @returns 如果是 Map 则返回 true,否则返回 false
546
+ * @example
547
+ * ```ts
548
+ * isMap(new Map()) // true
549
+ * isMap(new WeakMap()) // false
550
+ * isMap({}) // false
551
+ * ```
552
+ */
553
+ declare function isMap<K = any, V = any>(val: unknown): val is Map<K, V>;
554
+ /**
555
+ * 检查值是否为 Set
556
+ * @param val 要检查的值
557
+ * @returns 如果是 Set 则返回 true,否则返回 false
558
+ * @example
559
+ * ```ts
560
+ * isSet(new Set()) // true
561
+ * isSet(new WeakSet()) // false
562
+ * isSet([]) // false
563
+ * ```
564
+ */
565
+ declare function isSet<T = any>(val: unknown): val is Set<T>;
566
+ /**
567
+ * 检查值是否为 Symbol
568
+ * @param val 要检查的值
569
+ * @returns 如果是 Symbol 则返回 true,否则返回 false
570
+ * @example
571
+ * ```ts
572
+ * isSymbol(Symbol('foo')) // true
573
+ * isSymbol(Symbol.for('bar')) // true
574
+ * isSymbol('symbol') // false
575
+ * ```
576
+ */
577
+ declare function isSymbol(val: unknown): val is symbol;
578
+ /**
579
+ * 检查值是否为原始类型(string、number、boolean、symbol、bigint、null、undefined)
580
+ * @param val 要检查的值
581
+ * @returns 如果是原始类型则返回 true,否则返回 false
582
+ * @example
583
+ * ```ts
584
+ * isPrimitive('hello') // true
585
+ * isPrimitive(123) // true
586
+ * isPrimitive(true) // true
587
+ * isPrimitive(Symbol()) // true
588
+ * isPrimitive(null) // true
589
+ * isPrimitive(undefined) // true
590
+ * isPrimitive(BigInt(123)) // true
591
+ * isPrimitive({}) // false
592
+ * isPrimitive([]) // false
593
+ * isPrimitive(() => {}) // false
594
+ * ```
595
+ */
596
+ declare function isPrimitive(val: unknown): boolean;
597
+ /**
598
+ * 检查值是否为 undefined
599
+ * @param val 要检查的值
600
+ * @returns 如果是 undefined 则返回 true,否则返回 false
601
+ * @example
602
+ * ```ts
603
+ * isUndefined(undefined) // true
604
+ * isUndefined(void 0) // true
605
+ * isUndefined(null) // false
606
+ * isUndefined('') // false
607
+ * ```
608
+ */
609
+ declare function isUndefined(val: unknown): val is undefined;
610
+ /**
611
+ * 检查值是否为 null
612
+ * @param val 要检查的值
613
+ * @returns 如果是 null 则返回 true,否则返回 false
614
+ * @example
615
+ * ```ts
616
+ * isNull(null) // true
617
+ * isNull(undefined) // false
618
+ * isNull(0) // false
619
+ * isNull('') // false
620
+ * ```
621
+ */
622
+ declare function isNull(val: unknown): val is null;
623
+ /**
624
+ * 检查值是否为 null 或 undefined
625
+ * @param val 要检查的值
626
+ * @returns 如果是 null 或 undefined 则返回 true,否则返回 false
627
+ * @example
628
+ * ```ts
629
+ * isNullOrUndefined(null) // true
630
+ * isNullOrUndefined(undefined) // true
631
+ * isNullOrUndefined(void 0) // true
632
+ * isNullOrUndefined('') // false
633
+ * isNullOrUndefined(0) // false
634
+ * ```
635
+ */
636
+ declare function isNullOrUndefined(val: unknown): val is null | undefined;
637
+ /**
638
+ * 检查对象是否为空对象(没有自身可枚举属性)
639
+ * @param val 要检查的值
640
+ * @returns 如果是空对象则返回 true,否则返回 false;如果不是对象类型则返回 false
641
+ * @example
642
+ * ```ts
643
+ * isEmptyObject({}) // true
644
+ * isEmptyObject({ a: 1 }) // false
645
+ * isEmptyObject([]) // true(空数组也会返回 true)
646
+ * isEmptyObject(null) // false(不是对象)
647
+ * isEmptyObject(Object.create(null)) // true
648
+ * isEmptyObject(Object.create({ toString: () => '' })) // true(不包括继承的属性)
649
+ * ```
650
+ */
651
+ declare function isEmptyObject(val: unknown): boolean;
652
+ /**
653
+ * 检查值是否为空(null、undefined、空字符串、空数组或空对象)
654
+ * @param val 要检查的值
655
+ * @returns 如果是空值则返回 true,否则返回 false
656
+ * @example
657
+ * ```ts
658
+ * isEmpty(null) // true
659
+ * isEmpty(undefined) // true
660
+ * isEmpty('') // true
661
+ * isEmpty(' ') // true(空白字符串也视为空)
662
+ * isEmpty([]) // true
663
+ * isEmpty({}) // true
664
+ * isEmpty(0) // false
665
+ * isEmpty(false) // false
666
+ * isEmpty(' hello ') // false
667
+ * ```
668
+ */
669
+ declare function isEmpty(val: unknown): boolean;
670
+ /**
671
+ * 检查值是否为 NaN
672
+ * @param value 要检查的值
673
+ * @returns 如果值是 NaN 则返回 true,否则返回 false
674
+ * @example
675
+ * ```ts
676
+ * isNaN(NaN) // true
677
+ * isNaN(Number('abc')) // true
678
+ * isNaN(0 / 0) // true
679
+ * isNaN(123) // false
680
+ * isNaN('123') // false(不同于全局 isNaN,此函数不会先尝试转换为数字)
681
+ * ```
682
+ */
683
+ declare function isNaN(value: unknown): boolean;
684
+ /**
685
+ * 检查值是否为普通对象(由 Object 构造函数创建或对象字面量)
686
+ *
687
+ * @param {unknown} value - 要检查的值
688
+ * @returns {boolean} 如果值是普通对象则返回 true,否则返回 false
689
+ *
690
+ * @example
691
+ * ```ts
692
+ * isPlainObject({}) // true
693
+ * isPlainObject({ a: 1 }) // true
694
+ * isPlainObject(new Date()) // false
695
+ * isPlainObject([]) // false
696
+ * isPlainObject(null) // false
697
+ * ```
698
+ */
699
+ declare function isPlainObject(value: unknown): value is Record<string, any>;
700
+
701
+ /**
702
+ * @module 数字处理
703
+ * @description 提供各种数字处理函数,包括数字格式化、计算、范围控制等实用功能
704
+ */
705
+ /**
706
+ * 将数字四舍五入到指定小数位
707
+ * @param num 要处理的数字
708
+ * @param precision 小数位数,默认为0(整数)
709
+ * @returns 四舍五入后的数字
710
+ * @example
711
+ * ```ts
712
+ * round(3.1415) // 3
713
+ * round(3.1415, 2) // 3.14
714
+ * round(3.1415, 3) // 3.142
715
+ * round(3.9999) // 4
716
+ * ```
717
+ */
718
+ declare function round(num: number, precision?: number): number;
719
+ /**
720
+ * 将数字格式化为带千位分隔符的字符串
721
+ * @param num 要格式化的数字
722
+ * @param locale 区域设置,默认为浏览器默认区域
723
+ * @returns 格式化后的字符串
724
+ * @example
725
+ * ```ts
726
+ * formatThousands(1234567) // '1,234,567'(根据浏览器默认区域可能有所不同)
727
+ * formatThousands(1234567.89, 'en-US') // '1,234,567.89'
728
+ * formatThousands(1234567.89, 'de-DE') // '1.234.567,89'
729
+ * ```
730
+ */
731
+ declare function formatThousands(num: number, locale?: string): string;
732
+ /**
733
+ * 将数字格式化为货币字符串
734
+ * @param value 要格式化的数字
735
+ * @param options 格式化选项
736
+ * @param options.currency 货币代码(如 'CNY', 'USD'),默认为 'CNY'
737
+ * @param options.locale 地区设置(如 'zh-CN', 'en-US'),默认为 'zh-CN'
738
+ * @param options.minimumFractionDigits 最小小数位数,默认为 2
739
+ * @param options.maximumFractionDigits 最大小数位数,默认为 2
740
+ * @returns 格式化后的货币字符串
741
+ * @example
742
+ * ```ts
743
+ * formatCurrency(1234.56) // '¥1,234.56'
744
+ * formatCurrency(1234.56, { currency: 'USD', locale: 'en-US' }) // '$1,234.56'
745
+ * formatCurrency(1234.56789, { maximumFractionDigits: 4 }) // '¥1,234.5679'
746
+ * formatCurrency(1234, { minimumFractionDigits: 0 }) // '¥1,234'
747
+ * ```
748
+ */
749
+ declare function formatCurrency(value: number, options?: {
750
+ currency?: string;
751
+ locale?: string;
752
+ minimumFractionDigits?: number;
753
+ maximumFractionDigits?: number;
754
+ }): string;
755
+ /**
756
+ * 确保数字在指定范围内
757
+ * @param num 要限制范围的数字
758
+ * @param min 最小值
759
+ * @param max 最大值
760
+ * @returns 在指定范围内的数字:如果小于最小值返回最小值,如果大于最大值返回最大值
761
+ * @example
762
+ * ```ts
763
+ * clamp(5, 0, 10) // 5 - 在范围内,保持不变
764
+ * clamp(-5, 0, 10) // 0 - 小于最小值,返回最小值
765
+ * clamp(15, 0, 10) // 10 - 大于最大值,返回最大值
766
+ * ```
767
+ */
768
+ declare function clamp(num: number, min: number, max: number): number;
769
+ /**
770
+ * 生成指定范围内的随机整数(包含边界值)
771
+ * @param min 最小值(包含)
772
+ * @param max 最大值(包含)
773
+ * @returns 指定范围内的随机整数
774
+ * @example
775
+ * ```ts
776
+ * randomInt(1, 10) // 返回 1 到 10 之间的随机整数,包括 1 和 10
777
+ * randomInt(0, 1) // 返回 0 或 1
778
+ * randomInt(5, 5) // 总是返回 5
779
+ * ```
780
+ */
781
+ declare function randomInt(min: number, max: number): number;
782
+ /**
783
+ * 检查一个数字是否为偶数
784
+ * @param num 要检查的数字
785
+ * @returns 如果是偶数则返回 true,否则返回 false
786
+ * @example
787
+ * ```ts
788
+ * isEven(2) // true
789
+ * isEven(3) // false
790
+ * isEven(0) // true
791
+ * isEven(-4) // true
792
+ * ```
793
+ */
794
+ declare function isEven(num: number): boolean;
795
+ /**
796
+ * 检查一个数字是否为奇数
797
+ * @param num 要检查的数字
798
+ * @returns 如果是奇数则返回 true,否则返回 false
799
+ * @example
800
+ * ```ts
801
+ * isOdd(3) // true
802
+ * isOdd(2) // false
803
+ * isOdd(0) // false
804
+ * isOdd(-3) // true
805
+ * ```
806
+ */
807
+ declare function isOdd(num: number): boolean;
808
+ /**
809
+ * 计算百分比值
810
+ * @param value 当前值
811
+ * @param total 总值
812
+ * @param precision 结果保留的小数位数,默认为2
813
+ * @returns 百分比值,如果总值为零则返回 0
814
+ * @example
815
+ * ```ts
816
+ * percentage(25, 100) // 25
817
+ * percentage(1, 3) // 33.33
818
+ * percentage(1, 3, 0) // 33
819
+ * percentage(5, 0) // 0,避免除以零错误
820
+ * ```
821
+ */
822
+ declare function percentage(value: number, total: number, precision?: number): number;
823
+ /**
824
+ * 将数值转换为带"万"单位的字符串
825
+ * @param num 要转换的数值
826
+ * @param fractionDigits 小数位数,默认为2
827
+ * @returns 转换后的字符串,如果值大于等于10000则转为"万"单位
828
+ * @example
829
+ * ```ts
830
+ * formatNumberWithTenThousand(1234) // '1234'
831
+ * formatNumberWithTenThousand(12345) // '1.23万'
832
+ * formatNumberWithTenThousand(12345, 1) // '1.2万'
833
+ * formatNumberWithTenThousand(0) // '0'
834
+ * ```
835
+ */
836
+ declare function formatNumberWithTenThousand(num: number, fractionDigits?: number): string;
837
+
838
+ /**
839
+ * @module 对象操作
840
+ * @description 提供各种对象处理函数,包括对象深拷贝、合并、转换等实用功能
841
+ */
842
+ /**
843
+ * 检查对象是否具有指定的自有属性。
844
+ *
845
+ * @param {object} obj - 需要检查的对象
846
+ * @param {string | symbol} prop - 需要检查的属性键
847
+ * @returns {boolean} 如果对象具有该自有属性,则返回 true,否则返回 false
848
+ *
849
+ * @example
850
+ * ```ts
851
+ * const obj = { name: 'Tom', age: 25 }
852
+ * hasOwnProp(obj, 'name') // true
853
+ * hasOwnProp(obj, 'toString') // false,toString 是继承的属性
854
+ * ```
855
+ */
856
+ declare function hasOwnProp(obj: object, prop: string | symbol): boolean;
857
+ /**
858
+ * 深拷贝对象,支持基本类型、数组、对象、日期和正则表达式
859
+ * @param obj 要拷贝的对象
860
+ * @returns 深拷贝后的对象,与原对象完全独立
861
+ * @example
862
+ * ```ts
863
+ * const original = { a: 1, b: { c: 2 }, d: [1, 2, 3], e: new Date() }
864
+ * const copy = deepClone(original)
865
+ * copy.b.c = 100
866
+ * console.log(original.b.c) // 2,原对象不受影响
867
+ *
868
+ * // 支持日期对象
869
+ * const date = new Date()
870
+ * const clonedDate = deepClone(date)
871
+ * console.log(date === clonedDate) // false,不是同一引用
872
+ * ```
873
+ */
874
+ declare function deepClone<T>(obj: T): T;
875
+ /**
876
+ * 从对象中获取指定路径的值,支持点分隔的嵌套路径
877
+ * @param obj 源对象
878
+ * @param path 属性路径,如 'user.address.street'
879
+ * @param defaultValue 默认值,当路径不存在时返回
880
+ * @returns 路径对应的值,如果路径不存在则返回默认值
881
+ * @example
882
+ * ```ts
883
+ * const obj = { user: { profile: { name: 'Tom', age: 25 }, roles: ['admin'] } }
884
+ *
885
+ * get(obj, 'user.profile.name') // 'Tom'
886
+ * get(obj, 'user.roles.0') // 'admin'
887
+ * get(obj, 'user.settings', { theme: 'dark' }) // { theme: 'dark' }(路径不存在,返回默认值)
888
+ * get(obj, 'user.profile.gender') // undefined(路径不存在且没提供默认值)
889
+ * get(obj, 'user.profile.gender', 'unknown') // 'unknown'(使用默认值)
890
+ * ```
891
+ */
892
+ declare function get<T = any>(obj: Record<string, any>, path: string, defaultValue?: T): T | undefined;
893
+ /**
894
+ * 从对象中选择指定的属性,创建一个新对象
895
+ * @param obj 原始对象
896
+ * @param keys 要选择的键数组
897
+ * @returns 包含指定键的新对象
898
+ * @example
899
+ * ```ts
900
+ * const user = { id: 1, name: 'Tom', age: 25, email: 'tom@example.com' }
901
+ *
902
+ * pick(user, ['name', 'email']) // { name: 'Tom', email: 'tom@example.com' }
903
+ * pick(user, ['name', 'gender']) // { name: 'Tom' }(不存在的键会被忽略)
904
+ * pick(user, []) // {}(空数组返回空对象)
905
+ * ```
906
+ */
907
+ declare function pick<T extends Record<string, any>, K extends keyof T>(obj: T, keys: K[]): Pick<T, K>;
908
+ /**
909
+ * 从对象中排除指定的属性,创建一个新对象
910
+ * @param obj 原始对象
911
+ * @param keys 要排除的键数组
912
+ * @returns 不包含指定键的新对象
913
+ * @example
914
+ * ```ts
915
+ * const user = { id: 1, name: 'Tom', age: 25, password: '123456' }
916
+ *
917
+ * omit(user, ['password']) // { id: 1, name: 'Tom', age: 25 }
918
+ * omit(user, ['id', 'age']) // { name: 'Tom', password: '123456' }
919
+ * omit(user, ['nonExistent']) // 原对象的拷贝,不存在的键会被忽略
920
+ * ```
921
+ */
922
+ declare function omit<T extends Record<string, any>, K extends keyof T>(obj: T, keys: K[]): Omit<T, K>;
923
+ /**
924
+ * 将对象转换为 URL 查询字符串
925
+ * @param obj 要转换的对象
926
+ * @returns 格式化后的查询字符串(不包含前导?)
927
+ * @example
928
+ * ```ts
929
+ * objectToQueryString({ name: 'Tom', age: 25 }) // 'name=Tom&age=25'
930
+ * objectToQueryString({ search: 'hello world', page: 1 }) // 'search=hello%20world&page=1'
931
+ * objectToQueryString({ tags: ['js', 'ts'] }) // 'tags=js&tags=ts'
932
+ * objectToQueryString({ name: 'Tom', empty: null }) // 'name=Tom'(null 和 undefined 会被过滤)
933
+ * ```
934
+ */
935
+ declare function objectToQueryString(obj: Record<string, any>): string;
936
+ /**
937
+ * 合并多个对象,后面的对象的属性会覆盖前面的
938
+ * @param objects 要合并的对象数组
939
+ * @returns 合并后的新对象
940
+ * @example
941
+ * ```ts
942
+ * merge({ a: 1 }, { b: 2 }) // { a: 1, b: 2 }
943
+ * merge({ a: 1 }, { a: 2, b: 2 }) // { a: 2, b: 2 }(后面对象的属性会覆盖前面的)
944
+ * merge({ a: { x: 1 } }, { a: { y: 2 } }) // { a: { y: 2 } }(不是深度合并)
945
+ * merge({}, { a: 1 }, { b: 2 }, { c: 3 }) // { a: 1, b: 2, c: 3 }
946
+ * ```
947
+ */
948
+ declare function merge<T extends Record<string, any>>(...objects: T[]): T;
949
+ /**
950
+ * 深度合并多个对象,会递归合并嵌套对象
951
+ * @param objects 要合并的对象数组
952
+ * @returns 深度合并后的新对象
953
+ * @example
954
+ * ```ts
955
+ * deepMerge({ a: { x: 1 } }, { a: { y: 2 } }) // { a: { x: 1, y: 2 } }
956
+ * deepMerge({ a: 1 }, { b: 2 }) // { a: 1, b: 2 }
957
+ * deepMerge({ a: [1, 2] }, { a: [3, 4] }) // { a: [3, 4] }(数组会被替换,不会合并)
958
+ * deepMerge({}, { a: null }, { a: { b: 2 } }) // { a: { b: 2 } }(null 会被后面的对象覆盖)
959
+ * ```
960
+ */
961
+ declare function deepMerge(...objects: Record<string, any>[]): Record<string, any>;
962
+ /**
963
+ * 根据传入的键数组和键名映射对象过滤并重命名对象,返回一个新对象
964
+ * @param originalObject 要过滤的原始对象
965
+ * @param keysArray 要保留的键名数组
966
+ * @param keyMapping 可选的键名映射对象,格式为 { 原键名: 新键名 }
967
+ * @returns 返回一个新对象,其中只包含原对象中匹配的键值对,并根据映射重命名键
968
+ * @example
969
+ * ```ts
970
+ * const originalObject = { name: "John", age: 30, gender: "male", country: "USA" }
971
+ * const keysToFilter = ["name", "country"]
972
+ * const keyMapping = { name: "fullName", country: "location" }
973
+ * const result = filterObjectByKeys(originalObject, keysToFilter, keyMapping)
974
+ * // 结果: { fullName: 'John', location: 'USA' }
975
+ * ```
976
+ */
977
+ declare function filterObjectByKeys(originalObject: Record<string, any>, keysArray: string[], keyMapping?: Record<string, string>): Record<string, any>;
978
+
979
+ /**
980
+ * 延迟指定的毫秒数
981
+ *
982
+ * @param {number} ms - 需要延迟的毫秒数
983
+ * @returns {Promise<void>} 返回一个在指定时间后resolve的Promise
984
+ *
985
+ * @example
986
+ * ```ts
987
+ * // 延迟2秒
988
+ * await delay(2000)
989
+ * console.log('2秒后执行')
990
+ *
991
+ * // 在异步函数中使用
992
+ * async function fetchWithDelay() {
993
+ * await delay(1000)
994
+ * return fetch('/api/data')
995
+ * }
996
+ * ```
997
+ */
998
+ declare function delay(ms: number): Promise<void>;
999
+
1000
+ /**
1001
+ * @module 字符串处理
1002
+ * @description 提供各种字符串处理函数,包括字符串转换、格式化、验证等实用功能
1003
+ */
1004
+ /**
1005
+ * 将字符串首字母转为大写
1006
+ * @param str 输入字符串
1007
+ * @returns 首字母大写后的字符串
1008
+ * @example
1009
+ * ```ts
1010
+ * capitalize('hello') // 'Hello'
1011
+ * capitalize('') // ''
1012
+ * capitalize('a') // 'A'
1013
+ * ```
1014
+ */
1015
+ declare function capitalize(str: string): string;
1016
+ /**
1017
+ * 将驼峰命名转换为短横线命名(kebab-case)
1018
+ * @param str 驼峰命名的字符串
1019
+ * @returns 短横线命名的字符串
1020
+ * @example
1021
+ * ```ts
1022
+ * camelToKebab('helloWorld') // 'hello-world'
1023
+ * camelToKebab('HelloWorld') // 'hello-world'
1024
+ * camelToKebab('APIVersion') // 'api-version'
1025
+ * camelToKebab('iOS9App') // 'i-os9-app'
1026
+ * ```
1027
+ */
1028
+ declare function camelToKebab(str: string): string;
1029
+ /**
1030
+ * 将短横线命名(kebab-case)转换为驼峰命名(camelCase)
1031
+ * @param str 短横线命名的字符串
1032
+ * @returns 驼峰命名的字符串
1033
+ * @example
1034
+ * ```ts
1035
+ * kebabToCamel('hello-world') // 'helloWorld'
1036
+ * kebabToCamel('api-version') // 'apiVersion'
1037
+ * kebabToCamel('i-os9-app') // 'iOs9App'
1038
+ * kebabToCamel('--hello') // 'hello'
1039
+ * ```
1040
+ */
1041
+ declare function kebabToCamel(str: string): string;
1042
+ /**
1043
+ * 截取字符串并添加省略号
1044
+ * @param str 原始字符串
1045
+ * @param length 截取的最大长度,默认为 50
1046
+ * @param ellipsis 省略号字符,默认为'...'
1047
+ * @returns 截取后的字符串,如果原字符串长度小于等于截取长度,则返回原字符串
1048
+ * @example
1049
+ * ```ts
1050
+ * truncate('这是一个很长的字符串', 5) // '这是...'
1051
+ * truncate('这是一个很长的字符串', 5, '…') // '这是一…'
1052
+ * truncate('短字符', 10) // '短字符'
1053
+ * ```
1054
+ */
1055
+ declare function truncate(str: string, length?: number, ellipsis?: string): string;
1056
+ /**
1057
+ * 生成指定长度的随机字符串
1058
+ * @param length 字符串长度
1059
+ * @param chars 可选的字符集,默认包含大小写字母和数字
1060
+ * @returns 生成的随机字符串
1061
+ * @example
1062
+ * ```ts
1063
+ * randomString(5) // 例如: 'aB9cD'
1064
+ * randomString(10) // 例如: 'aBcD1eF2gH'
1065
+ * randomString(3, '123456') // 例如: '426'
1066
+ * ```
1067
+ */
1068
+ declare function randomString(length: number, chars?: string): string;
1069
+ /**
1070
+ * 将字符串中的 HTML 特殊字符转义,防止 XSS 攻击
1071
+ * @param html 包含 HTML 的字符串
1072
+ * @returns 转义后的安全字符串
1073
+ * @example
1074
+ * ```ts
1075
+ * escapeHtml('<div>Hello & World</div>') // '&lt;div&gt;Hello &amp; World&lt;/div&gt;'
1076
+ * escapeHtml('<script>alert("XSS")</script>') // '&lt;script&gt;alert(&quot;XSS&quot;)&lt;/script&gt;'
1077
+ * ```
1078
+ */
1079
+ declare function escapeHtml(html: string): string;
1080
+ /**
1081
+ * 检查字符串是否为有效的 URL
1082
+ * @param url 要检查的 URL 字符串
1083
+ * @returns 如果是有效的 URL 则返回 true,否则返回 false
1084
+ * @example
1085
+ * ```ts
1086
+ * isValidUrl('https://example.com') // true
1087
+ * isValidUrl('http://localhost:3000') // true
1088
+ * isValidUrl('example.com') // false,缺少协议
1089
+ * isValidUrl('not a url') // false
1090
+ * ```
1091
+ */
1092
+ declare function isValidUrl(url: string): boolean;
1093
+ /**
1094
+ * 检查字符串是否为有效的电子邮件地址
1095
+ * @param email 要检查的电子邮件地址
1096
+ * @returns 如果是有效的电子邮件地址则返回 true,否则返回 false
1097
+ * @example
1098
+ * ```ts
1099
+ * isValidEmail('user@example.com') // true
1100
+ * isValidEmail('user.name+tag@example.co.uk') // true
1101
+ * isValidEmail('invalid@email') // false,缺少顶级域名
1102
+ * isValidEmail('not an email') // false
1103
+ * ```
1104
+ */
1105
+ declare function isValidEmail(email: string): boolean;
1106
+ /**
1107
+ * 检查字符串是否为空或只包含空白字符
1108
+ * @param str 要检查的字符串
1109
+ * @returns 如果字符串为空或只包含空白字符,则返回 true
1110
+ * @example
1111
+ * ```ts
1112
+ * isEmptyString('') // true
1113
+ * isEmptyString(' \t\n ') // true
1114
+ * isEmptyString('hello') // false
1115
+ * isEmptyString(' hello ') // false
1116
+ * ```
1117
+ */
1118
+ declare function isEmptyString(str: string): boolean;
1119
+ /**
1120
+ * 确保值具有 rpx 单位,主要用于小程序/uni-app 样式处理
1121
+ * @param val 需要转化的值,可以是数字或字符串
1122
+ * @returns 转化后带有 rpx 单位的字符串,如果输入不是数字则原样返回
1123
+ * @example
1124
+ * ```ts
1125
+ * ensureRpxUnit(100) // '100rpx'
1126
+ * ensureRpxUnit('100') // '100rpx'
1127
+ * ensureRpxUnit('100px') // '100px',不是数字,保持原样
1128
+ * ensureRpxUnit('auto') // 'auto',不是数字,保持原样
1129
+ * ```
1130
+ */
1131
+ declare function ensureRpxUnit(val: string | number): string;
1132
+ /**
1133
+ * 替换表格数据中指定字段的 &nbsp; 为不换行空格
1134
+ *
1135
+ * @param {Array<Record<string, any>>} tableData - 表格数据数组或Vue ref
1136
+ * @param {string} key - 需要处理的字段名
1137
+ * @returns {Array<Record<string, any>>} 处理后的表格数据数组
1138
+ *
1139
+ * @example
1140
+ * ```ts
1141
+ * // 基本用法
1142
+ * const data = [
1143
+ * { name: 'John&nbsp;Doe', age: 30 },
1144
+ * { name: 'Jane&nbsp;&nbsp;Smith', age: 25 }
1145
+ * ]
1146
+ * const result = replaceNBSP(data, 'name')
1147
+ * // 结果: [
1148
+ * // { name: 'John Doe', age: 30 },
1149
+ * // { name: 'Jane Smith', age: 25 }
1150
+ * // ]
1151
+ *
1152
+ * // 使用ref包装的数据 (在Vue中使用)
1153
+ * const refData = ref([
1154
+ * { desc: 'Product&nbsp;Info', price: 99 },
1155
+ * { desc: 'Service&nbsp;&nbsp;Details', price: 50 }
1156
+ * ])
1157
+ * const result = replaceNBSP(refData, 'desc')
1158
+ * // 结果: [
1159
+ * // { desc: 'Product Info', price: 99 },
1160
+ * // { desc: 'Service Details', price: 50 }
1161
+ * // ]
1162
+ *
1163
+ * // 空数组情况
1164
+ * const emptyData = []
1165
+ * const result = replaceNBSP(emptyData, 'name')
1166
+ * // 结果: []
1167
+ * ```
1168
+ */
1169
+ declare function replaceNBSP<T extends Record<string, string | number | boolean | object>>(tableData: T[] | {
1170
+ value: T[];
1171
+ }, key: string): T[];
1172
+
1173
+ /**
1174
+ * 将对象转换为URL查询字符串
1175
+ *
1176
+ * @deprecated 请使用 objectToQueryString 函数代替,它在 object 模块中。该函数将在下一个主要版本中移除。
1177
+ * @param {Record<string, any>} params - 需要转换的对象
1178
+ * @returns {string} 转换后的查询字符串,如果有参数则以?开头
1179
+ *
1180
+ * @example
1181
+ * ```ts
1182
+ * // 基本用法
1183
+ * getQueryStringify({ name: 'John', age: 30 })
1184
+ * // 返回: ?name=John&age=30
1185
+ *
1186
+ * // 包含对象的情况
1187
+ * getQueryStringify({ user: { name: 'John', age: 30 }, active: true })
1188
+ * // 返回: ?user={"name":"John","age":30}&active=true
1189
+ *
1190
+ * // 空对象
1191
+ * getQueryStringify({})
1192
+ * // 返回: ''
1193
+ *
1194
+ * // null 或 undefined
1195
+ * getQueryStringify(null)
1196
+ * // 返回: ''
1197
+ * ```
1198
+ */
1199
+ declare function getQueryStringify(params: Record<string, any> | null | undefined): string;
1200
+
1201
+ export { add, addDays, addMonths, addYears, appendUniversalOption, camelToKebab, capitalize, chunk, clamp, createDate, deepClone, deepMerge, delay, diff, endOf, ensureRpxUnit, escapeHtml, filterObjectByKeys, first, formatCurrency, formatDate, formatHumanReadable, formatNumberWithTenThousand, formatThousands, fromNow, get, getDayOfWeek, getDaysInMonth, getQueryStringify, groupBy, hasOwnProp, isArray, isBoolean, isDate, isDateInRange, isEmpty, isEmptyObject, isEmptyString, isEqual, isEven, isFunction, isMap, isNaN, isNull, isNullOrUndefined, isNumber, isObject, isOdd, isPlainObject, isPrimitive, isPromise, isRegExp, isSet, isString, isSymbol, isUndefined, isValidEmail, isValidUrl, kebabToCamel, last, merge, now, objectToQueryString, omit, parseDate, percentage, pick, randomInt, randomString, remove, replaceNBSP, round, sample, shuffle, startOf, truncate, unique };