@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/core.d.cts
ADDED
|
@@ -0,0 +1,369 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 通用错误
|
|
3
|
+
*/
|
|
4
|
+
declare class CommonError extends Error {
|
|
5
|
+
rawError?: Error;
|
|
6
|
+
constructor(message: string, rawError?: unknown);
|
|
7
|
+
toString(): string;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* 业务执行结果
|
|
12
|
+
*/
|
|
13
|
+
declare class BizResult<T> {
|
|
14
|
+
success: boolean;
|
|
15
|
+
msg: string;
|
|
16
|
+
data?: T;
|
|
17
|
+
constructor(success: boolean, msg: string, data?: T);
|
|
18
|
+
static createSuccess<T>(data: T): BizResult<T>;
|
|
19
|
+
static createFail<T>(msg?: string, data?: T): BizResult<T>;
|
|
20
|
+
static createError<T>(e: unknown): BizResult<T>;
|
|
21
|
+
/**
|
|
22
|
+
* 展开成Promise
|
|
23
|
+
*/
|
|
24
|
+
toPromise(): Promise<void>;
|
|
25
|
+
toPromise<T>(): Promise<T>;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* 执行业务(自动包裹BuResult)
|
|
30
|
+
* @param run 执行方法
|
|
31
|
+
*/
|
|
32
|
+
declare const execBiz: <T>(run: () => Promise<T>) => Promise<BizResult<T>>;
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* 提取getter属性
|
|
36
|
+
*/
|
|
37
|
+
type ExtractGetterProperties<T> = {
|
|
38
|
+
[K in keyof T as K extends `get${infer Rest}` ? Uncapitalize<Rest> : never]: T[K] extends (...args: any[]) => Promise<infer R> ? R : never;
|
|
39
|
+
};
|
|
40
|
+
/**
|
|
41
|
+
* 至少包含一个字段
|
|
42
|
+
*/
|
|
43
|
+
type RequireAtLeastOne<T, Keys extends keyof T = keyof T> = Pick<T, Exclude<keyof T, Keys>> & {
|
|
44
|
+
[K in Keys]: Required<Pick<T, K>> & Partial<Pick<T, Exclude<Keys, K>>>;
|
|
45
|
+
}[Keys];
|
|
46
|
+
/**
|
|
47
|
+
* 用元组限制长度
|
|
48
|
+
*/
|
|
49
|
+
type MaxLengthArray<T, N extends number> = T[] & {
|
|
50
|
+
length: N;
|
|
51
|
+
};
|
|
52
|
+
/**
|
|
53
|
+
* 提取 T 中所有值类型为 V 的键
|
|
54
|
+
*/
|
|
55
|
+
type KeysMatching<T, V> = {
|
|
56
|
+
[K in keyof T]: T[K] extends V ? K : never;
|
|
57
|
+
}[keyof T];
|
|
58
|
+
/**
|
|
59
|
+
* 任意方法
|
|
60
|
+
*/
|
|
61
|
+
type AnyFn = (...args: any[]) => any;
|
|
62
|
+
/**
|
|
63
|
+
* 提取所有方法
|
|
64
|
+
*/
|
|
65
|
+
type MethodKeys<T> = {
|
|
66
|
+
[K in keyof T]-?: T[K] extends AnyFn ? K : never;
|
|
67
|
+
}[keyof T];
|
|
68
|
+
/**
|
|
69
|
+
* 方法参数类型
|
|
70
|
+
*/
|
|
71
|
+
type MethodParams<T> = T extends (...args: infer P) => any ? P : never;
|
|
72
|
+
/**
|
|
73
|
+
* 方法返回类型
|
|
74
|
+
*/
|
|
75
|
+
type MethodReturn<T> = T extends (...args: any[]) => infer R ? R : never;
|
|
76
|
+
/**
|
|
77
|
+
* 提取所有函数 key
|
|
78
|
+
*/
|
|
79
|
+
type FunctionKeys<T> = {
|
|
80
|
+
[K in keyof T]: T[K] extends AnyFn ? K : never;
|
|
81
|
+
}[keyof T];
|
|
82
|
+
/**
|
|
83
|
+
* 拥有指定键集合的对象类型
|
|
84
|
+
*/
|
|
85
|
+
type ObjectWithKeys<K extends string | symbol> = {
|
|
86
|
+
[key in K]: any;
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* 物理像素的区域数据
|
|
91
|
+
*/
|
|
92
|
+
interface PhysicalRect {
|
|
93
|
+
x: number;
|
|
94
|
+
y: number;
|
|
95
|
+
width: number;
|
|
96
|
+
height: number;
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* 物理像素的点位
|
|
100
|
+
*/
|
|
101
|
+
interface PhysicalPoint {
|
|
102
|
+
x: number;
|
|
103
|
+
y: number;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* 是否为空数组
|
|
108
|
+
* @param data
|
|
109
|
+
*/
|
|
110
|
+
declare function isEmptyArr(data: unknown): boolean;
|
|
111
|
+
/**
|
|
112
|
+
* 将数组分块为指定大小的多个子数组
|
|
113
|
+
* @param arr 原数组
|
|
114
|
+
* @param size 每个块的长度
|
|
115
|
+
* @example chunk([1,2,3,4,5], 2) => [[1,2],[3,4],[5]]
|
|
116
|
+
*/
|
|
117
|
+
declare function chunk<T>(arr: T[], size: number): T[][];
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* 是否为取消操作的错误
|
|
121
|
+
*/
|
|
122
|
+
declare function isCanceledError(err: unknown): boolean;
|
|
123
|
+
/**
|
|
124
|
+
* 创建取消错误
|
|
125
|
+
* @param msg
|
|
126
|
+
*/
|
|
127
|
+
declare function createAbortError(msg?: string): Error;
|
|
128
|
+
/**
|
|
129
|
+
* 是否为通用错误对象
|
|
130
|
+
*/
|
|
131
|
+
declare function isCommonError(error: unknown): error is CommonError;
|
|
132
|
+
/**
|
|
133
|
+
* 转换到通用异常
|
|
134
|
+
*/
|
|
135
|
+
declare function convertToCommonError(error: unknown, prefix?: string): Error;
|
|
136
|
+
/**
|
|
137
|
+
* 获取异常信息
|
|
138
|
+
*/
|
|
139
|
+
declare function getErrorMessage(error: unknown): string;
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* 用于指定 `fetch` 响应体的解析方式。
|
|
143
|
+
* 对应 `Response` 对象的不同解析方法(`.json()`、`.text()` 等)。
|
|
144
|
+
*/
|
|
145
|
+
type ResponseBodyFormat = 'json' | 'text' | 'blob' | 'arrayBuffer' | 'formData';
|
|
146
|
+
/**
|
|
147
|
+
* 创建响应错误
|
|
148
|
+
*/
|
|
149
|
+
declare function createResponseError(response: Response): Error;
|
|
150
|
+
/**
|
|
151
|
+
* 使用指定的响应解析格式发起 fetch 请求
|
|
152
|
+
*/
|
|
153
|
+
declare function fetchWithFormat<T = any>(url: string, format: ResponseBodyFormat, init?: RequestInit): Promise<T>;
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* 随机运行参数
|
|
157
|
+
* @param fns
|
|
158
|
+
* @param maxCount 最多执行次数(1 ~ fns.length)
|
|
159
|
+
*/
|
|
160
|
+
declare function runRandomFunctions(fns: Array<() => void>, maxCount?: number): void;
|
|
161
|
+
/**
|
|
162
|
+
* 将数字范围按批次处理,每个批次调用一次异步函数
|
|
163
|
+
*
|
|
164
|
+
* @param start - 起始值(包含)
|
|
165
|
+
* @param end - 结束值(包含)
|
|
166
|
+
* @param batchSize - 每批最大元素个数
|
|
167
|
+
* @param processor - 处理单批数字数组的异步函数
|
|
168
|
+
*
|
|
169
|
+
* @example
|
|
170
|
+
* // 分批处理 1..10,每批最多 3 个数字
|
|
171
|
+
* await processRangeInBatches(1, 10, 3, async (batch) => {
|
|
172
|
+
* console.log(batch) // 输出: [1,2,3], [4,5,6], [7,8,9], [10]
|
|
173
|
+
* })
|
|
174
|
+
*/
|
|
175
|
+
declare function processRangeInBatches(start: number, end: number, batchSize: number, processor: (batch: number[]) => Promise<void>): Promise<void>;
|
|
176
|
+
/**
|
|
177
|
+
* 动态调用
|
|
178
|
+
* @param root 对象
|
|
179
|
+
* @param path 调用路径
|
|
180
|
+
* @param args 参数
|
|
181
|
+
*/
|
|
182
|
+
declare function dynamicCall<T = any>(root: any, path: string, ...args: any[]): T;
|
|
183
|
+
|
|
184
|
+
/** github 仓库信息 */
|
|
185
|
+
interface GitHubRepo {
|
|
186
|
+
/** 仓库所有者(用户或组织名) */
|
|
187
|
+
owner: string;
|
|
188
|
+
/** 仓库名称 */
|
|
189
|
+
repo: string;
|
|
190
|
+
}
|
|
191
|
+
/** github 仓库分支信息 */
|
|
192
|
+
type GitHubRepoBranch = GitHubRepo & {
|
|
193
|
+
/** 分支名称 */
|
|
194
|
+
branch: string;
|
|
195
|
+
};
|
|
196
|
+
/** github 仓库文件信息 */
|
|
197
|
+
type GitHubRepoFile = GitHubRepoBranch & {
|
|
198
|
+
/** 文件路径 */
|
|
199
|
+
filePath: string;
|
|
200
|
+
};
|
|
201
|
+
/**
|
|
202
|
+
* 从 url 解析 github 仓库信息
|
|
203
|
+
* @param repositoryUrl 仓库 URL(支持带有git+前缀、具体文件路径)
|
|
204
|
+
*/
|
|
205
|
+
declare function parseGithubRepoUrl(repositoryUrl: string): GitHubRepoBranch;
|
|
206
|
+
/**
|
|
207
|
+
* 解析 github 仓库文件的原始内容的 url
|
|
208
|
+
*/
|
|
209
|
+
declare function parseGithubRawUrl(gitHubRepoFile: GitHubRepoFile): string;
|
|
210
|
+
/**
|
|
211
|
+
* 获取 github 仓库文件的 json 内容并解析为对象 T
|
|
212
|
+
* @param gitHubRepoFile
|
|
213
|
+
*/
|
|
214
|
+
declare function getGithubRawJson<O>(gitHubRepoFile: GitHubRepoFile): Promise<O>;
|
|
215
|
+
|
|
216
|
+
/**
|
|
217
|
+
* 数组洗牌
|
|
218
|
+
*/
|
|
219
|
+
declare function shuffle<T>(arr: T[]): T[];
|
|
220
|
+
/**
|
|
221
|
+
* 获取 [min, max] 的随机整数
|
|
222
|
+
*/
|
|
223
|
+
declare function randomInt(min: number, max: number): number;
|
|
224
|
+
/**
|
|
225
|
+
* 获取 [min, max) 的随机浮点数
|
|
226
|
+
*/
|
|
227
|
+
declare function randomFloat(min: number, max: number): number;
|
|
228
|
+
/**
|
|
229
|
+
* 从数组中随机取若干项,不可重复
|
|
230
|
+
*/
|
|
231
|
+
declare function sample<T>(arr: T[], count: number): T[];
|
|
232
|
+
/**
|
|
233
|
+
* 从数组中随机取若干项,可重复
|
|
234
|
+
*/
|
|
235
|
+
declare function sampleWithReplacement<T>(arr: T[], count: number): T[];
|
|
236
|
+
/**
|
|
237
|
+
* 从数组中获取随机项
|
|
238
|
+
*/
|
|
239
|
+
declare function sampleOne<T>(arr: T[]): T;
|
|
240
|
+
/**
|
|
241
|
+
* 获取 1~n 的随机数
|
|
242
|
+
*/
|
|
243
|
+
declare function random1ToN(n: number): number;
|
|
244
|
+
|
|
245
|
+
/**
|
|
246
|
+
* 递归移除对象(或数组)中的所有函数属性,返回一个可安全序列化的副本。
|
|
247
|
+
*
|
|
248
|
+
* @typeParam T - 输入值的类型。
|
|
249
|
+
* @param value - 待处理的值(基本类型、对象、数组等)。
|
|
250
|
+
* @param seen - 内部使用的 WeakMap,用于记录已访问过的对象,防止循环引用。
|
|
251
|
+
* 调用方通常无需传递此参数。
|
|
252
|
+
* @returns 处理后的新副本,其中所有函数属性都被移除。
|
|
253
|
+
* 基本类型(string, number, boolean, null, undefined)将原样返回。
|
|
254
|
+
*/
|
|
255
|
+
declare function stripFunctions<T>(value: T, seen?: WeakMap<object, any>): T;
|
|
256
|
+
/**
|
|
257
|
+
* 将错误转换为可 JSON 序列化的普通对象。
|
|
258
|
+
*/
|
|
259
|
+
declare function serializeError(err: unknown): {
|
|
260
|
+
name: string;
|
|
261
|
+
message: string;
|
|
262
|
+
stack: string | undefined;
|
|
263
|
+
} | {
|
|
264
|
+
name: string;
|
|
265
|
+
message: string;
|
|
266
|
+
stack?: undefined;
|
|
267
|
+
};
|
|
268
|
+
|
|
269
|
+
/**
|
|
270
|
+
* 支持取消的 sleep
|
|
271
|
+
* @param ms 延迟毫秒数
|
|
272
|
+
* @param signal 取消信号
|
|
273
|
+
*/
|
|
274
|
+
declare function sleep(ms: number, signal?: AbortSignal): Promise<void>;
|
|
275
|
+
/**
|
|
276
|
+
* 支持取消的随机 sleep
|
|
277
|
+
* @param minMS 最小延迟毫秒数
|
|
278
|
+
* @param maxMS 最大延迟毫秒数
|
|
279
|
+
* @param signal 取消信号
|
|
280
|
+
*/
|
|
281
|
+
declare function sleepRandom(minMS: number, maxMS: number, signal?: AbortSignal): Promise<void>;
|
|
282
|
+
|
|
283
|
+
/**
|
|
284
|
+
* 格式化日期
|
|
285
|
+
*/
|
|
286
|
+
declare function formatDate(date: Date, pattern: 'date' | 'time' | 'datetime'): string;
|
|
287
|
+
/**
|
|
288
|
+
* 格式化时间为 2020-02-02 20:20:20 的字符串
|
|
289
|
+
* @param date 需要格式化的时间,为空则获取当前时间
|
|
290
|
+
*/
|
|
291
|
+
declare function getFormattedDateTime(date?: Date): string;
|
|
292
|
+
/**
|
|
293
|
+
* 格式化时间为 2020-02-02 20:20:20 的字符串
|
|
294
|
+
* @param date 需要格式化的时间,为空则获取当前时间
|
|
295
|
+
*/
|
|
296
|
+
declare function getFormattedDate(date?: Date): string;
|
|
297
|
+
/**
|
|
298
|
+
* 格式化时间为 20:20:20 的字符串
|
|
299
|
+
* @param date 需要格式化的时间,为空则获取当前时间
|
|
300
|
+
*/
|
|
301
|
+
declare function getFormattedTime(date?: Date): string;
|
|
302
|
+
|
|
303
|
+
/**
|
|
304
|
+
* 判断一个值是否为纯对象
|
|
305
|
+
*/
|
|
306
|
+
declare function isPlainObject(value: unknown): value is Record<string, unknown>;
|
|
307
|
+
/**
|
|
308
|
+
* 包括类实例、普通对象,不包括 数组、null 和原始类型
|
|
309
|
+
*/
|
|
310
|
+
declare function isObject<K extends string | symbol>(value: unknown, key?: K): value is ObjectWithKeys<K>;
|
|
311
|
+
|
|
312
|
+
/** URL query 中必须编码的保留字符 (RFC 3986) */
|
|
313
|
+
declare const invalidCharRegex: RegExp;
|
|
314
|
+
/** 查询参数 */
|
|
315
|
+
type QueryParams = Record<string, string | number | boolean | null | undefined>;
|
|
316
|
+
/**
|
|
317
|
+
* 已解析的 url
|
|
318
|
+
*/
|
|
319
|
+
interface ParsedUrl {
|
|
320
|
+
baseUrl: string;
|
|
321
|
+
searchParams: URLSearchParams;
|
|
322
|
+
}
|
|
323
|
+
/**
|
|
324
|
+
* 判断字符串是否为有效的 HTTP/HTTPS URL
|
|
325
|
+
* @param path - 待检测的字符串
|
|
326
|
+
*/
|
|
327
|
+
declare function isHttpUrl(path: string): boolean;
|
|
328
|
+
/**
|
|
329
|
+
* 解析 url
|
|
330
|
+
* @param url
|
|
331
|
+
*/
|
|
332
|
+
declare function parseUrl(url: string): ParsedUrl;
|
|
333
|
+
/**
|
|
334
|
+
* 编码 URL 参数
|
|
335
|
+
* @param params 查询参数
|
|
336
|
+
* @param keepEmptyValues 保留 null/undefined的值(空字符串)
|
|
337
|
+
*/
|
|
338
|
+
declare function encodeURLParams(params: QueryParams, keepEmptyValues?: boolean): string;
|
|
339
|
+
/**
|
|
340
|
+
*
|
|
341
|
+
*/
|
|
342
|
+
/**
|
|
343
|
+
* 合并查询参数
|
|
344
|
+
* @param urlSearchParams url解析后查询参数
|
|
345
|
+
* @param params 单独传的查询参数
|
|
346
|
+
*/
|
|
347
|
+
declare function mergeQueryParams(urlSearchParams: URLSearchParams, params?: QueryParams): QueryParams;
|
|
348
|
+
|
|
349
|
+
/**
|
|
350
|
+
* 格式化后的单位数据(如文件大小)
|
|
351
|
+
*/
|
|
352
|
+
interface FormattedUnitSize {
|
|
353
|
+
/** 换算后该单位的数值(未四舍五入) */
|
|
354
|
+
size: number;
|
|
355
|
+
/** 单位名称,如 'MB' */
|
|
356
|
+
unit: string;
|
|
357
|
+
/** 显示文本:数值四舍五入保留两位小数 + 单位,如 '1.50 MB' */
|
|
358
|
+
text: string;
|
|
359
|
+
}
|
|
360
|
+
/**
|
|
361
|
+
* 按进制自动格式化单位
|
|
362
|
+
* @param value 数值
|
|
363
|
+
* @param base 进制(1024 / 1000)
|
|
364
|
+
* @param units 单位列表
|
|
365
|
+
* @param invalidText 无效值返回文本
|
|
366
|
+
*/
|
|
367
|
+
declare function formatUnitSize(value: number, base: number, units: string[], invalidText?: string): FormattedUnitSize;
|
|
368
|
+
|
|
369
|
+
export { type AnyFn, BizResult, CommonError, type ExtractGetterProperties, type FormattedUnitSize, type FunctionKeys, type GitHubRepo, type GitHubRepoBranch, type GitHubRepoFile, type KeysMatching, type MaxLengthArray, type MethodKeys, type MethodParams, type MethodReturn, type ObjectWithKeys, type ParsedUrl, type PhysicalPoint, type PhysicalRect, type QueryParams, type RequireAtLeastOne, type ResponseBodyFormat, chunk, convertToCommonError, createAbortError, createResponseError, dynamicCall, encodeURLParams, execBiz, fetchWithFormat, formatDate, formatUnitSize, getErrorMessage, getFormattedDate, getFormattedDateTime, getFormattedTime, getGithubRawJson, invalidCharRegex, isCanceledError, isCommonError, isEmptyArr, isHttpUrl, isObject, isPlainObject, mergeQueryParams, parseGithubRawUrl, parseGithubRepoUrl, parseUrl, processRangeInBatches, random1ToN, randomFloat, randomInt, runRandomFunctions, sample, sampleOne, sampleWithReplacement, serializeError, shuffle, sleep, sleepRandom, stripFunctions };
|