@ybgnb/utils 0.1.7 → 0.1.9
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/LICENSE +1 -1
- package/dist/core.cjs +504 -0
- package/dist/core.cjs.map +1 -0
- package/dist/core.d.cts +369 -0
- package/dist/core.d.ts +369 -0
- package/dist/core.js +437 -0
- package/dist/core.js.map +1 -0
- package/dist/dom.cjs +263 -0
- package/dist/dom.cjs.map +1 -0
- package/dist/dom.d.cts +105 -0
- package/dist/dom.d.ts +105 -0
- package/dist/dom.js +225 -0
- package/dist/dom.js.map +1 -0
- package/dist/node.cjs +410 -0
- package/dist/node.cjs.map +1 -0
- package/dist/node.d.cts +153 -0
- package/dist/node.d.ts +153 -0
- package/dist/node.js +350 -0
- package/dist/node.js.map +1 -0
- package/package.json +26 -15
- package/dist/index.d.ts +0 -444
- package/dist/index.js +0 -388
- package/dist/index.js.map +0 -1
- package/dist/index.umd.cjs +0 -2
- package/dist/index.umd.cjs.map +0 -1
package/dist/index.d.ts
DELETED
|
@@ -1,444 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* 中止异常
|
|
3
|
-
*/
|
|
4
|
-
export declare class AbortError extends CommonError {
|
|
5
|
-
constructor(message?: string);
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* 任意方法
|
|
10
|
-
*/
|
|
11
|
-
export declare type AnyFn = (...args: any[]) => any;
|
|
12
|
-
|
|
13
|
-
/**
|
|
14
|
-
* 业务执行结果
|
|
15
|
-
*/
|
|
16
|
-
export declare class BizResult<T> {
|
|
17
|
-
success: boolean;
|
|
18
|
-
msg: string;
|
|
19
|
-
data?: T;
|
|
20
|
-
constructor(success: boolean, msg: string, data?: T);
|
|
21
|
-
static createSuccess<T>(data: T): BizResult<T>;
|
|
22
|
-
static createFail<T>(msg?: string, data?: T): BizResult<T>;
|
|
23
|
-
static createError<T>(e: unknown): BizResult<T>;
|
|
24
|
-
/**
|
|
25
|
-
* 展开成Promise
|
|
26
|
-
*/
|
|
27
|
-
toPromise(): Promise<void>;
|
|
28
|
-
toPromise<T>(): Promise<T>;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
/**
|
|
32
|
-
* 将数组分块为指定大小的多个子数组
|
|
33
|
-
* @param arr 原数组
|
|
34
|
-
* @param size 每个块的长度
|
|
35
|
-
* @example chunk([1,2,3,4,5], 2) => [[1,2],[3,4],[5]]
|
|
36
|
-
*/
|
|
37
|
-
export declare function chunk<T>(arr: T[], size: number): T[][];
|
|
38
|
-
|
|
39
|
-
/**
|
|
40
|
-
* 通用错误
|
|
41
|
-
*/
|
|
42
|
-
export declare class CommonError extends Error {
|
|
43
|
-
rawError?: Error;
|
|
44
|
-
constructor(message: string, rawError?: unknown);
|
|
45
|
-
toString(): string;
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
/**
|
|
49
|
-
* 转换到通用异常
|
|
50
|
-
*/
|
|
51
|
-
export declare function convertToCommonError(error: unknown, prefix?: string): CommonError;
|
|
52
|
-
|
|
53
|
-
/**
|
|
54
|
-
* 动态调用
|
|
55
|
-
* @param root 对象
|
|
56
|
-
* @param path 调用路径
|
|
57
|
-
* @param args 参数
|
|
58
|
-
*/
|
|
59
|
-
export declare function dynamicCall<T = any>(root: any, path: string, ...args: any[]): T;
|
|
60
|
-
|
|
61
|
-
/**
|
|
62
|
-
* 编码 URL 参数
|
|
63
|
-
* @param params 查询参数
|
|
64
|
-
*/
|
|
65
|
-
export declare function encodeURLParams(params: QueryParams): string;
|
|
66
|
-
|
|
67
|
-
/**
|
|
68
|
-
* 执行业务(自动包裹BuResult)
|
|
69
|
-
* @param run 执行方法
|
|
70
|
-
*/
|
|
71
|
-
export declare const execBiz: <T>(run: () => Promise<T>) => Promise<BizResult<T>>;
|
|
72
|
-
|
|
73
|
-
/**
|
|
74
|
-
* 提取getter属性
|
|
75
|
-
*/
|
|
76
|
-
export declare type ExtractGetterProperties<T> = {
|
|
77
|
-
[K in keyof T as K extends `get${infer Rest}` ? Uncapitalize<Rest> : never]: T[K] extends (...args: any[]) => Promise<infer R> ? R : never;
|
|
78
|
-
};
|
|
79
|
-
|
|
80
|
-
/**
|
|
81
|
-
* 提取所有函数 key
|
|
82
|
-
*/
|
|
83
|
-
export declare type FunctionKeys<T> = {
|
|
84
|
-
[K in keyof T]: T[K] extends AnyFn ? K : never;
|
|
85
|
-
}[keyof T];
|
|
86
|
-
|
|
87
|
-
/**
|
|
88
|
-
* 获取异常信息
|
|
89
|
-
*/
|
|
90
|
-
export declare function getErrorMessage(error: unknown): string;
|
|
91
|
-
|
|
92
|
-
/**
|
|
93
|
-
* 格式化时间为 2020-02-02 20:20:20 的字符串
|
|
94
|
-
* @param date 需要格式化的时间,为空则获取当前时间
|
|
95
|
-
*/
|
|
96
|
-
export declare function getFormattedDate(date?: Date): string;
|
|
97
|
-
|
|
98
|
-
/**
|
|
99
|
-
* 格式化时间为 2020-02-02 20:20:20 的字符串
|
|
100
|
-
* @param date 需要格式化的时间,为空则获取当前时间
|
|
101
|
-
*/
|
|
102
|
-
export declare function getFormattedDateTime(date?: Date): string;
|
|
103
|
-
|
|
104
|
-
/**
|
|
105
|
-
* 格式化时间为 20:20:20 的字符串
|
|
106
|
-
* @param date 需要格式化的时间,为空则获取当前时间
|
|
107
|
-
*/
|
|
108
|
-
export declare function getFormattedTime(date?: Date): string;
|
|
109
|
-
|
|
110
|
-
/**
|
|
111
|
-
* 从 <img> 元素获取图片字节数据(ArrayBuffer)
|
|
112
|
-
* @param img 已加载的 <img> 元素
|
|
113
|
-
* @param type 图片类型,可选(默认 png)
|
|
114
|
-
*/
|
|
115
|
-
export declare function getImageArrayBuffer(img: HTMLImageElement, type?: string): Promise<ArrayBuffer>;
|
|
116
|
-
|
|
117
|
-
/**
|
|
118
|
-
* 等待加载完成并获取字节数据
|
|
119
|
-
*/
|
|
120
|
-
export declare function getImgArrayBufferAfterLoad(img: HTMLImageElement, type?: string): Promise<ArrayBuffer>;
|
|
121
|
-
|
|
122
|
-
/**
|
|
123
|
-
* 获取网络信息
|
|
124
|
-
*/
|
|
125
|
-
export declare function getNetworkInfo(): NetworkState;
|
|
126
|
-
|
|
127
|
-
/** github 仓库信息 */
|
|
128
|
-
export declare interface GithubRepository {
|
|
129
|
-
/** 仓库所有者(用户或组织名) */
|
|
130
|
-
owner: string;
|
|
131
|
-
/** 仓库名称 */
|
|
132
|
-
repo: string;
|
|
133
|
-
/** 分支名称 */
|
|
134
|
-
branch: string;
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
/**
|
|
138
|
-
* 将十六进制颜色转换为 RGB
|
|
139
|
-
*/
|
|
140
|
-
export declare const hexToRgb: (hex: string) => {
|
|
141
|
-
r: number;
|
|
142
|
-
g: number;
|
|
143
|
-
b: number;
|
|
144
|
-
};
|
|
145
|
-
|
|
146
|
-
/**
|
|
147
|
-
* 滚动元素到屏幕中心
|
|
148
|
-
* @param el
|
|
149
|
-
*/
|
|
150
|
-
export declare function humanScrollElIntoCenter(el: HTMLElement): Promise<void>;
|
|
151
|
-
|
|
152
|
-
/**
|
|
153
|
-
* 模拟人类手感滚动到指定位置
|
|
154
|
-
*/
|
|
155
|
-
export declare function humanScrollTo(targetX: number, targetY: number): Promise<void>;
|
|
156
|
-
|
|
157
|
-
/** URL query 中必须编码的保留字符 (RFC 3986) */
|
|
158
|
-
export declare const invalidCharRegex: RegExp;
|
|
159
|
-
|
|
160
|
-
export declare function isAbortError(error: unknown): error is AbortError;
|
|
161
|
-
|
|
162
|
-
export declare function isCommonError(error: unknown): error is CommonError;
|
|
163
|
-
|
|
164
|
-
/**
|
|
165
|
-
* 是否为空数组
|
|
166
|
-
* @param data
|
|
167
|
-
*/
|
|
168
|
-
export declare function isEmptyArr(data: unknown): boolean;
|
|
169
|
-
|
|
170
|
-
/**
|
|
171
|
-
* 判断字符串是否为有效的 HTTP/HTTPS URL
|
|
172
|
-
* @param path - 待检测的字符串
|
|
173
|
-
*/
|
|
174
|
-
export declare function isHttpUrl(path: string): boolean;
|
|
175
|
-
|
|
176
|
-
/**
|
|
177
|
-
* 判断一个值是否为纯对象
|
|
178
|
-
*/
|
|
179
|
-
export declare function isPlainObject(value: unknown): value is Record<string, unknown>;
|
|
180
|
-
|
|
181
|
-
/**
|
|
182
|
-
* 提取 T 中所有值类型为 V 的键
|
|
183
|
-
*/
|
|
184
|
-
export declare type KeysMatching<T, V> = {
|
|
185
|
-
[K in keyof T]: T[K] extends V ? K : never;
|
|
186
|
-
}[keyof T];
|
|
187
|
-
|
|
188
|
-
/**
|
|
189
|
-
* 用元组限制长度
|
|
190
|
-
*/
|
|
191
|
-
export declare type MaxLengthArray<T, N extends number> = T[] & {
|
|
192
|
-
length: N;
|
|
193
|
-
};
|
|
194
|
-
|
|
195
|
-
/**
|
|
196
|
-
*
|
|
197
|
-
*/
|
|
198
|
-
/**
|
|
199
|
-
* 合并查询参数
|
|
200
|
-
* @param urlSearchParams url解析后查询参数
|
|
201
|
-
* @param params 单独传的查询参数
|
|
202
|
-
*/
|
|
203
|
-
export declare function mergeQueryParams(urlSearchParams: URLSearchParams, params?: QueryParams): QueryParams;
|
|
204
|
-
|
|
205
|
-
/**
|
|
206
|
-
* 提取所有方法
|
|
207
|
-
*/
|
|
208
|
-
export declare type MethodKeys<T> = {
|
|
209
|
-
[K in keyof T]-?: T[K] extends AnyFn ? K : never;
|
|
210
|
-
}[keyof T];
|
|
211
|
-
|
|
212
|
-
/**
|
|
213
|
-
* 方法参数类型
|
|
214
|
-
*/
|
|
215
|
-
export declare type MethodParams<T> = T extends (...args: infer P) => any ? P : never;
|
|
216
|
-
|
|
217
|
-
/**
|
|
218
|
-
* 方法返回类型
|
|
219
|
-
*/
|
|
220
|
-
export declare type MethodReturn<T> = T extends (...args: any[]) => infer R ? R : never;
|
|
221
|
-
|
|
222
|
-
/**
|
|
223
|
-
* 生成 CSS `color-mix()` 函数的字符串表示,用于动态计算两种颜色的混合结果
|
|
224
|
-
*
|
|
225
|
-
* @param {string} color1 - 参与混合的第一种颜色(支持 CSS 合法颜色值,如 HEX、RGB、HSL 等)
|
|
226
|
-
* @param {string} color2 - 参与混合的第二种颜色
|
|
227
|
-
* @param {number} percentage - 主颜色(color1)在混合中的占比(百分比数值,范围 0-100)
|
|
228
|
-
* @param {'srgb' | 'hsl'} [colorSpace='srgb'] - 色彩空间选项:
|
|
229
|
-
* - `'srgb'`: 标准 RGB 色彩空间(默认)
|
|
230
|
-
* - `'hsl'`: 色相-饱和度-明度色彩空间
|
|
231
|
-
* @param {number} [percentage2] - 可选参数:副颜色(color2)的独立占比。
|
|
232
|
-
* 若未提供,则自动计算为 `100 - percentage`
|
|
233
|
-
* @returns {string} 可直接用于 CSS 的 `color-mix()` 函数字符串(如 `color-mix(in srgb, red 30%, blue 70%)`)
|
|
234
|
-
*
|
|
235
|
-
* @example
|
|
236
|
-
* // 基础用法(自动计算互补占比)
|
|
237
|
-
* mixColor('red', 'blue', 30)
|
|
238
|
-
* // 返回: "color-mix(in srgb, red 30%, blue 70%)"
|
|
239
|
-
*
|
|
240
|
-
* @example
|
|
241
|
-
* // 显式指定双占比 + HSL 色彩空间
|
|
242
|
-
* mixColor('#ff0000', '#0000ff', 40, 'hsl', 60)
|
|
243
|
-
* // 返回: "color-mix(in hsl, #ff0000 40%, #0000ff 60%)"
|
|
244
|
-
*/
|
|
245
|
-
export declare const mixColor: (color1: string, color2: string, percentage: number, colorSpace?: "srgb" | "hsl", percentage2?: number) => string;
|
|
246
|
-
|
|
247
|
-
/**
|
|
248
|
-
* 网络信息
|
|
249
|
-
*/
|
|
250
|
-
export declare interface NetworkInfo extends EventTarget {
|
|
251
|
-
downlink: number;
|
|
252
|
-
rtt: number;
|
|
253
|
-
effectiveType: 'slow-2g' | '2g' | '3g' | '4g';
|
|
254
|
-
}
|
|
255
|
-
|
|
256
|
-
/**
|
|
257
|
-
* 网络状态
|
|
258
|
-
* - 'offline': 无网络
|
|
259
|
-
* - 'online': 在线但不支持 NetworkInformation API
|
|
260
|
-
* - NetworkInformation: 含详细网络信息
|
|
261
|
-
*/
|
|
262
|
-
export declare type NetworkState = 'offline' | Omit<NetworkInfo, keyof EventTarget> | 'online';
|
|
263
|
-
|
|
264
|
-
/**
|
|
265
|
-
* 监听网络状态变化
|
|
266
|
-
* @param listener 监听器
|
|
267
|
-
* @returns 成功监听时返回解绑函数
|
|
268
|
-
*/
|
|
269
|
-
export declare function onNetworkChange(listener: (info: NetworkState) => void): () => void;
|
|
270
|
-
|
|
271
|
-
/**
|
|
272
|
-
* 监听页面可见性变化(兼容旧的IE/Chrome)
|
|
273
|
-
* @param listener 监听器
|
|
274
|
-
* @returns 成功监听时返回解绑函数
|
|
275
|
-
*/
|
|
276
|
-
export declare function onVisibilityChange(listener: VisibilityChangeListener): (() => void) | null;
|
|
277
|
-
|
|
278
|
-
/**
|
|
279
|
-
* 已解析的 url
|
|
280
|
-
*/
|
|
281
|
-
export declare interface ParsedUrl {
|
|
282
|
-
baseUrl: string;
|
|
283
|
-
searchParams: URLSearchParams;
|
|
284
|
-
}
|
|
285
|
-
|
|
286
|
-
/**
|
|
287
|
-
* 从 url 解析 github 仓库信息
|
|
288
|
-
* @param repositoryUrl 仓库 URL(支持带有git+前缀、具体文件路径)
|
|
289
|
-
*/
|
|
290
|
-
export declare function parseGithubRepo(repositoryUrl: string): GithubRepository;
|
|
291
|
-
|
|
292
|
-
/**
|
|
293
|
-
* 解析 url
|
|
294
|
-
* @param url
|
|
295
|
-
*/
|
|
296
|
-
export declare function parseUrl(url: string): ParsedUrl;
|
|
297
|
-
|
|
298
|
-
/**
|
|
299
|
-
* 物理像素的点位
|
|
300
|
-
*/
|
|
301
|
-
export declare interface PhysicalPoint {
|
|
302
|
-
x: number;
|
|
303
|
-
y: number;
|
|
304
|
-
}
|
|
305
|
-
|
|
306
|
-
/**
|
|
307
|
-
* 物理像素的区域数据
|
|
308
|
-
*/
|
|
309
|
-
export declare interface PhysicalRect {
|
|
310
|
-
x: number;
|
|
311
|
-
y: number;
|
|
312
|
-
width: number;
|
|
313
|
-
height: number;
|
|
314
|
-
}
|
|
315
|
-
|
|
316
|
-
/**
|
|
317
|
-
* 将数字范围按批次处理,每个批次调用一次异步函数
|
|
318
|
-
*
|
|
319
|
-
* @param start - 起始值(包含)
|
|
320
|
-
* @param end - 结束值(包含)
|
|
321
|
-
* @param batchSize - 每批最大元素个数
|
|
322
|
-
* @param processor - 处理单批数字数组的异步函数
|
|
323
|
-
*
|
|
324
|
-
* @example
|
|
325
|
-
* // 分批处理 1..10,每批最多 3 个数字
|
|
326
|
-
* await processRangeInBatches(1, 10, 3, async (batch) => {
|
|
327
|
-
* console.log(batch) // 输出: [1,2,3], [4,5,6], [7,8,9], [10]
|
|
328
|
-
* })
|
|
329
|
-
*/
|
|
330
|
-
export declare function processRangeInBatches(start: number, end: number, batchSize: number, processor: (batch: number[]) => Promise<void>): Promise<void>;
|
|
331
|
-
|
|
332
|
-
/** 查询参数 */
|
|
333
|
-
export declare type QueryParams = Record<string, string | number | boolean | null | undefined>;
|
|
334
|
-
|
|
335
|
-
/**
|
|
336
|
-
* 获取 1~n 的随机数
|
|
337
|
-
*/
|
|
338
|
-
export declare function random1ToN(n: number): number;
|
|
339
|
-
|
|
340
|
-
/**
|
|
341
|
-
* 获取 [min, max) 的随机浮点数
|
|
342
|
-
*/
|
|
343
|
-
export declare function randomFloat(min: number, max: number): number;
|
|
344
|
-
|
|
345
|
-
/**
|
|
346
|
-
* 获取 [min, max] 的随机整数
|
|
347
|
-
*/
|
|
348
|
-
export declare function randomInt(min: number, max: number): number;
|
|
349
|
-
|
|
350
|
-
/**
|
|
351
|
-
* 至少包含一个字段
|
|
352
|
-
*/
|
|
353
|
-
export declare type RequireAtLeastOne<T, Keys extends keyof T = keyof T> = Pick<T, Exclude<keyof T, Keys>> & {
|
|
354
|
-
[K in Keys]: Required<Pick<T, K>> & Partial<Pick<T, Exclude<Keys, K>>>;
|
|
355
|
-
}[Keys];
|
|
356
|
-
|
|
357
|
-
/**
|
|
358
|
-
* 随机运行参数
|
|
359
|
-
* @param fns
|
|
360
|
-
* @param maxCount 最多执行次数(1 ~ fns.length)
|
|
361
|
-
*/
|
|
362
|
-
export declare function runRandomFunctions(fns: Array<() => void>, maxCount?: number): void;
|
|
363
|
-
|
|
364
|
-
/**
|
|
365
|
-
* 从数组中随机取若干项,不可重复
|
|
366
|
-
*/
|
|
367
|
-
export declare function sample<T>(arr: T[], count: number): T[];
|
|
368
|
-
|
|
369
|
-
/**
|
|
370
|
-
* 从数组中获取随机项
|
|
371
|
-
*/
|
|
372
|
-
export declare function sampleOne<T>(arr: T[]): T;
|
|
373
|
-
|
|
374
|
-
/**
|
|
375
|
-
* 从数组中随机取若干项,可重复
|
|
376
|
-
*/
|
|
377
|
-
export declare function sampleWithReplacement<T>(arr: T[], count: number): T[];
|
|
378
|
-
|
|
379
|
-
/**
|
|
380
|
-
* 将错误转换为可 JSON 序列化的普通对象。
|
|
381
|
-
*/
|
|
382
|
-
export declare function serializeError(err: unknown): {
|
|
383
|
-
name: string;
|
|
384
|
-
message: string;
|
|
385
|
-
stack: string | undefined;
|
|
386
|
-
} | {
|
|
387
|
-
name: string;
|
|
388
|
-
message: string;
|
|
389
|
-
stack?: undefined;
|
|
390
|
-
};
|
|
391
|
-
|
|
392
|
-
/**
|
|
393
|
-
* 设置 html 根元素的 css 变量
|
|
394
|
-
*/
|
|
395
|
-
export declare const setCssVar: (k: string, v: string) => void;
|
|
396
|
-
|
|
397
|
-
/**
|
|
398
|
-
* 数组洗牌
|
|
399
|
-
*/
|
|
400
|
-
export declare function shuffle<T>(arr: T[]): T[];
|
|
401
|
-
|
|
402
|
-
/**
|
|
403
|
-
* 支持取消的 sleep
|
|
404
|
-
* @param ms 延迟毫秒数
|
|
405
|
-
* @param signal 取消信号
|
|
406
|
-
*/
|
|
407
|
-
export declare function sleep(ms: number, signal?: AbortSignal): Promise<void>;
|
|
408
|
-
|
|
409
|
-
/**
|
|
410
|
-
* 支持取消的随机 sleep
|
|
411
|
-
* @param minMS 最小延迟毫秒数
|
|
412
|
-
* @param maxMS 最大延迟毫秒数
|
|
413
|
-
* @param signal 取消信号
|
|
414
|
-
*/
|
|
415
|
-
export declare function sleepRandom(minMS: number, maxMS: number, signal?: AbortSignal): Promise<void>;
|
|
416
|
-
|
|
417
|
-
/**
|
|
418
|
-
* 递归移除对象(或数组)中的所有函数属性,返回一个可安全序列化的副本。
|
|
419
|
-
*
|
|
420
|
-
* @typeParam T - 输入值的类型。
|
|
421
|
-
* @param value - 待处理的值(基本类型、对象、数组等)。
|
|
422
|
-
* @param seen - 内部使用的 WeakMap,用于记录已访问过的对象,防止循环引用。
|
|
423
|
-
* 调用方通常无需传递此参数。
|
|
424
|
-
* @returns 处理后的新副本,其中所有函数属性都被移除。
|
|
425
|
-
* 基本类型(string, number, boolean, null, undefined)将原样返回。
|
|
426
|
-
*/
|
|
427
|
-
export declare function stripFunctions<T>(value: T, seen?: WeakMap<object, any>): T;
|
|
428
|
-
|
|
429
|
-
/** 可见性改变的监听器 */
|
|
430
|
-
export declare interface VisibilityChangeListener {
|
|
431
|
-
(hidden: boolean): void;
|
|
432
|
-
}
|
|
433
|
-
|
|
434
|
-
/**
|
|
435
|
-
* 等待滚动
|
|
436
|
-
*/
|
|
437
|
-
export declare function waitScrollTo(targetX: number, targetY: number): Promise<void>;
|
|
438
|
-
|
|
439
|
-
/**
|
|
440
|
-
* 等待 <img> 加载完成
|
|
441
|
-
*/
|
|
442
|
-
export declare function whenImageLoaded(img: HTMLImageElement): Promise<HTMLImageElement>;
|
|
443
|
-
|
|
444
|
-
export { }
|