@pmun/utils 0.3.9 → 0.4.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.
- package/dist/index.d.ts +633 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/package.json +5 -2
package/dist/index.d.ts
CHANGED
|
@@ -300,6 +300,138 @@ declare function arrayToObject<T>(arr: T[], key: KeysMatching<T, string | number
|
|
|
300
300
|
* ```
|
|
301
301
|
*/
|
|
302
302
|
declare function arrayReplaceNBSP<T extends Record<string, string | number | boolean | object>>(arr: T[], key: string): T[];
|
|
303
|
+
/**
|
|
304
|
+
* 将嵌套数组扁平化一层
|
|
305
|
+
* @param array 要扁平化的数组
|
|
306
|
+
* @returns 扁平化后的新数组
|
|
307
|
+
* @group Array
|
|
308
|
+
* @example
|
|
309
|
+
* ```ts
|
|
310
|
+
* flatten([1, [2, 3], [4, [5, 6]]]) // [1, 2, 3, 4, [5, 6]]
|
|
311
|
+
* flatten([[1, 2], [3, 4]]) // [1, 2, 3, 4]
|
|
312
|
+
* flatten([1, 2, 3]) // [1, 2, 3](已经是扁平的)
|
|
313
|
+
* ```
|
|
314
|
+
*/
|
|
315
|
+
declare function flatten<T>(array: (T | T[])[]): T[];
|
|
316
|
+
/**
|
|
317
|
+
* 递归扁平化数组至指定深度
|
|
318
|
+
* @param array 要扁平化的数组
|
|
319
|
+
* @param depth 扁平化深度,默认为 1
|
|
320
|
+
* @returns 扁平化后的新数组
|
|
321
|
+
* @group Array
|
|
322
|
+
* @example
|
|
323
|
+
* ```ts
|
|
324
|
+
* flattenDepth([1, [2, [3, [4]]]], 1) // [1, 2, [3, [4]]]
|
|
325
|
+
* flattenDepth([1, [2, [3, [4]]]], 2) // [1, 2, 3, [4]]
|
|
326
|
+
* flattenDepth([1, [2, [3, [4]]]], 3) // [1, 2, 3, 4]
|
|
327
|
+
* ```
|
|
328
|
+
*/
|
|
329
|
+
declare function flattenDepth(array: any[], depth?: number): any[];
|
|
330
|
+
/**
|
|
331
|
+
* 递归扁平化嵌套数组至完全扁平
|
|
332
|
+
* @param array 要扁平化的数组
|
|
333
|
+
* @returns 完全扁平化后的新数组
|
|
334
|
+
* @group Array
|
|
335
|
+
* @example
|
|
336
|
+
* ```ts
|
|
337
|
+
* flattenDeep([1, [2, [3, [4]]]]) // [1, 2, 3, 4]
|
|
338
|
+
* flattenDeep([[1, 2], [3, [4, [5]]]]) // [1, 2, 3, 4, 5]
|
|
339
|
+
* flattenDeep([1, 2, 3]) // [1, 2, 3]
|
|
340
|
+
* ```
|
|
341
|
+
*/
|
|
342
|
+
declare function flattenDeep(array: any[]): any[];
|
|
343
|
+
/**
|
|
344
|
+
* 返回第一个数组中不存在于其他数组的元素
|
|
345
|
+
* @param array 主数组
|
|
346
|
+
* @param values 要排除的值数组
|
|
347
|
+
* @returns 差集数组
|
|
348
|
+
* @group Array
|
|
349
|
+
* @example
|
|
350
|
+
* ```ts
|
|
351
|
+
* difference([1, 2, 3, 4], [2, 4]) // [1, 3]
|
|
352
|
+
* difference([1, 2, 3], [4, 5]) // [1, 2, 3]
|
|
353
|
+
* difference(['a', 'b', 'c'], ['b', 'd']) // ['a', 'c']
|
|
354
|
+
* ```
|
|
355
|
+
*/
|
|
356
|
+
declare function difference<T>(array: T[], ...values: T[][]): T[];
|
|
357
|
+
/**
|
|
358
|
+
* 返回所有数组的交集(所有数组中都存在的元素)
|
|
359
|
+
* @param arrays 要比较的数组
|
|
360
|
+
* @returns 交集数组
|
|
361
|
+
* @group Array
|
|
362
|
+
* @example
|
|
363
|
+
* ```ts
|
|
364
|
+
* intersection([1, 2, 3], [2, 3, 4], [2, 3, 5]) // [2, 3]
|
|
365
|
+
* intersection([1, 2], [2, 3], [3, 4]) // [](没有共同元素)
|
|
366
|
+
* intersection(['a', 'b'], ['b', 'c'], ['b', 'd']) // ['b']
|
|
367
|
+
* ```
|
|
368
|
+
*/
|
|
369
|
+
declare function intersection<T>(...arrays: T[][]): T[];
|
|
370
|
+
/**
|
|
371
|
+
* 返回所有数组的并集(去重后的所有元素)
|
|
372
|
+
* @param arrays 要合并的数组
|
|
373
|
+
* @returns 并集数组
|
|
374
|
+
* @group Array
|
|
375
|
+
* @example
|
|
376
|
+
* ```ts
|
|
377
|
+
* union([1, 2], [2, 3], [3, 4]) // [1, 2, 3, 4]
|
|
378
|
+
* union([1, 1, 2], [2, 3, 3]) // [1, 2, 3]
|
|
379
|
+
* union(['a', 'b'], ['b', 'c']) // ['a', 'b', 'c']
|
|
380
|
+
* ```
|
|
381
|
+
*/
|
|
382
|
+
declare function union<T>(...arrays: T[][]): T[];
|
|
383
|
+
/**
|
|
384
|
+
* 根据迭代函数返回的值进行数组去重
|
|
385
|
+
* @param array 原始数组
|
|
386
|
+
* @param iteratee 迭代函数,返回用于比较的值
|
|
387
|
+
* @returns 去重后的新数组
|
|
388
|
+
* @group Array
|
|
389
|
+
* @example
|
|
390
|
+
* ```ts
|
|
391
|
+
* const users = [
|
|
392
|
+
* { id: 1, name: 'Tom' },
|
|
393
|
+
* { id: 2, name: 'Jerry' },
|
|
394
|
+
* { id: 1, name: 'Tom2' }
|
|
395
|
+
* ]
|
|
396
|
+
* uniqBy(users, user => user.id)
|
|
397
|
+
* // [{ id: 1, name: 'Tom' }, { id: 2, name: 'Jerry' }]
|
|
398
|
+
*
|
|
399
|
+
* uniqBy([2.1, 1.2, 2.3], Math.floor) // [2.1, 1.2]
|
|
400
|
+
* ```
|
|
401
|
+
*/
|
|
402
|
+
declare function uniqBy<T, K>(array: T[], iteratee: (item: T) => K): T[];
|
|
403
|
+
/**
|
|
404
|
+
* 根据迭代函数返回的值进行排序
|
|
405
|
+
* @param array 要排序的数组
|
|
406
|
+
* @param iteratee 迭代函数或属性名,返回用于排序的值
|
|
407
|
+
* @param order 排序方向,'asc' 为升序,'desc' 为降序,默认为 'asc'
|
|
408
|
+
* @returns 排序后的新数组,不会修改原数组
|
|
409
|
+
* @group Array
|
|
410
|
+
* @example
|
|
411
|
+
* ```ts
|
|
412
|
+
* const users = [
|
|
413
|
+
* { name: 'Tom', age: 25 },
|
|
414
|
+
* { name: 'Jerry', age: 20 },
|
|
415
|
+
* { name: 'Alice', age: 30 }
|
|
416
|
+
* ]
|
|
417
|
+
*
|
|
418
|
+
* // 使用函数
|
|
419
|
+
* sortBy(users, user => user.age)
|
|
420
|
+
* // [{ name: 'Jerry', age: 20 }, { name: 'Tom', age: 25 }, { name: 'Alice', age: 30 }]
|
|
421
|
+
*
|
|
422
|
+
* // 降序
|
|
423
|
+
* sortBy(users, user => user.age, 'desc')
|
|
424
|
+
* // [{ name: 'Alice', age: 30 }, { name: 'Tom', age: 25 }, { name: 'Jerry', age: 20 }]
|
|
425
|
+
*
|
|
426
|
+
* // 使用属性名
|
|
427
|
+
* sortBy(users, 'name')
|
|
428
|
+
* // [{ name: 'Alice', age: 30 }, { name: 'Jerry', age: 20 }, { name: 'Tom', age: 25 }]
|
|
429
|
+
*
|
|
430
|
+
* // 数字数组排序
|
|
431
|
+
* sortBy([3, 1, 4, 1, 5], x => x) // [1, 1, 3, 4, 5]
|
|
432
|
+
* ```
|
|
433
|
+
*/
|
|
434
|
+
declare function sortBy<T>(array: T[], iteratee: ((item: T) => any) | keyof T, order?: 'asc' | 'desc'): T[];
|
|
303
435
|
|
|
304
436
|
/**
|
|
305
437
|
* 表示日期的各种类型,可以是日期对象、日期字符串或时间戳
|
|
@@ -632,6 +764,247 @@ declare function formatChatTime(date: DateLike): string;
|
|
|
632
764
|
* ```
|
|
633
765
|
*/
|
|
634
766
|
declare function formatDuration(timestamp: number, format?: string): string | number;
|
|
767
|
+
/**
|
|
768
|
+
* 日期范围元组类型,包含开始和结束时间戳(毫秒)
|
|
769
|
+
*/
|
|
770
|
+
type DateRange = [number, number];
|
|
771
|
+
/**
|
|
772
|
+
* 日期快捷键函数类型
|
|
773
|
+
*/
|
|
774
|
+
type DateShortcutFn = () => DateRange;
|
|
775
|
+
/**
|
|
776
|
+
* 预设快捷键名称
|
|
777
|
+
*/
|
|
778
|
+
type PresetShortcutKey = '今天' | '本周' | '本月' | '本季度' | '本半年' | '本年' | '最近7天' | '最近30天' | '最近3个月' | '最近半年' | '最近一年';
|
|
779
|
+
/**
|
|
780
|
+
* 日期快捷键配置对象类型
|
|
781
|
+
* 支持预设快捷键名称和自定义字符串键
|
|
782
|
+
*/
|
|
783
|
+
type DateShortcutsConfig = {
|
|
784
|
+
[K in PresetShortcutKey]?: DateShortcutFn;
|
|
785
|
+
} & {
|
|
786
|
+
[key: string]: DateShortcutFn | undefined;
|
|
787
|
+
};
|
|
788
|
+
/**
|
|
789
|
+
* 日期快捷键配置项类型,可以是预设名称字符串或自定义配置对象
|
|
790
|
+
* 支持预设快捷键名称(带自动补全)和任意自定义字符串
|
|
791
|
+
*/
|
|
792
|
+
type DateShortcutItem = PresetShortcutKey | (string & {}) | Record<string, PresetShortcutKey | (string & {}) | DateShortcutFn>;
|
|
793
|
+
/**
|
|
794
|
+
* 获取指定偏移天数的日期
|
|
795
|
+
* @param offset 偏移天数(负数表示过去,正数表示未来),默认为 0
|
|
796
|
+
* @returns 日期对象
|
|
797
|
+
* @group DateRange
|
|
798
|
+
* @example
|
|
799
|
+
* ```ts
|
|
800
|
+
* getDateByOffset() // 今天
|
|
801
|
+
* getDateByOffset(-1) // 昨天
|
|
802
|
+
* getDateByOffset(1) // 明天
|
|
803
|
+
* getDateByOffset(-7) // 7天前
|
|
804
|
+
* ```
|
|
805
|
+
*/
|
|
806
|
+
declare function getDateByOffset(offset?: number): Date;
|
|
807
|
+
/**
|
|
808
|
+
* 获取昨天的日期字符串 (YYYY-MM-DD)
|
|
809
|
+
* @returns 昨天的日期字符串
|
|
810
|
+
* @group DateRange
|
|
811
|
+
* @example
|
|
812
|
+
* ```ts
|
|
813
|
+
* getYesterdayStr() // "2023-05-14"(假设今天是 2023-05-15)
|
|
814
|
+
* ```
|
|
815
|
+
*/
|
|
816
|
+
declare function getYesterdayStr(): string;
|
|
817
|
+
/**
|
|
818
|
+
* 获取今天的日期字符串 (YYYY-MM-DD)
|
|
819
|
+
* @returns 今天的日期字符串
|
|
820
|
+
* @group DateRange
|
|
821
|
+
* @example
|
|
822
|
+
* ```ts
|
|
823
|
+
* getTodayStr() // "2023-05-15"
|
|
824
|
+
* ```
|
|
825
|
+
*/
|
|
826
|
+
declare function getTodayStr(): string;
|
|
827
|
+
/**
|
|
828
|
+
* 获取今天的开始和结束时间戳
|
|
829
|
+
* @returns 日期范围元组 [开始时间戳, 结束时间戳]
|
|
830
|
+
* @group DateRange
|
|
831
|
+
* @example
|
|
832
|
+
* ```ts
|
|
833
|
+
* const [start, end] = getTodayRange()
|
|
834
|
+
* // start: 今天 00:00:00 的时间戳
|
|
835
|
+
* // end: 今天 23:59:59.999 的时间戳
|
|
836
|
+
* ```
|
|
837
|
+
*/
|
|
838
|
+
declare function getTodayRange(): DateRange;
|
|
839
|
+
/**
|
|
840
|
+
* 获取本周的开始和结束时间戳(周一到周日)
|
|
841
|
+
* @returns 日期范围元组 [开始时间戳, 结束时间戳]
|
|
842
|
+
* @group DateRange
|
|
843
|
+
* @example
|
|
844
|
+
* ```ts
|
|
845
|
+
* const [start, end] = getThisWeekRange()
|
|
846
|
+
* // start: 本周一 00:00:00 的时间戳
|
|
847
|
+
* // end: 本周日 23:59:59.999 的时间戳
|
|
848
|
+
* ```
|
|
849
|
+
*/
|
|
850
|
+
declare function getThisWeekRange(): DateRange;
|
|
851
|
+
/**
|
|
852
|
+
* 获取本月的开始和结束时间戳
|
|
853
|
+
* @returns 日期范围元组 [开始时间戳, 结束时间戳]
|
|
854
|
+
* @group DateRange
|
|
855
|
+
* @example
|
|
856
|
+
* ```ts
|
|
857
|
+
* const [start, end] = getThisMonthRange()
|
|
858
|
+
* // start: 本月1日 00:00:00 的时间戳
|
|
859
|
+
* // end: 本月最后一天 23:59:59.999 的时间戳
|
|
860
|
+
* ```
|
|
861
|
+
*/
|
|
862
|
+
declare function getThisMonthRange(): DateRange;
|
|
863
|
+
/**
|
|
864
|
+
* 获取本季度的开始和结束时间戳
|
|
865
|
+
* @returns 日期范围元组 [开始时间戳, 结束时间戳]
|
|
866
|
+
* @group DateRange
|
|
867
|
+
* @example
|
|
868
|
+
* ```ts
|
|
869
|
+
* const [start, end] = getThisQuarterRange()
|
|
870
|
+
* // 如果当前是5月(Q2),则:
|
|
871
|
+
* // start: 4月1日 00:00:00 的时间戳
|
|
872
|
+
* // end: 6月30日 23:59:59.999 的时间戳
|
|
873
|
+
* ```
|
|
874
|
+
*/
|
|
875
|
+
declare function getThisQuarterRange(): DateRange;
|
|
876
|
+
/**
|
|
877
|
+
* 获取本半年的开始和结束时间戳
|
|
878
|
+
* @returns 日期范围元组 [开始时间戳, 结束时间戳]
|
|
879
|
+
* @group DateRange
|
|
880
|
+
* @example
|
|
881
|
+
* ```ts
|
|
882
|
+
* const [start, end] = getThisHalfYearRange()
|
|
883
|
+
* // 如果当前是5月(上半年),则:
|
|
884
|
+
* // start: 1月1日 00:00:00 的时间戳
|
|
885
|
+
* // end: 6月30日 23:59:59.999 的时间戳
|
|
886
|
+
* ```
|
|
887
|
+
*/
|
|
888
|
+
declare function getThisHalfYearRange(): DateRange;
|
|
889
|
+
/**
|
|
890
|
+
* 获取本年的开始和结束时间戳
|
|
891
|
+
* @returns 日期范围元组 [开始时间戳, 结束时间戳]
|
|
892
|
+
* @group DateRange
|
|
893
|
+
* @example
|
|
894
|
+
* ```ts
|
|
895
|
+
* const [start, end] = getThisYearRange()
|
|
896
|
+
* // start: 1月1日 00:00:00 的时间戳
|
|
897
|
+
* // end: 12月31日 23:59:59.999 的时间戳
|
|
898
|
+
* ```
|
|
899
|
+
*/
|
|
900
|
+
declare function getThisYearRange(): DateRange;
|
|
901
|
+
/**
|
|
902
|
+
* 获取最近 N 天的时间范围
|
|
903
|
+
* @param days 天数
|
|
904
|
+
* @returns 日期范围元组 [开始时间戳, 结束时间戳]
|
|
905
|
+
* @group DateRange
|
|
906
|
+
* @example
|
|
907
|
+
* ```ts
|
|
908
|
+
* const [start, end] = getLastDaysRange(7)
|
|
909
|
+
* // start: 7天前 00:00:00 的时间戳
|
|
910
|
+
* // end: 当前时间戳
|
|
911
|
+
*
|
|
912
|
+
* const [start, end] = getLastDaysRange(30)
|
|
913
|
+
* // start: 30天前 00:00:00 的时间戳
|
|
914
|
+
* // end: 当前时间戳
|
|
915
|
+
* ```
|
|
916
|
+
*/
|
|
917
|
+
declare function getLastDaysRange(days: number): DateRange;
|
|
918
|
+
/**
|
|
919
|
+
* 获取最近 N 周的时间范围
|
|
920
|
+
* @param weeks 周数
|
|
921
|
+
* @returns 日期范围元组 [开始时间戳, 结束时间戳]
|
|
922
|
+
* @group DateRange
|
|
923
|
+
* @example
|
|
924
|
+
* ```ts
|
|
925
|
+
* const [start, end] = getLastWeeksRange(2)
|
|
926
|
+
* // start: 2周前的时间戳
|
|
927
|
+
* // end: 当前时间戳
|
|
928
|
+
* ```
|
|
929
|
+
*/
|
|
930
|
+
declare function getLastWeeksRange(weeks: number): DateRange;
|
|
931
|
+
/**
|
|
932
|
+
* 获取最近 N 个月的时间范围
|
|
933
|
+
* @param months 月数
|
|
934
|
+
* @returns 日期范围元组 [开始时间戳, 结束时间戳]
|
|
935
|
+
* @group DateRange
|
|
936
|
+
* @example
|
|
937
|
+
* ```ts
|
|
938
|
+
* const [start, end] = getLastMonthsRange(3)
|
|
939
|
+
* // start: 3个月前当天 00:00:00 的时间戳
|
|
940
|
+
* // end: 当前时间戳
|
|
941
|
+
*
|
|
942
|
+
* const [start, end] = getLastMonthsRange(12)
|
|
943
|
+
* // start: 12个月前当天 00:00:00 的时间戳
|
|
944
|
+
* // end: 当前时间戳
|
|
945
|
+
* ```
|
|
946
|
+
*/
|
|
947
|
+
declare function getLastMonthsRange(months: number): DateRange;
|
|
948
|
+
/**
|
|
949
|
+
* 常用日期范围快捷键配置
|
|
950
|
+
*
|
|
951
|
+
* 可直接用于 DateRangePicker 组件的 shortcuts 属性
|
|
952
|
+
*
|
|
953
|
+
* @group DateRange
|
|
954
|
+
* @example
|
|
955
|
+
* ```ts
|
|
956
|
+
* // 在 Element Plus 中使用
|
|
957
|
+
* <el-date-picker
|
|
958
|
+
* type="daterange"
|
|
959
|
+
* :shortcuts="Object.entries(commonDateShortcuts).map(([text, value]) => ({ text, value }))"
|
|
960
|
+
* />
|
|
961
|
+
*
|
|
962
|
+
* // 获取今天的范围
|
|
963
|
+
* const [start, end] = commonDateShortcuts['今天']()
|
|
964
|
+
*
|
|
965
|
+
* // 获取本月的范围
|
|
966
|
+
* const [start, end] = commonDateShortcuts['本月']()
|
|
967
|
+
* ```
|
|
968
|
+
*/
|
|
969
|
+
declare const commonDateShortcuts: DateShortcutsConfig;
|
|
970
|
+
/**
|
|
971
|
+
* 创建日期范围快捷键配置
|
|
972
|
+
*
|
|
973
|
+
* 支持多种输入格式,简化快捷键配置。可以从预设的 commonDateShortcuts 中选择,
|
|
974
|
+
* 也可以自定义快捷键名称和函数。
|
|
975
|
+
*
|
|
976
|
+
* @param config 快捷键配置,支持以下格式:
|
|
977
|
+
* - 字符串数组:从 commonDateShortcuts 中选择预设快捷键
|
|
978
|
+
* - 对象数组:自定义键名映射到预设或自定义函数
|
|
979
|
+
* - 对象:直接传入快捷键配置对象
|
|
980
|
+
* @returns 快捷键配置对象
|
|
981
|
+
* @group DateRange
|
|
982
|
+
* @example
|
|
983
|
+
* ```ts
|
|
984
|
+
* // 方式1: 传入字符串数组(从 commonDateShortcuts 中选择)
|
|
985
|
+
* const shortcuts = createDateShortcuts(['今天', '本周', '本月'])
|
|
986
|
+
* // => { 今天: getTodayRange, 本周: getThisWeekRange, 本月: getThisMonthRange }
|
|
987
|
+
*
|
|
988
|
+
* // 方式2: 传入对象数组(支持自定义键名)
|
|
989
|
+
* const shortcuts = createDateShortcuts([
|
|
990
|
+
* { 今天: '今天' },
|
|
991
|
+
* { 这周: '本周' }, // 显示"这周",实际使用"本周"的逻辑
|
|
992
|
+
* { 自定义: () => [Date.now() - 86400000, Date.now()] }
|
|
993
|
+
* ])
|
|
994
|
+
*
|
|
995
|
+
* // 方式3: 直接传入对象(兼容旧方式)
|
|
996
|
+
* const shortcuts = createDateShortcuts({
|
|
997
|
+
* 今天: commonDateShortcuts['今天'],
|
|
998
|
+
* 自定义: () => [Date.now(), Date.now()]
|
|
999
|
+
* })
|
|
1000
|
+
*
|
|
1001
|
+
* // 在 Vue 组件中使用
|
|
1002
|
+
* const shortcuts = createDateShortcuts(['今天', '本周', '本月', '最近7天'])
|
|
1003
|
+
* // 转换为 Element Plus 格式
|
|
1004
|
+
* const elShortcuts = Object.entries(shortcuts).map(([text, value]) => ({ text, value }))
|
|
1005
|
+
* ```
|
|
1006
|
+
*/
|
|
1007
|
+
declare function createDateShortcuts(config: DateShortcutItem[] | DateShortcutsConfig): DateShortcutsConfig;
|
|
635
1008
|
|
|
636
1009
|
/**
|
|
637
1010
|
* @module 类型检查
|
|
@@ -1276,6 +1649,60 @@ declare function deepMerge(...objects: Record<string, any>[]): Record<string, an
|
|
|
1276
1649
|
* ```
|
|
1277
1650
|
*/
|
|
1278
1651
|
declare function filterObjectByKeys(originalObject: Record<string, any>, keysArray: string[], keyMapping?: Record<string, string>): Record<string, any>;
|
|
1652
|
+
/**
|
|
1653
|
+
* 创建一个新对象,通过函数转换每个键
|
|
1654
|
+
* @param obj 原始对象
|
|
1655
|
+
* @param iteratee 转换函数,接收值、键和对象,返回新的键名
|
|
1656
|
+
* @returns 转换后的新对象
|
|
1657
|
+
* @group Object
|
|
1658
|
+
* @example
|
|
1659
|
+
* ```ts
|
|
1660
|
+
* const obj = { a: 1, b: 2, c: 3 }
|
|
1661
|
+
* mapKeys(obj, (value, key) => key.toUpperCase())
|
|
1662
|
+
* // { A: 1, B: 2, C: 3 }
|
|
1663
|
+
*
|
|
1664
|
+
* mapKeys(obj, (value, key) => `prefix_${key}`)
|
|
1665
|
+
* // { prefix_a: 1, prefix_b: 2, prefix_c: 3 }
|
|
1666
|
+
* ```
|
|
1667
|
+
*/
|
|
1668
|
+
declare function mapKeys<T extends Record<string, any>>(obj: T, iteratee: (value: T[keyof T], key: string, obj: T) => string): Record<string, T[keyof T]>;
|
|
1669
|
+
/**
|
|
1670
|
+
* 创建一个新对象,通过函数转换每个值
|
|
1671
|
+
* @param obj 原始对象
|
|
1672
|
+
* @param iteratee 转换函数,接收值、键和对象,返回新的值
|
|
1673
|
+
* @returns 转换后的新对象
|
|
1674
|
+
* @group Object
|
|
1675
|
+
* @example
|
|
1676
|
+
* ```ts
|
|
1677
|
+
* const obj = { a: 1, b: 2, c: 3 }
|
|
1678
|
+
* mapValues(obj, value => value * 2)
|
|
1679
|
+
* // { a: 2, b: 4, c: 6 }
|
|
1680
|
+
*
|
|
1681
|
+
* const users = { user1: { age: 20 }, user2: { age: 30 } }
|
|
1682
|
+
* mapValues(users, user => user.age)
|
|
1683
|
+
* // { user1: 20, user2: 30 }
|
|
1684
|
+
* ```
|
|
1685
|
+
*/
|
|
1686
|
+
declare function mapValues<T extends Record<string, any>, R>(obj: T, iteratee: (value: T[keyof T], key: string, obj: T) => R): Record<keyof T, R>;
|
|
1687
|
+
/**
|
|
1688
|
+
* 反转对象的键值对,值变成键,键变成值
|
|
1689
|
+
* @param obj 原始对象
|
|
1690
|
+
* @returns 反转后的新对象
|
|
1691
|
+
* @group Object
|
|
1692
|
+
* @example
|
|
1693
|
+
* ```ts
|
|
1694
|
+
* invert({ a: '1', b: '2', c: '3' })
|
|
1695
|
+
* // { '1': 'a', '2': 'b', '3': 'c' }
|
|
1696
|
+
*
|
|
1697
|
+
* invert({ firstName: 'John', lastName: 'Doe' })
|
|
1698
|
+
* // { John: 'firstName', Doe: 'lastName' }
|
|
1699
|
+
*
|
|
1700
|
+
* // 注意:如果有重复的值,后面的键会覆盖前面的
|
|
1701
|
+
* invert({ a: '1', b: '1' })
|
|
1702
|
+
* // { '1': 'b' }
|
|
1703
|
+
* ```
|
|
1704
|
+
*/
|
|
1705
|
+
declare function invert<T extends Record<string, string | number>>(obj: T): Record<string, string>;
|
|
1279
1706
|
|
|
1280
1707
|
/**
|
|
1281
1708
|
* 延迟指定的毫秒数
|
|
@@ -1297,6 +1724,145 @@ declare function filterObjectByKeys(originalObject: Record<string, any>, keysArr
|
|
|
1297
1724
|
* ```
|
|
1298
1725
|
*/
|
|
1299
1726
|
declare function delay(ms: number): Promise<void>;
|
|
1727
|
+
/**
|
|
1728
|
+
* 带超时控制的 Promise
|
|
1729
|
+
*
|
|
1730
|
+
* 如果 Promise 在指定时间内没有完成,则拒绝并抛出超时错误。
|
|
1731
|
+
*
|
|
1732
|
+
* @param {Promise<T>} promise - 要执行的 Promise
|
|
1733
|
+
* @param {number} ms - 超时时间(毫秒)
|
|
1734
|
+
* @param {string} [message] - 超时错误信息,默认为 "Operation timed out"
|
|
1735
|
+
* @returns {Promise<T>} 返回原 Promise 的结果,或在超时时抛出错误
|
|
1736
|
+
* @group Promise
|
|
1737
|
+
* @example
|
|
1738
|
+
* ```ts
|
|
1739
|
+
* // 正常完成
|
|
1740
|
+
* await timeout(fetch('/api/data'), 5000)
|
|
1741
|
+
*
|
|
1742
|
+
* // 超时
|
|
1743
|
+
* try {
|
|
1744
|
+
* await timeout(delay(3000), 1000)
|
|
1745
|
+
* } catch (error) {
|
|
1746
|
+
* console.error('超时了')
|
|
1747
|
+
* }
|
|
1748
|
+
*
|
|
1749
|
+
* // 自定义错误信息
|
|
1750
|
+
* await timeout(fetch('/api/data'), 5000, '请求超时')
|
|
1751
|
+
* ```
|
|
1752
|
+
*/
|
|
1753
|
+
declare function timeout<T>(promise: Promise<T>, ms: number, message?: string): Promise<T>;
|
|
1754
|
+
/**
|
|
1755
|
+
* 重试失败的 Promise
|
|
1756
|
+
*
|
|
1757
|
+
* 当 Promise 失败时,会自动重试指定次数,每次重试之间可以设置延迟时间。
|
|
1758
|
+
*
|
|
1759
|
+
* @param {() => Promise<T>} fn - 返回 Promise 的函数
|
|
1760
|
+
* @param {number} retries - 最大重试次数,默认为 3
|
|
1761
|
+
* @param {number} delayMs - 每次重试之间的延迟时间(毫秒),默认为 1000
|
|
1762
|
+
* @returns {Promise<T>} 返回成功的结果,或在所有重试失败后抛出最后一次的错误
|
|
1763
|
+
* @group Promise
|
|
1764
|
+
* @example
|
|
1765
|
+
* ```ts
|
|
1766
|
+
* // 重试失败的请求
|
|
1767
|
+
* const data = await retry(() => fetch('/api/data').then(r => r.json()), 3, 1000)
|
|
1768
|
+
*
|
|
1769
|
+
* // 自定义重试次数和延迟
|
|
1770
|
+
* await retry(
|
|
1771
|
+
* () => riskyOperation(),
|
|
1772
|
+
* 5, // 重试5次
|
|
1773
|
+
* 2000 // 每次间隔2秒
|
|
1774
|
+
* )
|
|
1775
|
+
* ```
|
|
1776
|
+
*/
|
|
1777
|
+
declare function retry<T>(fn: () => Promise<T>, retries?: number, delayMs?: number): Promise<T>;
|
|
1778
|
+
/**
|
|
1779
|
+
* 创建一个防抖函数(异步版本)
|
|
1780
|
+
*
|
|
1781
|
+
* 在指定的延迟时间内,如果函数被多次调用,只执行最后一次。
|
|
1782
|
+
* 返回一个 Promise,会在函数实际执行后 resolve。
|
|
1783
|
+
*
|
|
1784
|
+
* @param {(...args: T) => Promise<R> | R} fn - 要防抖的函数
|
|
1785
|
+
* @param {number} wait - 延迟时间(毫秒)
|
|
1786
|
+
* @returns 防抖后的函数
|
|
1787
|
+
* @group Promise
|
|
1788
|
+
* @example
|
|
1789
|
+
* ```ts
|
|
1790
|
+
* const searchAPI = debounce(async (query: string) => {
|
|
1791
|
+
* const response = await fetch(`/api/search?q=${query}`)
|
|
1792
|
+
* return response.json()
|
|
1793
|
+
* }, 300)
|
|
1794
|
+
*
|
|
1795
|
+
* // 只有最后一次调用会实际执行
|
|
1796
|
+
* searchAPI('a')
|
|
1797
|
+
* searchAPI('ab')
|
|
1798
|
+
* const result = await searchAPI('abc') // 只有这次会执行
|
|
1799
|
+
* ```
|
|
1800
|
+
*/
|
|
1801
|
+
declare function debounce<T extends any[], R>(fn: (...args: T) => Promise<R> | R, wait: number): (...args: T) => Promise<R>;
|
|
1802
|
+
/**
|
|
1803
|
+
* 创建一个节流函数(异步版本)
|
|
1804
|
+
*
|
|
1805
|
+
* 在指定的时间间隔内,函数最多只执行一次。
|
|
1806
|
+
* 如果在等待期间有新的调用,会在等待结束后立即执行最新的调用。
|
|
1807
|
+
*
|
|
1808
|
+
* @param {(...args: T) => Promise<R> | R} fn - 要节流的函数
|
|
1809
|
+
* @param {number} wait - 时间间隔(毫秒)
|
|
1810
|
+
* @returns 节流后的函数
|
|
1811
|
+
* @group Promise
|
|
1812
|
+
* @example
|
|
1813
|
+
* ```ts
|
|
1814
|
+
* const saveData = throttle(async (data: any) => {
|
|
1815
|
+
* await fetch('/api/save', {
|
|
1816
|
+
* method: 'POST',
|
|
1817
|
+
* body: JSON.stringify(data)
|
|
1818
|
+
* })
|
|
1819
|
+
* }, 1000)
|
|
1820
|
+
*
|
|
1821
|
+
* // 1秒内多次调用,只执行一次
|
|
1822
|
+
* saveData({ value: 1 })
|
|
1823
|
+
* saveData({ value: 2 })
|
|
1824
|
+
* saveData({ value: 3 }) // 只有第一次和最后一次会执行
|
|
1825
|
+
* ```
|
|
1826
|
+
*/
|
|
1827
|
+
declare function throttle<T extends any[], R>(fn: (...args: T) => Promise<R> | R, wait: number): (...args: T) => Promise<R> | undefined;
|
|
1828
|
+
/**
|
|
1829
|
+
* 并发执行多个 Promise,并限制同时执行的数量
|
|
1830
|
+
*
|
|
1831
|
+
* @param {(() => Promise<T>)[]} tasks - Promise 工厂函数数组
|
|
1832
|
+
* @param {number} limit - 最大并发数,默认为 5
|
|
1833
|
+
* @returns {Promise<T[]>} 返回所有 Promise 的结果数组
|
|
1834
|
+
* @group Promise
|
|
1835
|
+
* @example
|
|
1836
|
+
* ```ts
|
|
1837
|
+
* const urls = ['/api/1', '/api/2', '/api/3', '/api/4', '/api/5']
|
|
1838
|
+
* const tasks = urls.map(url => () => fetch(url).then(r => r.json()))
|
|
1839
|
+
*
|
|
1840
|
+
* // 最多同时执行 2 个请求
|
|
1841
|
+
* const results = await parallel(tasks, 2)
|
|
1842
|
+
* console.log(results)
|
|
1843
|
+
* ```
|
|
1844
|
+
*/
|
|
1845
|
+
declare function parallel<T>(tasks: (() => Promise<T>)[], limit?: number): Promise<T[]>;
|
|
1846
|
+
/**
|
|
1847
|
+
* 顺序执行多个 Promise(一个接一个)
|
|
1848
|
+
*
|
|
1849
|
+
* @param {(() => Promise<T>)[]} tasks - Promise 工厂函数数组
|
|
1850
|
+
* @returns {Promise<T[]>} 返回所有 Promise 的结果数组
|
|
1851
|
+
* @group Promise
|
|
1852
|
+
* @example
|
|
1853
|
+
* ```ts
|
|
1854
|
+
* const tasks = [
|
|
1855
|
+
* () => fetch('/api/1').then(r => r.json()),
|
|
1856
|
+
* () => fetch('/api/2').then(r => r.json()),
|
|
1857
|
+
* () => fetch('/api/3').then(r => r.json()),
|
|
1858
|
+
* ]
|
|
1859
|
+
*
|
|
1860
|
+
* // 按顺序执行,每个完成后才执行下一个
|
|
1861
|
+
* const results = await sequential(tasks)
|
|
1862
|
+
* console.log(results)
|
|
1863
|
+
* ```
|
|
1864
|
+
*/
|
|
1865
|
+
declare function sequential<T>(tasks: (() => Promise<T>)[]): Promise<T[]>;
|
|
1300
1866
|
|
|
1301
1867
|
/**
|
|
1302
1868
|
* @module 字符串处理
|
|
@@ -1497,6 +2063,72 @@ declare function replaceNBSP(str?: string): string;
|
|
|
1497
2063
|
* ```
|
|
1498
2064
|
*/
|
|
1499
2065
|
declare function parseJsonStr<T = any>(str?: string | null, defaultValue?: T): T | string | Record<string, any>;
|
|
2066
|
+
/**
|
|
2067
|
+
* 将字符串分割成单词数组
|
|
2068
|
+
* @param str 要分割的字符串
|
|
2069
|
+
* @returns 单词数组
|
|
2070
|
+
* @group String
|
|
2071
|
+
* @example
|
|
2072
|
+
* ```ts
|
|
2073
|
+
* words('hello world') // ['hello', 'world']
|
|
2074
|
+
* words('helloWorld') // ['hello', 'World']
|
|
2075
|
+
* words('hello-world-foo') // ['hello', 'world', 'foo']
|
|
2076
|
+
* words('hello_world_foo') // ['hello', 'world', 'foo']
|
|
2077
|
+
* words('HelloWorldFoo') // ['Hello', 'World', 'Foo']
|
|
2078
|
+
* words('hello123world') // ['hello', '123', 'world']
|
|
2079
|
+
* ```
|
|
2080
|
+
*/
|
|
2081
|
+
declare function words(str: string): string[];
|
|
2082
|
+
/**
|
|
2083
|
+
* 将字符串转换为蛇形命名(snake_case)
|
|
2084
|
+
* @param str 输入字符串
|
|
2085
|
+
* @returns 蛇形命名的字符串
|
|
2086
|
+
* @group String
|
|
2087
|
+
* @example
|
|
2088
|
+
* ```ts
|
|
2089
|
+
* snakeCase('helloWorld') // 'hello_world'
|
|
2090
|
+
* snakeCase('HelloWorld') // 'hello_world'
|
|
2091
|
+
* snakeCase('hello-world') // 'hello_world'
|
|
2092
|
+
* snakeCase('hello world') // 'hello_world'
|
|
2093
|
+
* snakeCase('HelloWorldFoo') // 'hello_world_foo'
|
|
2094
|
+
* ```
|
|
2095
|
+
*/
|
|
2096
|
+
declare function snakeCase(str: string): string;
|
|
2097
|
+
/**
|
|
2098
|
+
* 将字符串转换为帕斯卡命名(PascalCase)
|
|
2099
|
+
* @param str 输入字符串
|
|
2100
|
+
* @returns 帕斯卡命名的字符串
|
|
2101
|
+
* @group String
|
|
2102
|
+
* @example
|
|
2103
|
+
* ```ts
|
|
2104
|
+
* pascalCase('hello world') // 'HelloWorld'
|
|
2105
|
+
* pascalCase('hello-world') // 'HelloWorld'
|
|
2106
|
+
* pascalCase('hello_world') // 'HelloWorld'
|
|
2107
|
+
* pascalCase('helloWorld') // 'HelloWorld'
|
|
2108
|
+
* pascalCase('HELLO_WORLD') // 'HelloWorld'
|
|
2109
|
+
* ```
|
|
2110
|
+
*/
|
|
2111
|
+
declare function pascalCase(str: string): string;
|
|
2112
|
+
/**
|
|
2113
|
+
* 简单的模板字符串替换
|
|
2114
|
+
* @param template 模板字符串,使用 {{key}} 或 {key} 标记占位符
|
|
2115
|
+
* @param data 数据对象
|
|
2116
|
+
* @returns 替换后的字符串
|
|
2117
|
+
* @group String
|
|
2118
|
+
* @example
|
|
2119
|
+
* ```ts
|
|
2120
|
+
* template('Hello {{name}}!', { name: 'Tom' }) // 'Hello Tom!'
|
|
2121
|
+
* template('Hello {name}, you are {age} years old', { name: 'Tom', age: 25 })
|
|
2122
|
+
* // 'Hello Tom, you are 25 years old'
|
|
2123
|
+
*
|
|
2124
|
+
* // 支持嵌套属性访问
|
|
2125
|
+
* template('Hello {{user.name}}!', { user: { name: 'Tom' } }) // 'Hello Tom!'
|
|
2126
|
+
*
|
|
2127
|
+
* // 未找到的键会保持原样
|
|
2128
|
+
* template('Hello {{name}}!', {}) // 'Hello {{name}}!'
|
|
2129
|
+
* ```
|
|
2130
|
+
*/
|
|
2131
|
+
declare function template(template: string, data: Record<string, any>): string;
|
|
1500
2132
|
|
|
1501
2133
|
/**
|
|
1502
2134
|
* 将对象转换为URL查询字符串
|
|
@@ -1526,4 +2158,4 @@ declare function parseJsonStr<T = any>(str?: string | null, defaultValue?: T): T
|
|
|
1526
2158
|
*/
|
|
1527
2159
|
declare function getQueryStringify(params: Record<string, any> | null | undefined): string;
|
|
1528
2160
|
|
|
1529
|
-
export { type DateLike, add, addDays, addMonths, addYears, appendUniversalOption, arrayReplaceNBSP, arrayToObject, camelToKebab, capitalize, chunk, clamp, convertToDayjsParam, createDate, deepClone, deepMerge, delay, diff, endOf, ensureRpxUnit, escapeHtml, filterObjectByKeys, first, formatChatTime, formatCurrency, formatDate, formatDuration, formatFullTime, formatHumanReadable, formatNumberWithTenThousand, formatThousands, fromNow, get, getDayOfWeek, getDaysInMonth, getQueryStringify, groupBy, hasOwnProp, isArray, isBoolean, isClass, isDate, isDateInRange, isEmpty, isEmptyObject, isEmptyString, isEqual, isEven, isFunction, isMap, isMillisecondTimestamp, isMobilePhone, isNaN, isNull, isNullOrUndefined, isNumber, isObject, isOdd, isPlainObject, isPrimitive, isPromise, isRegExp, isSet, isString, isSymbol, isUndefined, isValidEmail, isValidUrl, kebabToCamel, last, merge, now, objectToQueryString, omit, parseDate, parseJsonStr, percentage, pick, randomInt, randomString, remove, renameTreeNodes, replaceNBSP, round, sample, shuffle, startOf, transformTree, truncate, unique };
|
|
2161
|
+
export { type DateLike, type DateRange, type DateShortcutFn, type DateShortcutItem, type DateShortcutsConfig, type PresetShortcutKey, add, addDays, addMonths, addYears, appendUniversalOption, arrayReplaceNBSP, arrayToObject, camelToKebab, capitalize, chunk, clamp, commonDateShortcuts, convertToDayjsParam, createDate, createDateShortcuts, debounce, deepClone, deepMerge, delay, diff, difference, endOf, ensureRpxUnit, escapeHtml, filterObjectByKeys, first, flatten, flattenDeep, flattenDepth, formatChatTime, formatCurrency, formatDate, formatDuration, formatFullTime, formatHumanReadable, formatNumberWithTenThousand, formatThousands, fromNow, get, getDateByOffset, getDayOfWeek, getDaysInMonth, getLastDaysRange, getLastMonthsRange, getLastWeeksRange, getQueryStringify, getThisHalfYearRange, getThisMonthRange, getThisQuarterRange, getThisWeekRange, getThisYearRange, getTodayRange, getTodayStr, getYesterdayStr, groupBy, hasOwnProp, intersection, invert, isArray, isBoolean, isClass, isDate, isDateInRange, isEmpty, isEmptyObject, isEmptyString, isEqual, isEven, isFunction, isMap, isMillisecondTimestamp, isMobilePhone, isNaN, isNull, isNullOrUndefined, isNumber, isObject, isOdd, isPlainObject, isPrimitive, isPromise, isRegExp, isSet, isString, isSymbol, isUndefined, isValidEmail, isValidUrl, kebabToCamel, last, mapKeys, mapValues, merge, now, objectToQueryString, omit, parallel, parseDate, parseJsonStr, pascalCase, percentage, pick, randomInt, randomString, remove, renameTreeNodes, replaceNBSP, retry, round, sample, sequential, shuffle, snakeCase, sortBy, startOf, template, throttle, timeout, transformTree, truncate, union, uniqBy, unique, words };
|