@aiao/utils 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (134) hide show
  1. package/@browser/IdleTimer.d.ts +18 -0
  2. package/@browser/broadcast-channel-pool.d.ts +17 -0
  3. package/@browser/index.d.ts +4 -0
  4. package/@browser/leader-election.d.ts +7 -0
  5. package/@browser/perform-chunk.d.ts +9 -0
  6. package/README.md +1 -0
  7. package/array/chunk.d.ts +24 -0
  8. package/array/difference.d.ts +30 -0
  9. package/array/flatten.d.ts +24 -0
  10. package/array/flattenDeep.d.ts +25 -0
  11. package/array/index.d.ts +8 -0
  12. package/array/intersection.d.ts +29 -0
  13. package/array/needArray.d.ts +37 -0
  14. package/array/sortBy.d.ts +36 -0
  15. package/async/index.d.ts +2 -0
  16. package/async/next-tick.d.ts +28 -0
  17. package/async/sleep.d.ts +5 -0
  18. package/binary/index.d.ts +1 -0
  19. package/binary/uint8ArrayToString.d.ts +12 -0
  20. package/collection/index.d.ts +1 -0
  21. package/collection/traverseObjectKeys.d.ts +3 -0
  22. package/crypto/aesDecrypt.d.ts +8 -0
  23. package/crypto/aesEncrypt.d.ts +10 -0
  24. package/crypto/base64Decode.d.ts +6 -0
  25. package/crypto/base64Encode.d.ts +6 -0
  26. package/crypto/decodeJWTPayload.d.ts +6 -0
  27. package/crypto/index.d.ts +8 -0
  28. package/crypto/rsaDecrypt.d.ts +7 -0
  29. package/crypto/rsaEncrypt.d.ts +8 -0
  30. package/crypto/rsaGenerateKey.d.ts +5 -0
  31. package/date/canBeDate.d.ts +5 -0
  32. package/date/dateStringToDate.d.ts +7 -0
  33. package/date/dateStringWithTimezone.d.ts +11 -0
  34. package/date/formatCountdown.d.ts +12 -0
  35. package/date/formatPassTime.d.ts +10 -0
  36. package/date/index.d.ts +12 -0
  37. package/date/isISODateString.d.ts +6 -0
  38. package/date/isMSTime.d.ts +7 -0
  39. package/date/msTimeToMilliseconds.d.ts +12 -0
  40. package/date/parseTime.d.ts +9 -0
  41. package/date/stringTime.d.ts +3 -0
  42. package/date/unixTimestamp.d.ts +5 -0
  43. package/function/debounce.d.ts +2 -0
  44. package/function/emptyFunction.d.ts +4 -0
  45. package/function/index.d.ts +4 -0
  46. package/function/once.d.ts +27 -0
  47. package/function/throttle.d.ts +30 -0
  48. package/index.d.ts +18 -0
  49. package/index.js +955 -0
  50. package/indexing/fractional-indexing.d.ts +26 -0
  51. package/number/canBeNumber.d.ts +27 -0
  52. package/number/index.d.ts +6 -0
  53. package/number/numberStep.d.ts +19 -0
  54. package/number/numberStepScreenSize.d.ts +1 -0
  55. package/number/numberStrip.d.ts +21 -0
  56. package/number/toInt.d.ts +1 -0
  57. package/number/tryToNumber.d.ts +1 -0
  58. package/object/deepFreeze.d.ts +5 -0
  59. package/object/flattenPathObjectToPlainObject.d.ts +7 -0
  60. package/object/get.d.ts +23 -0
  61. package/object/getTag.d.ts +1 -0
  62. package/object/has.d.ts +20 -0
  63. package/object/index.d.ts +15 -0
  64. package/object/isEqual.d.ts +25 -0
  65. package/object/isEqualUint8Array.d.ts +1 -0
  66. package/object/omit.d.ts +24 -0
  67. package/object/omitBy.d.ts +7 -0
  68. package/object/pick.d.ts +24 -0
  69. package/object/pickBy.d.ts +1 -0
  70. package/object/plainObjectToFlattenPathObject.d.ts +8 -0
  71. package/object/set.d.ts +27 -0
  72. package/object/setWith.d.ts +12 -0
  73. package/object/toPlainObject.d.ts +1 -0
  74. package/object/zipObject.d.ts +9 -0
  75. package/package.json +20 -0
  76. package/platform/index.d.ts +1 -0
  77. package/platform/is-browser.d.ts +4 -0
  78. package/random/index.d.ts +6 -0
  79. package/random/randomArrayItem.d.ts +1 -0
  80. package/random/randomFloat.d.ts +1 -0
  81. package/random/randomInt.d.ts +7 -0
  82. package/random/randomString.d.ts +9 -0
  83. package/random/randomUintByLength.d.ts +1 -0
  84. package/random/randomUintString.d.ts +1 -0
  85. package/string/camelCase.d.ts +22 -0
  86. package/string/capitalize.d.ts +7 -0
  87. package/string/compressToBase64Url.d.ts +4 -0
  88. package/string/decompressFromBase64Url.d.ts +4 -0
  89. package/string/getWords.d.ts +1 -0
  90. package/string/index.d.ts +17 -0
  91. package/string/kebabCase.d.ts +9 -0
  92. package/string/parseChineseNumber.d.ts +1 -0
  93. package/string/queryParse.d.ts +1 -0
  94. package/string/queryStringify.d.ts +5 -0
  95. package/string/rmb.d.ts +6 -0
  96. package/string/similarity.d.ts +1 -0
  97. package/string/snakeCase.d.ts +10 -0
  98. package/string/startCase.d.ts +7 -0
  99. package/string/stringSingleline.d.ts +4 -0
  100. package/string/stringTemplate.d.ts +7 -0
  101. package/string/stringToArrayBuffer.d.ts +7 -0
  102. package/string/uncapitalize.d.ts +7 -0
  103. package/string/urlJoin.d.ts +1 -0
  104. package/tools/event.d.ts +18 -0
  105. package/tools/image.d.ts +2 -0
  106. package/tools/index.d.ts +3 -0
  107. package/tools/log.d.ts +4 -0
  108. package/type-definition/index.d.ts +1 -0
  109. package/type-definition/result-types.d.ts +11 -0
  110. package/types/AnyFunction.d.ts +20 -0
  111. package/types/DeepPartial.d.ts +48 -0
  112. package/types/index.d.ts +24 -0
  113. package/types/isArray.d.ts +19 -0
  114. package/types/isArrayBuffer.d.ts +23 -0
  115. package/types/isBoolean.d.ts +27 -0
  116. package/types/isDate.d.ts +25 -0
  117. package/types/isEmpty.d.ts +40 -0
  118. package/types/isFloat.d.ts +1 -0
  119. package/types/isFunction.d.ts +2 -0
  120. package/types/isInt.d.ts +1 -0
  121. package/types/isIntArray.d.ts +1 -0
  122. package/types/isNil.d.ts +20 -0
  123. package/types/isNumber.d.ts +29 -0
  124. package/types/isNumberArray.d.ts +1 -0
  125. package/types/isObject.d.ts +22 -0
  126. package/types/isObjectLike.d.ts +1 -0
  127. package/types/isPlainObject.d.ts +27 -0
  128. package/types/isPrimitive.d.ts +33 -0
  129. package/types/isPromise.d.ts +1 -0
  130. package/types/isRegExp.d.ts +23 -0
  131. package/types/isString.d.ts +22 -0
  132. package/types/isStringArray.d.ts +1 -0
  133. package/types/isSymbol.d.ts +1 -0
  134. package/types/isUint8Array.d.ts +25 -0
