@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.
- package/@browser/IdleTimer.d.ts +18 -0
- package/@browser/broadcast-channel-pool.d.ts +17 -0
- package/@browser/index.d.ts +4 -0
- package/@browser/leader-election.d.ts +7 -0
- package/@browser/perform-chunk.d.ts +9 -0
- package/README.md +1 -0
- package/array/chunk.d.ts +24 -0
- package/array/difference.d.ts +30 -0
- package/array/flatten.d.ts +24 -0
- package/array/flattenDeep.d.ts +25 -0
- package/array/index.d.ts +8 -0
- package/array/intersection.d.ts +29 -0
- package/array/needArray.d.ts +37 -0
- package/array/sortBy.d.ts +36 -0
- package/async/index.d.ts +2 -0
- package/async/next-tick.d.ts +28 -0
- package/async/sleep.d.ts +5 -0
- package/binary/index.d.ts +1 -0
- package/binary/uint8ArrayToString.d.ts +12 -0
- package/collection/index.d.ts +1 -0
- package/collection/traverseObjectKeys.d.ts +3 -0
- package/crypto/aesDecrypt.d.ts +8 -0
- package/crypto/aesEncrypt.d.ts +10 -0
- package/crypto/base64Decode.d.ts +6 -0
- package/crypto/base64Encode.d.ts +6 -0
- package/crypto/decodeJWTPayload.d.ts +6 -0
- package/crypto/index.d.ts +8 -0
- package/crypto/rsaDecrypt.d.ts +7 -0
- package/crypto/rsaEncrypt.d.ts +8 -0
- package/crypto/rsaGenerateKey.d.ts +5 -0
- package/date/canBeDate.d.ts +5 -0
- package/date/dateStringToDate.d.ts +7 -0
- package/date/dateStringWithTimezone.d.ts +11 -0
- package/date/formatCountdown.d.ts +12 -0
- package/date/formatPassTime.d.ts +10 -0
- package/date/index.d.ts +12 -0
- package/date/isISODateString.d.ts +6 -0
- package/date/isMSTime.d.ts +7 -0
- package/date/msTimeToMilliseconds.d.ts +12 -0
- package/date/parseTime.d.ts +9 -0
- package/date/stringTime.d.ts +3 -0
- package/date/unixTimestamp.d.ts +5 -0
- package/function/debounce.d.ts +2 -0
- package/function/emptyFunction.d.ts +4 -0
- package/function/index.d.ts +4 -0
- package/function/once.d.ts +27 -0
- package/function/throttle.d.ts +30 -0
- package/index.d.ts +18 -0
- package/index.js +955 -0
- package/indexing/fractional-indexing.d.ts +26 -0
- package/number/canBeNumber.d.ts +27 -0
- package/number/index.d.ts +6 -0
- package/number/numberStep.d.ts +19 -0
- package/number/numberStepScreenSize.d.ts +1 -0
- package/number/numberStrip.d.ts +21 -0
- package/number/toInt.d.ts +1 -0
- package/number/tryToNumber.d.ts +1 -0
- package/object/deepFreeze.d.ts +5 -0
- package/object/flattenPathObjectToPlainObject.d.ts +7 -0
- package/object/get.d.ts +23 -0
- package/object/getTag.d.ts +1 -0
- package/object/has.d.ts +20 -0
- package/object/index.d.ts +15 -0
- package/object/isEqual.d.ts +25 -0
- package/object/isEqualUint8Array.d.ts +1 -0
- package/object/omit.d.ts +24 -0
- package/object/omitBy.d.ts +7 -0
- package/object/pick.d.ts +24 -0
- package/object/pickBy.d.ts +1 -0
- package/object/plainObjectToFlattenPathObject.d.ts +8 -0
- package/object/set.d.ts +27 -0
- package/object/setWith.d.ts +12 -0
- package/object/toPlainObject.d.ts +1 -0
- package/object/zipObject.d.ts +9 -0
- package/package.json +20 -0
- package/platform/index.d.ts +1 -0
- package/platform/is-browser.d.ts +4 -0
- package/random/index.d.ts +6 -0
- package/random/randomArrayItem.d.ts +1 -0
- package/random/randomFloat.d.ts +1 -0
- package/random/randomInt.d.ts +7 -0
- package/random/randomString.d.ts +9 -0
- package/random/randomUintByLength.d.ts +1 -0
- package/random/randomUintString.d.ts +1 -0
- package/string/camelCase.d.ts +22 -0
- package/string/capitalize.d.ts +7 -0
- package/string/compressToBase64Url.d.ts +4 -0
- package/string/decompressFromBase64Url.d.ts +4 -0
- package/string/getWords.d.ts +1 -0
- package/string/index.d.ts +17 -0
- package/string/kebabCase.d.ts +9 -0
- package/string/parseChineseNumber.d.ts +1 -0
- package/string/queryParse.d.ts +1 -0
- package/string/queryStringify.d.ts +5 -0
- package/string/rmb.d.ts +6 -0
- package/string/similarity.d.ts +1 -0
- package/string/snakeCase.d.ts +10 -0
- package/string/startCase.d.ts +7 -0
- package/string/stringSingleline.d.ts +4 -0
- package/string/stringTemplate.d.ts +7 -0
- package/string/stringToArrayBuffer.d.ts +7 -0
- package/string/uncapitalize.d.ts +7 -0
- package/string/urlJoin.d.ts +1 -0
- package/tools/event.d.ts +18 -0
- package/tools/image.d.ts +2 -0
- package/tools/index.d.ts +3 -0
- package/tools/log.d.ts +4 -0
- package/type-definition/index.d.ts +1 -0
- package/type-definition/result-types.d.ts +11 -0
- package/types/AnyFunction.d.ts +20 -0
- package/types/DeepPartial.d.ts +48 -0
- package/types/index.d.ts +24 -0
- package/types/isArray.d.ts +19 -0
- package/types/isArrayBuffer.d.ts +23 -0
- package/types/isBoolean.d.ts +27 -0
- package/types/isDate.d.ts +25 -0
- package/types/isEmpty.d.ts +40 -0
- package/types/isFloat.d.ts +1 -0
- package/types/isFunction.d.ts +2 -0
- package/types/isInt.d.ts +1 -0
- package/types/isIntArray.d.ts +1 -0
- package/types/isNil.d.ts +20 -0
- package/types/isNumber.d.ts +29 -0
- package/types/isNumberArray.d.ts +1 -0
- package/types/isObject.d.ts +22 -0
- package/types/isObjectLike.d.ts +1 -0
- package/types/isPlainObject.d.ts +27 -0
- package/types/isPrimitive.d.ts +33 -0
- package/types/isPromise.d.ts +1 -0
- package/types/isRegExp.d.ts +23 -0
- package/types/isString.d.ts +22 -0
- package/types/isStringArray.d.ts +1 -0
- package/types/isSymbol.d.ts +1 -0
- 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 {};
|
package/README.md
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
# utils
|
package/array/chunk.d.ts
ADDED
|
@@ -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[];
|
package/array/index.d.ts
ADDED
|
@@ -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;
|
package/async/index.d.ts
ADDED
|
@@ -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>;
|
package/async/sleep.d.ts
ADDED
|
@@ -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,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,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;
|
package/date/index.d.ts
ADDED
|
@@ -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,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,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;
|