@@ -0,0 +1,18 @@
1
+ interface IdleTimerOptions {
2
+ /**
3
+ * 多少毫秒内没有时间就算是空闲
4
+ * @default 2000
5
+ */
6
+ timeout?: number;
7
+ }
8
+ /**
9
+ * 浏览器空闲检测
10
+ */
11
+ export declare class IdleTimer {
12
+ #private;
13
+ private options?;
14
+ idle$: import('rxjs').Observable<boolean>;
15
+ constructor(options?: IdleTimerOptions | undefined);
16
+ start(): void;
17
+ }
18
+ export {};
@@ -0,0 +1,17 @@
1
+ import { Observable } from 'rxjs';
2
+ export type BroadcastTopic<T> = {
3
+ message$: Observable<T>;
4
+ emit: (message: T) => void;
5
+ };
6
+ /**
7
+ * BroadcastChannel pool
8
+ * 统一管理所有的 BroadcastChannel
9
+ */
10
+ declare class BroadcastChannelPool {
11
+ #private;
12
+ emit<T = any>(event: string, data: T): void;
13
+ on<T = any>(event: string): Observable<any>;
14
+ }
15
+ export declare const pool: BroadcastChannelPool;
16
+ export declare const createBroadcastTopic: <T = any>(event: string) => BroadcastTopic<T>;
17
+ export {};
@@ -0,0 +1,4 @@
1
+ export * from './broadcast-channel-pool';
2
+ export * from './IdleTimer';
3
+ export * from './leader-election';
4
+ export * from './perform-chunk';
@@ -0,0 +1,7 @@
1
+ export declare class LeaderElection {
2
+ #private;
3
+ private name;
4
+ get isLeader(): boolean;
5
+ constructor(name: string);
6
+ leadership(): import('rxjs').Observable<boolean>;
7
+ }
@@ -0,0 +1,9 @@
1
+ /**
2
+ * 分片执行任务
3
+ * 只能在浏览器环境中执行
4
+ * 把一个大任务分片执行,每次执行一小部分任务,然后让出主线程,等待下一次执行
5
+ *
6
+ * @param array 要执行的数据组
7
+ * @param consumer 消费者函数
8
+ */
9
+ export declare function performChunk<T>(array: T[], consumer: (data: T, index: number) => void): void;
package/README.md ADDED
@@ -0,0 +1 @@
1
+ # utils
@@ -0,0 +1,24 @@
1
+ /**
2
+ * 将数组分割成指定大小的数组块
3
+ * 当数组长度不能被分割大小整除时,最后一个块将包含剩余的元素
4
+ *
5
+ * @template T - 数组元素的类型
6
+ * @param array - 要分割的数组(只读,不能为null/undefined)
7
+ * @param size - 每个块的大小,必须是正整数
8
+ * @returns 分割后的数组块组成的新数组
9
+ * @example
10
+ * chunk(['a', 'b', 'c', 'd'], 2);
11
+ * // 返回 [['a', 'b'], ['c', 'd']]
12
+ * @example
13
+ * chunk(['a', 'b', 'c', 'd'], 3);
14
+ * // 返回 [['a', 'b', 'c'], ['d']]
15
+ * @example
16
+ * chunk([], 2); // 返回 [](空数组)
17
+ * @example
18
+ * chunk([1, 2, 3], 5); // 返回 [[1, 2, 3]](块大小大于数组长度)
19
+ * @example
20
+ * chunk([1, 2, 3, 4], 1); // 返回 [[1], [2], [3], [4]](块大小为1)
21
+ * @warning size必须是正整数,非正数值可能导致意外行为
22
+ * @note 函数不会修改原数组,而是返回一个包含新数组块的新数组
23
+ */
24
+ export declare const chunk: <T>(array: readonly T[], size: number) => T[][];
@@ -0,0 +1,30 @@
1
+ /**
2
+ * 计算两个数组的差异,返回仅存在于第一个数组中且不在第二个数组中的元素
3
+ * 元素比较使用严格相等运算符 (===),因此对象仅在引用相同时被视为相等
4
+ * 结果数组保留元素在原数组中的出现顺序
5
+ *
6
+ * @template T - 数组元素的类型
7
+ * @param array1 - 源数组,从中筛选差异元素
8
+ * @param array2 - 比较数组,用于判断元素是否应被排除
9
+ * @returns 新数组,包含所有仅存在于 array1 中的元素(保留原顺序)
10
+ * @example
11
+ * difference([0, 1, 2], [2, 3, 4]);
12
+ * // 返回 [0, 1](基础类型比较)
13
+ * @example
14
+ * difference(['a', 'b', 'c'], ['b', 'd']);
15
+ * // 返回 ['a', 'c'](字符串比较)
16
+ * @example
17
+ * difference([1, 2, 2, 3], [2]);
18
+ * // 返回 [1, 3](自动去重 array1 中被排除的重复元素)
19
+ * @example
20
+ * difference([], [1, 2]); // 返回 [](源数组为空)
21
+ * @example
22
+ * difference([1, 2], []); // 返回 [1, 2](比较数组为空)
23
+ * @example
24
+ * difference([{ id: 1 }], [{ id: 1 }]);
25
+ * // 返回 [{ id: 1 }](对象引用不同,视为不同元素)
26
+ * @note 使用 Array.prototype.includes 实现,因此比较使用严格相等 (===)
27
+ * @note 结果数组中不会包含 array1 中重复出现但也存在于 array2 中的元素
28
+ * @warning 不支持对象的深度比较,仅比较引用是否相同
29
+ */
30
+ export declare const difference: <T>(array1: T[], array2: T[]) => T[];
@@ -0,0 +1,24 @@
1
+ /**
2
+ * 将嵌套一层的数组扁平化为一维数组
3
+ * 仅处理数组中的直接嵌套数组元素,不进行深度扁平化
4
+ *
5
+ * @template T - 数组元素的基础类型
6
+ * @param array - 输入数组,包含基础类型元素和/或嵌套一层的数组
7
+ * @returns 扁平化后的新数组,所有元素均为基础类型T
8
+ * @example
9
+ * flatten([1, [2, 3], 4]);
10
+ * // 返回 [1, 2, 3, 4]
11
+ * @example
12
+ * flatten(['a', ['b', ['c']], 'd']);
13
+ * // 返回 ['a', 'b', ['c'], 'd'] (仅扁平化一层,保留深层嵌套数组)
14
+ * @example
15
+ * flatten([[], [1, 2], 3]); // 返回 [1, 2, 3] (处理空数组)
16
+ * @example
17
+ * flatten([1, 2, 3]); // 返回 [1, 2, 3] (无嵌套数组时返回原数组元素)
18
+ * @example
19
+ * flatten([]); // 返回 [] (空输入数组)
20
+ * @note 与flattenDeep的区别:此函数仅扁平化一层,而flattenDeep会递归扁平化所有层级
21
+ * @note 函数返回新数组,不会修改原输入数组
22
+ * @note 非数组元素会被直接包含在结果中
23
+ */
24
+ export declare const flatten: <T>(array: (T | T[])[]) => T[];
@@ -0,0 +1,25 @@
1
+ /**
2
+ * 将任意深度的嵌套数组完全扁平化为一维数组
3
+ * 递归处理所有层级的嵌套数组,直至所有元素都为非数组类型
4
+ *
5
+ * @template T - 数组元素的基础类型
6
+ * @param array - 任意深度的嵌套数组,可包含基础类型元素和/或嵌套数组
7
+ * @returns 完全扁平化后的一维数组,所有元素均为基础类型T
8
+ * @example
9
+ * flattenDeep([1, [2, [3, [4]], 5]]);
10
+ * // 返回 [1, 2, 3, 4, 5] (多层嵌套数组)
11
+ * @example
12
+ * flattenDeep(['a', [['b']], [[[['c']]]]]);
13
+ * // 返回 ['a', 'b', 'c'] (极深嵌套数组)
14
+ * @example
15
+ * flattenDeep([]); // 返回 [] (空数组)
16
+ * @example
17
+ * flattenDeep([1, [2, [3]], null, undefined, [true]]);
18
+ * // 返回 [1, 2, 3, null, undefined, true] (包含多种类型元素)
19
+ * @example
20
+ * flattenDeep([[[[[1]]]]]); // 返回 [1] (单一元素的深层嵌套)
21
+ * @note 使用递归实现,对于层级极深的数组可能导致栈溢出
22
+ * @note 非数组元素(包括null和undefined)会被直接包含在结果中
23
+ * @note 仅处理Array类型的嵌套结构,不处理类数组对象(如arguments、NodeList等)
24
+ */
25
+ export declare const flattenDeep: (array: any[]) => any[];
@@ -0,0 +1,8 @@
1
+ export * from './chunk';
2
+ export * from './difference';
3
+ export * from './flatten';
4
+ export { flattenDeep } from './flattenDeep';
5
+ export * from './index';
6
+ export * from './intersection';
7
+ export * from './needArray';
8
+ export * from './sortBy';
@@ -0,0 +1,29 @@
1
+ /**
2
+ * 计算多个数组的交集,返回所有输入数组中都存在的元素
3
+ * 使用严格相等运算符 (===) 进行元素比较,结果数组保留第一个数组中的元素顺序
4
+ * 如果输入单个数组,则返回该数组的去重版本
5
+ *
6
+ * @param arrays - 任意数量的数组,至少需要一个数组参数
7
+ * @returns 包含所有输入数组共有元素的新数组(自动去重)
8
+ * @example
9
+ * intersection([1, 2, 3], [2, 3, 4], [2, 5]);
10
+ * // 返回 [2](多数组交集)
11
+ * @example
12
+ * intersection(['a', 'b'], ['b', 'a'], ['b']);
13
+ * // 返回 ['b'](保留第一个数组的元素顺序)
14
+ * @example
15
+ * intersection([1, 2, 2, 3], [2, 2, 4]);
16
+ * // 返回 [2](自动去重结果)
17
+ * @example
18
+ * intersection([1, 2]); // 返回 [1, 2](单个数组去重)
19
+ * @example
20
+ * intersection([], [1, 2]); // 返回 [](任意数组为空则结果为空)
21
+ * @example
22
+ * intersection([{ id: 1 }], [{ id: 1 }]);
23
+ * // 返回 [](对象引用不同,视为不同元素)
24
+ * @note 元素比较使用严格相等 (===),因此 1 和 '1' 被视为不同元素
25
+ * @note 结果数组会自动去重,即使原数组包含重复元素
26
+ * @note 至少需要传入一个数组参数,否则会抛出运行时错误
27
+ * @warning 对于大型数组,此实现可能存在性能问题(时间复杂度 O(n^2))
28
+ */
29
+ export declare const intersection: (...arrays: any[]) => any;
@@ -0,0 +1,37 @@
1
+ type ArrayType<T = any> = T extends Array<infer R> ? R : T;
2
+ /**
3
+ * 将任意值标准化为数组格式,提供一致的数组处理接口
4
+ * 处理规则:
5
+ * - 若输入已是数组,则直接返回原数组(不创建新数组)
6
+ * - 若输入为null/undefined,则返回空数组
7
+ * - 其他任何类型的值均包装为单元素数组
8
+ *
9
+ * @template T - 输入值的类型,可以是任意类型(包括数组)
10
+ * @param value - 需要标准化为数组的值
11
+ * @returns 标准化后的数组
12
+ * - 当T为数组类型时,返回T本身
13
+ * - 当T为null/undefined时,返回空数组
14
+ * - 其他情况返回包含value的单元素数组
15
+ * @example
16
+ * needArray(5); // 返回 [5](基本类型包装)
17
+ * @example
18
+ * needArray([1, 2, 3]); // 返回 [1, 2, 3](数组直接返回)
19
+ * @example
20
+ * needArray(null); // 返回 [](null处理)
21
+ * @example
22
+ * needArray(undefined); // 返回 [](undefined处理)
23
+ * @example
24
+ * needArray('hello'); // 返回 ['hello'](字符串包装)
25
+ * @example
26
+ * needArray({ a: 1 }); // 返回 [{ a: 1 }](对象包装)
27
+ * @example
28
+ * needArray(true); // 返回 [true](布尔值包装)
29
+ * @example
30
+ * needArray(new Date()); // 返回 [Date实例](特殊对象包装)
31
+ * @see ArrayType - 用于提取数组元素类型的工具类型,定义为:type ArrayType<T = any> = T extends Array<infer R> ? R : T
32
+ * @note 使用isNil函数判断null/undefined,该函数来自'../types'模块
33
+ * @note 对于数组输入,返回原数组引用而非副本,修改返回值会影响原数组
34
+ * @note 不处理类数组对象(如arguments、NodeList),会将其视为普通对象包装为单元素数组
35
+ */
36
+ export declare const needArray: <T = any>(value: T) => ArrayType<T>[];
37
+ export {};
@@ -0,0 +1,36 @@
1
+ /**
2
+ * 创建一个按指定对象属性排序的比较函数,用于数组排序
3
+ * 按属性值的自然顺序进行升序排序,支持字符串、数字等基本类型比较
4
+ * 生成的比较函数兼容Array.sort()方法,会修改原数组
5
+ *
6
+ * @template T - 数组元素对象的类型
7
+ * @param key - 用于排序的对象属性名,必须是T的有效属性
8
+ * @returns 比较函数,返回值:
9
+ * - 正数:a应排在b后面
10
+ * - 负数:a应排在b前面
11
+ * - 0:a和b位置不变
12
+ * @example
13
+ * const data = [
14
+ * { name: 'Bob', age: 30 },
15
+ * { name: 'Alice', age: 25 },
16
+ * { name: 'Charlie', age: 35 }
17
+ * ];
18
+ * // 按age升序排序
19
+ * data.sort(sortBy('age')); // 结果: Alice(25), Bob(30), Charlie(35)
20
+ * @example
21
+ * // 按name字母顺序排序(区分大小写)
22
+ * data.sort(sortBy('name')); // 结果: Alice, Bob, Charlie
23
+ * @example
24
+ * // 数字字符串会按字典序而非数值排序
25
+ * const items = [{ id: '10' }, { id: '2' }, { id: '1' }];
26
+ * items.sort(sortBy('id')); // 结果: [{id: '1'}, {id: '10'}, {id: '2'}]
27
+ * @example
28
+ * // 处理包含null/undefined的属性值
29
+ * const mixed = [{ score: 90 }, { score: null }, { score: 85 }];
30
+ * mixed.sort(sortBy('score')); // null值排在最后
31
+ * @note 字符串比较区分大小写,'Z'会排在'a'前面
32
+ * @note 不支持对象类型的属性值比较,复杂类型需先转换为基本类型
33
+ * @note 排序是原地(in-place)进行的,会修改原始数组
34
+ * @note 当属性不存在于对象中时,该对象会被视为值为undefined
35
+ */
36
+ export declare const sortBy: (key: string) => (a: Record<string, any>, b: Record<string, any>) => 0 | 1 | -1;
@@ -0,0 +1,2 @@
1
+ export { nextTick } from './next-tick';
2
+ export * from './sleep';
@@ -0,0 +1,28 @@
1
+ /**
2
+ * 将函数推迟到下一个微任务队列执行
3
+ * 使用Promise.resolve()实现,确保函数在当前同步代码执行完成后、DOM更新前执行
4
+ *
5
+ * @template T - 回调函数中this的类型
6
+ * @template R - 回调函数的返回值类型
7
+ * @example
8
+ * // 基础用法
9
+ * nextTick(() => {
10
+ * console.log('在当前同步代码后执行');
11
+ * });
12
+ * @example
13
+ * // 带返回值和async/await
14
+ * async function example() {
15
+ * await nextTick(() => '执行完成');
16
+ * console.log('Promise已解析');
17
+ * }
18
+ * @example
19
+ * // 无回调函数(仅获取下一个微任务的Promise)
20
+ * async function delay() {
21
+ * await nextTick();
22
+ * console.log('下一个微任务');
23
+ * }
24
+ * @param fn - 要推迟执行的函数,可选
25
+ * @returns 解析为回调函数返回值的Promise,若回调抛出错误则Promise会被拒绝
26
+ * @note 基于微任务队列实现,执行时机早于setTimeout(fn, 0)等宏任务
27
+ */
28
+ export declare const nextTick: <T = void, R = void>(fn: (this: T) => R) => Promise<R>;
@@ -0,0 +1,5 @@
1
+ /**
2
+ * 等待时间
3
+ * @param ms 毫秒
4
+ */
5
+ export declare const sleep: (ms: number) => Promise<unknown>;
@@ -0,0 +1 @@
1
+ export { uint8ArrayToString } from './uint8ArrayToString';
@@ -0,0 +1,12 @@
1
+ /**
2
+ * 将Uint8Array转换为UTF-8字符串
3
+ * 使用TextDecoder API进行解码,确保正确处理多字节字符
4
+ *
5
+ * @example
6
+ * const bytes = new Uint8Array([72, 101, 108, 108, 111]);
7
+ * uint8ArrayToString(bytes); // 返回 'Hello'
8
+ * @param u8 - 要转换的Uint8Array实例
9
+ * @returns 解码后的UTF-8字符串
10
+ * @note 使用默认的UTF-8编码,不支持其他字符编码
11
+ */
12
+ export declare const uint8ArrayToString: (u8: Uint8Array) => string;
@@ -0,0 +1 @@
1
+ export { traverseObjectKeys } from './traverseObjectKeys';
@@ -0,0 +1,3 @@
1
+ type Callback = (key: string, value: any, object: any) => void;
2
+ export declare const traverseObjectKeys: (objOrArray: Record<string, any> | any[], callback: Callback) => void;
3
+ export {};
@@ -0,0 +1,8 @@
1
+ /**
2
+ * AES 解密
3
+ * @param cipherText
4
+ * @param keyString
5
+ * @param ivString
6
+ * @returns
7
+ */
8
+ export declare function aesDecrypt(cipherText: string, keyString: string, ivString: string): Promise<string>;
@@ -0,0 +1,10 @@
1
+ /**
2
+ * AES 加密
3
+ * @param plaintext
4
+ * @returns
5
+ */
6
+ export declare function aesEncrypt(text: string): Promise<{
7
+ key: string;
8
+ iv: string;
9
+ cipherText: string;
10
+ }>;
@@ -0,0 +1,6 @@
1
+ /**
2
+ * base64 解码
3
+ * @param value 字符串
4
+ * @returns
5
+ */
6
+ export declare const base64Decode: (value: string) => Uint8Array<ArrayBuffer>;
@@ -0,0 +1,6 @@
1
+ /**
2
+ * base64 编码
3
+ * @param value
4
+ * @returns
5
+ */
6
+ export declare const base64Encode: (value: Uint8Array | ArrayBuffer | string | number[]) => string;
@@ -0,0 +1,6 @@
1
+ /**
2
+ * 解码 JWT token 的信息
3
+ * @param token
4
+ * @returns
5
+ */
6
+ export declare const decodeJWTPayload: (token: string) => any;
@@ -0,0 +1,8 @@
1
+ export { aesDecrypt } from './aesDecrypt';
2
+ export { aesEncrypt } from './aesEncrypt';
3
+ export { base64Decode } from './base64Decode';
4
+ export { base64Encode } from './base64Encode';
5
+ export { decodeJWTPayload } from './decodeJWTPayload';
6
+ export { rsaDecrypt } from './rsaDecrypt';
7
+ export { rsaEncrypt } from './rsaEncrypt';
8
+ export { rsaGenerateKey } from './rsaGenerateKey';
@@ -0,0 +1,7 @@
1
+ /**
2
+ * RSA 解密
3
+ * @param cipherText 密文
4
+ * @param privateKeyPem 私钥
5
+ * @returns
6
+ */
7
+ export declare function rsaDecrypt(cipherText: string, privateKeyPem: string): Promise<string>;
@@ -0,0 +1,8 @@
1
+ export declare function importRsaPublicKey(publicKeyPem: string): Promise<CryptoKey>;
2
+ /**
3
+ * RAS 加密
4
+ * @param text
5
+ * @param publicKeyPem
6
+ * @returns
7
+ */
8
+ export declare function rsaEncrypt(text: string, publicKeyPem: string): Promise<string>;
@@ -0,0 +1,5 @@
1
+ export declare function exportCryptoKey(key: CryptoKey, type?: Exclude<KeyFormat, 'jwk'>): Promise<string>;
2
+ export declare function rsaGenerateKey(modulusLength?: number): Promise<{
3
+ publicKey: string;
4
+ privateKey: string;
5
+ }>;
@@ -0,0 +1,5 @@
1
+ /**
2
+ * 判断是否可以转换成日期
3
+ * @param date 时间
4
+ */
5
+ export declare const canBeDate: (date?: unknown) => boolean;
@@ -0,0 +1,7 @@
1
+ /**
2
+ * UTC 时间转换
3
+ * offset 分钟
4
+ * date 2018-12-04
5
+ * time 19:09:10
6
+ */
7
+ export declare const dateStringToDate: (date: string, time: string, offset: number) => Date;
@@ -0,0 +1,11 @@
1
+ /**
2
+ * UTC 时间转换
3
+ * @param date 2018-12-04
4
+ * @param time 19:09:10
5
+ * @param offset 本地时间 减去 格林威治标准时间 (GMT) 的分钟量 与 getTimezoneOffset 相反数
6
+ * @returns
7
+ *
8
+ * @example
9
+ * dateStringWithTimezone('2018-12-04', '19:09:10', 480) => '2018-12-04T19:09:10+08:00'
10
+ */
11
+ export declare const dateStringWithTimezone: (date: string, time: string, offset: number) => string;
@@ -0,0 +1,12 @@
1
+ import { ParseTimeConfig } from './formatPassTime';
2
+ import { ParseTime } from './parseTime';
3
+ type FormatCountdownFunction = (input: ParseTime) => string;
4
+ /**
5
+ * 倒计时
6
+ * @param startDate 开始时间
7
+ * @param endDate 结束时间
8
+ * @param config 配置
9
+ * @returns
10
+ */
11
+ export declare const formatCountdown: (startDate: Date, endDate: Date, config?: ParseTimeConfig | FormatCountdownFunction) => string;
12
+ export {};
@@ -0,0 +1,10 @@
1
+ import { ParseTime } from './parseTime';
2
+ export type ParseTimeConfig = {
3
+ [K in keyof ParseTime]: string;
4
+ };
5
+ export type FormatPassFunction = (input: {
6
+ key: keyof ParseTime;
7
+ value: number;
8
+ }) => string;
9
+ export declare const dateKeys: (keyof ParseTime)[];
10
+ export declare const formatPassTime: (startDate: Date, endDate: Date, config?: ParseTimeConfig | FormatPassFunction) => string;
@@ -0,0 +1,12 @@
1
+ export { canBeDate } from './canBeDate';
2
+ export { dateStringToDate } from './dateStringToDate';
3
+ export { dateStringWithTimezone } from './dateStringWithTimezone';
4
+ export { formatCountdown } from './formatCountdown';
5
+ export { formatPassTime } from './formatPassTime';
6
+ export { isISODateString } from './isISODateString';
7
+ export { isMSTime } from './isMSTime';
8
+ export { msTimeToMilliseconds } from './msTimeToMilliseconds';
9
+ export type { MSTime } from './msTimeToMilliseconds';
10
+ export { parseTime } from './parseTime';
11
+ export { stringTime } from './stringTime';
12
+ export { unixTimestamp } from './unixTimestamp';
@@ -0,0 +1,6 @@
1
+ /**
2
+ * string 是否是 ISO 日期字符串
3
+ * @param value
4
+ * @returns
5
+ */
6
+ export declare const isISODateString: (value: unknown) => boolean;
@@ -0,0 +1,7 @@
1
+ import { MSTime } from './msTimeToMilliseconds';
2
+ /**
3
+ * 判断是否是 ms 时间类型
4
+ * https://github.com/vercel/ms
5
+ * @param value
6
+ */
7
+ export declare function isMSTime(value: string | number): value is MSTime;
@@ -0,0 +1,12 @@
1
+ import { default as ms } from 'ms';
2
+ /**
3
+ * 毫秒
4
+ */
5
+ export type Milliseconds = number;
6
+ export type MSTime = ms.StringValue;
7
+ /**]
8
+ * ms 时间转换为毫秒
9
+ * value https://github.com/vercel/ms
10
+ * @example '2 days', '1d', '10h',
11
+ */
12
+ export declare function msTimeToMilliseconds(value: MSTime): Milliseconds;
@@ -0,0 +1,9 @@
1
+ export interface ParseTime {
2
+ year: number;
3
+ month: number;
4
+ day: number;
5
+ hour: number;
6
+ minute: number;
7
+ second: number;
8
+ }
9
+ export declare const parseTime: (startDate: Date, endDate: Date) => ParseTime;
@@ -0,0 +1,3 @@
1
+ import { ParseTimeConfig } from './formatPassTime';
2
+ import { ParseTime } from './parseTime';
3
+ export declare const stringTime: (key: keyof ParseTime, value: number, config?: ParseTimeConfig) => string;
@@ -0,0 +1,5 @@
1
+ /**
2
+ * unix 时间戳
3
+ * @returns
4
+ */
5
+ export declare const unixTimestamp: () => number;
@@ -0,0 +1,2 @@
1
+ import { AnyFunction } from '../types';
2
+ export declare const debounce: <Func extends AnyFunction>(func: Func, waitMilliseconds?: number) => ((this: ThisParameterType<Func>, ...args: Parameters<Func>) => void);
@@ -0,0 +1,4 @@
1
+ /**
2
+ * 空函数
3
+ */
4
+ export declare const emptyFunction: () => void;
@@ -0,0 +1,4 @@
1
+ export { debounce } from './debounce';
2
+ export { emptyFunction, emptyFunction as noop } from './emptyFunction';
3
+ export { once } from './once';
4
+ export { throttle } from './throttle';
@@ -0,0 +1,27 @@
1
+ /**
2
+ * 创建一个只执行一次的函数,后续调用将返回第一次执行的结果
3
+ * 适用于初始化函数、资源加载等只需要执行一次的场景
4
+ *
5
+ * @template F - 被包装的函数类型,必须是无参数函数
6
+ * @param func - 需要限制只执行一次的函数
7
+ * @returns 包装后的函数,与原函数类型相同
8
+ * @example
9
+ * // 基础用法
10
+ * const initialize = once(() => {
11
+ * console.log('初始化');
12
+ * return '完成';
13
+ * });
14
+ * initialize(); // 输出 '初始化' 并返回 '完成'
15
+ * initialize(); // 直接返回 '完成',不执行函数体
16
+ * @example
17
+ * // 带返回值的场景
18
+ * const getConfig = once(() => {
19
+ * return fetch('/config').then(res => res.json());
20
+ * });
21
+ * // 多次调用只会发起一次请求
22
+ * getConfig();
23
+ * getConfig();
24
+ * @note 该实现仅支持无参数函数,如需处理带参数的函数,请使用其他实现
25
+ * @note 第一次执行的结果会被缓存,后续调用返回相同结果
26
+ */
27
+ export declare function once<F extends () => any>(func: F): F;
@@ -0,0 +1,30 @@
1
+ import { AnyFunction } from '../types';
2
+ /**
3
+ * 创建一个节流函数,确保函数在指定时间间隔内最多执行一次
4
+ * 与防抖不同,节流会定期执行函数,而非等待最后一次调用后执行,适用于限制高频事件(如滚动、调整大小)的触发频率
5
+ *
6
+ * @template Func - 被节流的函数类型
7
+ * @param func - 需要节流的函数
8
+ * @param timeFrame - 时间间隔(毫秒),在此期间函数最多执行一次
9
+ * @returns 节流后的函数
10
+ * @example
11
+ * // 基础用法
12
+ * const handleScroll = throttle(() => {
13
+ * console.log('滚动位置:', window.scrollY);
14
+ * }, 100);
15
+ * window.addEventListener('scroll', handleScroll);
16
+ * @example
17
+ * // 带参数和this上下文
18
+ * const logger = {
19
+ * prefix: 'Log:',
20
+ * log: throttle(function(message: string) {
21
+ * console.log(this.prefix, message);
22
+ * }, 200)
23
+ * };
24
+ * logger.log('Hello');
25
+ * logger.log('World'); // 200ms内只会执行一次
26
+ * @note 节流函数会忽略在时间间隔内的调用,不累积执行
27
+ * @note 首次调用会立即执行,之后按时间间隔限制
28
+ * @warning 确保timeFrame为正整数,否则可能导致意外行为
29
+ */
30
+ export declare const throttle: <Func extends AnyFunction>(func: Func, timeFrame: number) => (this: ThisParameterType<Func>, ...args: Parameters<Func>) => void;