@jiakun-zhao/utils 1.4.2 → 1.4.3
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 +21 -0
- package/README.md +0 -2
- package/dist/index.d.mts +620 -0
- package/dist/index.mjs +558 -1
- package/llms.txt +596 -0
- package/package.json +5 -3
package/dist/index.d.mts
CHANGED
|
@@ -1,10 +1,55 @@
|
|
|
1
1
|
//#region src/addons/assert.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* 断言函数,当条件为 false 时抛出错误
|
|
4
|
+
* @param condition - 断言条件
|
|
5
|
+
* @param message - 错误信息
|
|
6
|
+
* @example
|
|
7
|
+
* ```ts
|
|
8
|
+
* assert(true, '不会抛出错误') // 正常执行
|
|
9
|
+
* assert(false, '会抛出错误') // 抛出 Error: 会抛出错误
|
|
10
|
+
* ```
|
|
11
|
+
*/
|
|
2
12
|
declare function assert(condition: boolean, message: string): asserts condition;
|
|
3
13
|
//#endregion
|
|
4
14
|
//#region src/addons/deep-equal.d.ts
|
|
15
|
+
/**
|
|
16
|
+
* 深度比较两个值是否相等
|
|
17
|
+
* 支持 Array、Object、RegExp、Date 等类型的比较
|
|
18
|
+
* @param a - 第一个值
|
|
19
|
+
* @param b - 第二个值
|
|
20
|
+
* @returns 是否深度相等
|
|
21
|
+
* @example
|
|
22
|
+
* ```ts
|
|
23
|
+
* isDeepEqual(1, 1) // true
|
|
24
|
+
* isDeepEqual({}, {}) // true
|
|
25
|
+
* isDeepEqual('foo', 'foo') // true
|
|
26
|
+
* isDeepEqual([1, 2, 3], [1, 2, 3]) // true
|
|
27
|
+
* isDeepEqual(isDeepEqual, isDeepEqual) // true
|
|
28
|
+
* isDeepEqual(/foo/, /foo/) // true
|
|
29
|
+
* isDeepEqual(null, null) // true
|
|
30
|
+
* isDeepEqual(Number.NaN, Number.NaN) // true
|
|
31
|
+
* isDeepEqual([], []) // true
|
|
32
|
+
* isDeepEqual([{ a: 1 }, [{ b: { c: [1] } }]], [{ a: 1 }, [{ b: { c: [1] } }]]) // true
|
|
33
|
+
* isDeepEqual(1, '1') // false
|
|
34
|
+
* isDeepEqual(null, undefined) // false
|
|
35
|
+
* isDeepEqual({ a: 1, b: [2, 3] }, { a: 1, b: [2, 5] }) // false
|
|
36
|
+
* isDeepEqual(/foo/i, /bar/g) // false
|
|
37
|
+
* ```
|
|
38
|
+
*/
|
|
5
39
|
declare function isDeepEqual(a: any, b: any): boolean;
|
|
6
40
|
//#endregion
|
|
7
41
|
//#region src/addons/http-status-code.d.ts
|
|
42
|
+
/**
|
|
43
|
+
* HTTP 状态码及其对应的描述信息
|
|
44
|
+
* 包含了所有标准的 HTTP 状态码
|
|
45
|
+
* @example
|
|
46
|
+
* ```ts
|
|
47
|
+
* status[200] // 'OK'
|
|
48
|
+
* status[404] // 'Not Found'
|
|
49
|
+
* status[500] // 'Internal Server Error'
|
|
50
|
+
* status[418] // "I'm a Teapot"
|
|
51
|
+
* ```
|
|
52
|
+
*/
|
|
8
53
|
declare const status: {
|
|
9
54
|
readonly 100: "Continue";
|
|
10
55
|
readonly 101: "Switching Protocols";
|
|
@@ -70,73 +115,648 @@ declare const status: {
|
|
|
70
115
|
readonly 510: "Not Extended";
|
|
71
116
|
readonly 511: "Network Authentication Required";
|
|
72
117
|
};
|
|
118
|
+
/**
|
|
119
|
+
* 根据 HTTP 状态码获取对应的描述信息
|
|
120
|
+
* @param code - HTTP 状态码
|
|
121
|
+
* @returns 状态码描述
|
|
122
|
+
* @example
|
|
123
|
+
* ```ts
|
|
124
|
+
* getHttpStatusMessage(200) // 'OK'
|
|
125
|
+
* getHttpStatusMessage(404) // 'Not Found'
|
|
126
|
+
* getHttpStatusMessage(500) // 'Internal Server Error'
|
|
127
|
+
* getHttpStatusMessage(418) // "I'm a Teapot"
|
|
128
|
+
* ```
|
|
129
|
+
*/
|
|
73
130
|
declare function getHttpStatusMessage(code: keyof typeof status): "Continue" | "Switching Protocols" | "Processing" | "Early Hints" | "OK" | "Created" | "Accepted" | "Non-Authoritative Information" | "No Content" | "Reset Content" | "Partial Content" | "Multi-Status" | "Already Reported" | "IM Used" | "Multiple Choices" | "Moved Permanently" | "Found" | "See Other" | "Not Modified" | "Use Proxy" | "Temporary Redirect" | "Permanent Redirect" | "Bad Request" | "Unauthorized" | "Payment Required" | "Forbidden" | "Not Found" | "Method Not Allowed" | "Not Acceptable" | "Proxy Authentication Required" | "Request Timeout" | "Conflict" | "Gone" | "Length Required" | "Precondition Failed" | "Payload Too Large" | "URI Too Long" | "Unsupported Media Type" | "Range Not Satisfiable" | "Expectation Failed" | "I'm a Teapot" | "Misdirected Request" | "Unprocessable Entity" | "Locked" | "Failed Dependency" | "Too Early" | "Upgrade Required" | "Precondition Required" | "Too Many Requests" | "Request Header Fields Too Large" | "Unavailable For Legal Reasons" | "Internal Server Error" | "Not Implemented" | "Bad Gateway" | "Service Unavailable" | "Gateway Timeout" | "HTTP Version Not Supported" | "Variant Also Negotiates" | "Insufficient Storage" | "Loop Detected" | "Bandwidth Limit Exceeded" | "Not Extended" | "Network Authentication Required";
|
|
74
131
|
//#endregion
|
|
75
132
|
//#region src/addons/natural-compare.d.ts
|
|
133
|
+
/**
|
|
134
|
+
* 自然排序比较函数,按自然语言顺序比较字符串或数字
|
|
135
|
+
* 类似文件名排序,"file2" < "file10"
|
|
136
|
+
* @param a - 第一个值
|
|
137
|
+
* @param b - 第二个值
|
|
138
|
+
* @returns 比较结果:-1 表示 a < b,0 表示 a = b,1 表示 a > b
|
|
139
|
+
* @example
|
|
140
|
+
* ```ts
|
|
141
|
+
* naturalCompare(1, 2) // -1
|
|
142
|
+
* naturalCompare(2, 1) // 1
|
|
143
|
+
* naturalCompare(1, 1) // 0
|
|
144
|
+
*
|
|
145
|
+
* naturalCompare('a', 'b') // -1
|
|
146
|
+
* naturalCompare('b', 'a') // 1
|
|
147
|
+
* naturalCompare('a', 'a') // 0
|
|
148
|
+
*
|
|
149
|
+
* // 数字字符串按数值大小排序,而不是字典序
|
|
150
|
+
* naturalCompare('file1', 'file2') // -1
|
|
151
|
+
* naturalCompare('file2', 'file10') // -1 (2 < 10 自然顺序)
|
|
152
|
+
* naturalCompare('file10', 'file2') // 1
|
|
153
|
+
*
|
|
154
|
+
* // 用于数组排序
|
|
155
|
+
* ['file10.txt', 'file2.txt', 'file1.txt'].sort(naturalCompare) // ['file1.txt', 'file2.txt', 'file10.txt']
|
|
156
|
+
*
|
|
157
|
+
* // 版本号比较
|
|
158
|
+
* naturalCompare('v1.0.1', 'v1.0.10') // -1
|
|
159
|
+
* naturalCompare('v1.0.10', 'v1.0.2') // 1
|
|
160
|
+
* ```
|
|
161
|
+
*/
|
|
76
162
|
declare function naturalCompare<T extends string | number>(a: T, b: T): -1 | 0 | 1;
|
|
77
163
|
//#endregion
|
|
78
164
|
//#region src/addons/promise.d.ts
|
|
165
|
+
/**
|
|
166
|
+
* 可等待的类型,可以是普通值或 Promise
|
|
167
|
+
* @example
|
|
168
|
+
* ```ts
|
|
169
|
+
* async function process<T>(value: Awaitable<T>): Promise<T> {
|
|
170
|
+
* return await value
|
|
171
|
+
* }
|
|
172
|
+
*
|
|
173
|
+
* process('hello') // 返回 Promise<'hello'>
|
|
174
|
+
* process(Promise.resolve('hello')) // 返回 Promise<'hello'>
|
|
175
|
+
* ```
|
|
176
|
+
*/
|
|
79
177
|
type Awaitable<T> = T | PromiseLike<T>;
|
|
80
178
|
//#endregion
|
|
81
179
|
//#region src/base/array.d.ts
|
|
180
|
+
/**
|
|
181
|
+
* 获取数组元素的类型
|
|
182
|
+
* @example
|
|
183
|
+
* ```ts
|
|
184
|
+
* type ElementType = ElementOf<string[]> // string
|
|
185
|
+
* type ElementType2 = ElementOf<number[]> // number
|
|
186
|
+
* ```
|
|
187
|
+
*/
|
|
82
188
|
type ElementOf<T> = T extends (infer E)[] ? E : never;
|
|
189
|
+
/**
|
|
190
|
+
* 可数组的类型,可以是单个值或数组
|
|
191
|
+
* @example
|
|
192
|
+
* ```ts
|
|
193
|
+
* type StringOrArray = Arrayable<string> // string | string[]
|
|
194
|
+
* function process(val: Arrayable<string>) {
|
|
195
|
+
* const arr = toArray(val) // 总是得到 string[]
|
|
196
|
+
* }
|
|
197
|
+
* ```
|
|
198
|
+
*/
|
|
83
199
|
type Arrayable<T> = T | Array<T>;
|
|
200
|
+
/**
|
|
201
|
+
* 判断值是否为数组
|
|
202
|
+
* @param val - 要判断的值
|
|
203
|
+
* @returns 是否为数组
|
|
204
|
+
* @example
|
|
205
|
+
* ```ts
|
|
206
|
+
* isArray([]) // true
|
|
207
|
+
* isArray([1, 2, 3]) // true
|
|
208
|
+
* isArray('') // false
|
|
209
|
+
* isArray(null) // false
|
|
210
|
+
* ```
|
|
211
|
+
*/
|
|
84
212
|
declare const isArray: (val: unknown) => val is any[];
|
|
213
|
+
/**
|
|
214
|
+
* 将值转换为数组,如果已经是数组则返回原数组
|
|
215
|
+
* @param val - 要转换的值
|
|
216
|
+
* @returns 数组
|
|
217
|
+
* @example
|
|
218
|
+
* ```ts
|
|
219
|
+
* toArray(1) // [1]
|
|
220
|
+
* toArray([1, 2, 3]) // [1, 2, 3]
|
|
221
|
+
* toArray('hello') // ['hello']
|
|
222
|
+
* toArray(null) // [null]
|
|
223
|
+
* ```
|
|
224
|
+
*/
|
|
85
225
|
declare const toArray: <T>(val: Arrayable<T>) => T[];
|
|
226
|
+
/**
|
|
227
|
+
* 数组去重
|
|
228
|
+
* @param arr - 输入数组
|
|
229
|
+
* @returns 去重后的数组
|
|
230
|
+
* @example
|
|
231
|
+
* ```ts
|
|
232
|
+
* uniq([1, 2, 2, 3, 3, 3]) // [1, 2, 3]
|
|
233
|
+
* uniq(['a', 'b', 'a', 'c']) // ['a', 'b', 'c']
|
|
234
|
+
* uniq([]) // []
|
|
235
|
+
* ```
|
|
236
|
+
*/
|
|
86
237
|
declare const uniq: <T>(arr: readonly T[]) => T[];
|
|
238
|
+
/**
|
|
239
|
+
* 获取数组的单个元素,如果数组只有一个元素则返回该元素,否则返回 null
|
|
240
|
+
* @param arr - 输入数组
|
|
241
|
+
* @returns 单个元素或 null
|
|
242
|
+
* @example
|
|
243
|
+
* ```ts
|
|
244
|
+
* singleOrNull([1]) // 1
|
|
245
|
+
* singleOrNull(['only']) // 'only'
|
|
246
|
+
* singleOrNull([]) // null
|
|
247
|
+
* singleOrNull([1, 2, 3]) // null
|
|
248
|
+
* ```
|
|
249
|
+
*/
|
|
87
250
|
declare const singleOrNull: <T>(arr: T[]) => T | null;
|
|
251
|
+
/**
|
|
252
|
+
* 创建指定长度的数组
|
|
253
|
+
* @param len - 数组长度
|
|
254
|
+
* @param mapFn - 映射函数,可选
|
|
255
|
+
* @returns 创建的数组
|
|
256
|
+
* @example
|
|
257
|
+
* ```ts
|
|
258
|
+
* createArray(3) // [0, 1, 2]
|
|
259
|
+
* createArray(3, (i) => i * 2) // [0, 2, 4]
|
|
260
|
+
* createArray(5, (i) => `item-${i}`) // ['item-0', 'item-1', 'item-2', 'item-3', 'item-4']
|
|
261
|
+
* createArray(0) // []
|
|
262
|
+
* ```
|
|
263
|
+
*/
|
|
88
264
|
declare function createArray<T = number>(len: number, mapFn?: (idx: number) => T): T[];
|
|
265
|
+
/**
|
|
266
|
+
* 随机打乱数组(会修改原数组)
|
|
267
|
+
* @param array - 要打乱的数组
|
|
268
|
+
* @returns 打乱后的数组
|
|
269
|
+
* @example
|
|
270
|
+
* ```ts
|
|
271
|
+
* const arr = [1, 2, 3, 4, 5]
|
|
272
|
+
* shuffle(arr) // [3, 1, 5, 2, 4] (随机顺序)
|
|
273
|
+
* // ⚠️ 原数组也会被修改
|
|
274
|
+
* ```
|
|
275
|
+
*/
|
|
89
276
|
declare function shuffle<T>(array: T[]): T[];
|
|
277
|
+
/**
|
|
278
|
+
* 生成数字范围序列
|
|
279
|
+
* @param stop - 结束值
|
|
280
|
+
* @returns 数字数组
|
|
281
|
+
* @example
|
|
282
|
+
* ```ts
|
|
283
|
+
* range(5) // [0, 1, 2, 3, 4]
|
|
284
|
+
* range(1, 5) // [1, 2, 3, 4]
|
|
285
|
+
* range(0, 10, 2) // [0, 2, 4, 6, 8]
|
|
286
|
+
* range(1, 1) // []
|
|
287
|
+
* range(0, 0) // []
|
|
288
|
+
* ```
|
|
289
|
+
*/
|
|
90
290
|
declare function range(stop: number): number[];
|
|
291
|
+
/**
|
|
292
|
+
* 生成数字范围序列
|
|
293
|
+
* @param start - 开始值
|
|
294
|
+
* @param stop - 结束值
|
|
295
|
+
* @param step - 步长,默认为 1
|
|
296
|
+
* @returns 数字数组
|
|
297
|
+
*/
|
|
91
298
|
declare function range(start: number, stop: number, step?: number): number[];
|
|
92
299
|
//#endregion
|
|
93
300
|
//#region src/base/boolean.d.ts
|
|
301
|
+
/**
|
|
302
|
+
* 判断值是否为布尔类型
|
|
303
|
+
* @param val - 要判断的值
|
|
304
|
+
* @returns 是否为布尔值
|
|
305
|
+
* @example
|
|
306
|
+
* ```ts
|
|
307
|
+
* isBoolean(true) // true
|
|
308
|
+
* isBoolean(false) // true
|
|
309
|
+
* isBoolean('true') // false
|
|
310
|
+
* isBoolean(1) // false
|
|
311
|
+
* isBoolean(null) // false
|
|
312
|
+
* ```
|
|
313
|
+
*/
|
|
94
314
|
declare const isBoolean: (val: any) => val is boolean;
|
|
95
315
|
//#endregion
|
|
96
316
|
//#region src/base/date.d.ts
|
|
317
|
+
/**
|
|
318
|
+
* 判断值是否为 Date 对象
|
|
319
|
+
* @param val - 要判断的值
|
|
320
|
+
* @returns 是否为 Date 对象
|
|
321
|
+
* @example
|
|
322
|
+
* ```ts
|
|
323
|
+
* isDate(new Date()) // true
|
|
324
|
+
* isDate(new Date('2024-01-01')) // true
|
|
325
|
+
* isDate('2024-01-01') // false
|
|
326
|
+
* isDate(null) // false
|
|
327
|
+
* ```
|
|
328
|
+
*/
|
|
97
329
|
declare const isDate: (val: any) => val is Date;
|
|
330
|
+
/**
|
|
331
|
+
* 获取当前时间戳(毫秒)
|
|
332
|
+
* @returns 时间戳
|
|
333
|
+
* @example
|
|
334
|
+
* ```ts
|
|
335
|
+
* timestamp() // 1705401600000
|
|
336
|
+
* ```
|
|
337
|
+
*/
|
|
98
338
|
declare const timestamp: () => number;
|
|
339
|
+
/**
|
|
340
|
+
* 判断日期是否为今天
|
|
341
|
+
* @param date - 要判断的日期
|
|
342
|
+
* @returns 是否为今天
|
|
343
|
+
* @example
|
|
344
|
+
* ```ts
|
|
345
|
+
* isToDay(new Date()) // true
|
|
346
|
+
* isToDay(new Date('2000-01-01')) // false
|
|
347
|
+
* ```
|
|
348
|
+
*/
|
|
99
349
|
declare const isToDay: (date: Date) => boolean;
|
|
350
|
+
/**
|
|
351
|
+
* 判断两个日期是否为同一天
|
|
352
|
+
* @param dateA - 第一个日期
|
|
353
|
+
* @param dateB - 第二个日期
|
|
354
|
+
* @returns 是否为同一天
|
|
355
|
+
* @example
|
|
356
|
+
* ```ts
|
|
357
|
+
* const date1 = new Date('2024-01-15T10:30:00')
|
|
358
|
+
* const date2 = new Date('2024-01-15T23:59:59')
|
|
359
|
+
* isSameDay(date1, date2) // true
|
|
360
|
+
* ```
|
|
361
|
+
* @example
|
|
362
|
+
* ```ts
|
|
363
|
+
* const date1 = new Date('2024-01-15')
|
|
364
|
+
* const date2 = new Date('2024-01-16')
|
|
365
|
+
* isSameDay(date1, date2) // false
|
|
366
|
+
* ```
|
|
367
|
+
*/
|
|
100
368
|
declare function isSameDay(dateA: Date, dateB: Date): boolean;
|
|
101
369
|
//#endregion
|
|
102
370
|
//#region src/base/function.d.ts
|
|
371
|
+
/**
|
|
372
|
+
* 函数类型定义
|
|
373
|
+
* @example
|
|
374
|
+
* ```ts
|
|
375
|
+
* type MyFunc = Fn<string> // () => string
|
|
376
|
+
* const fn: MyFunc = () => 'hello'
|
|
377
|
+
* ```
|
|
378
|
+
*/
|
|
103
379
|
type Fn<T = void> = () => T;
|
|
380
|
+
/**
|
|
381
|
+
* 判断值是否为函数
|
|
382
|
+
* @param val - 要判断的值
|
|
383
|
+
* @returns 是否为函数
|
|
384
|
+
* @example
|
|
385
|
+
* ```ts
|
|
386
|
+
* isFunction(() => {}) // true
|
|
387
|
+
* isFunction(function() {}) // true
|
|
388
|
+
* isFunction('function') // false
|
|
389
|
+
* isFunction(null) // false
|
|
390
|
+
* ```
|
|
391
|
+
*/
|
|
104
392
|
declare const isFunction: <T extends Function>(val: any) => val is T;
|
|
393
|
+
/**
|
|
394
|
+
* 空函数,什么都不做
|
|
395
|
+
* @returns void
|
|
396
|
+
* @example
|
|
397
|
+
* ```ts
|
|
398
|
+
* const callback: Fn = noop // 作为默认回调
|
|
399
|
+
* callback() // 什么都不执行
|
|
400
|
+
* ```
|
|
401
|
+
*/
|
|
105
402
|
declare const noop: () => void;
|
|
403
|
+
/**
|
|
404
|
+
* 对值应用转换函数
|
|
405
|
+
* @param val - 输入值
|
|
406
|
+
* @param fn - 转换函数
|
|
407
|
+
* @returns 转换后的值
|
|
408
|
+
* @example
|
|
409
|
+
* ```ts
|
|
410
|
+
* transform(5, (x) => x * 2) // 10
|
|
411
|
+
* transform('hello', (s) => s.toUpperCase()) // 'HELLO'
|
|
412
|
+
* transform([1, 2, 3], (arr) => arr.length) // 3
|
|
413
|
+
* ```
|
|
414
|
+
*/
|
|
106
415
|
declare const transform: <T, R>(val: T, fn: (val: T) => R) => R;
|
|
107
416
|
//#endregion
|
|
108
417
|
//#region src/base/nullable.d.ts
|
|
418
|
+
/**
|
|
419
|
+
* 可为空值的类型,包含 T、null 或 undefined
|
|
420
|
+
* @example
|
|
421
|
+
* ```ts
|
|
422
|
+
* type StringOrNull = Nullable<string> // string | null | undefined
|
|
423
|
+
* const val1: StringOrNull = 'hello'
|
|
424
|
+
* const val2: StringOrNull = null
|
|
425
|
+
* const val3: StringOrNull = undefined
|
|
426
|
+
* ```
|
|
427
|
+
*/
|
|
109
428
|
type Nullable<T> = T | null | undefined;
|
|
429
|
+
/**
|
|
430
|
+
* 判断值是否为真值
|
|
431
|
+
* @param v - 要判断的值
|
|
432
|
+
* @returns 是否为真值
|
|
433
|
+
* @example
|
|
434
|
+
* ```ts
|
|
435
|
+
* isTruthy(true) // true
|
|
436
|
+
* isTruthy(1) // true
|
|
437
|
+
* isTruthy('hello') // true
|
|
438
|
+
* isTruthy({}) // true
|
|
439
|
+
* isTruthy([]) // true
|
|
440
|
+
* isTruthy(false) // false
|
|
441
|
+
* isTruthy(0) // false
|
|
442
|
+
* isTruthy('') // false
|
|
443
|
+
* isTruthy(null) // false
|
|
444
|
+
* isTruthy(undefined) // false
|
|
445
|
+
* ```
|
|
446
|
+
*/
|
|
110
447
|
declare const isTruthy: <T>(v: T) => v is NonNullable<T>;
|
|
448
|
+
/**
|
|
449
|
+
* 判断值是否为 undefined
|
|
450
|
+
* @param val - 要判断的值
|
|
451
|
+
* @returns 是否为 undefined
|
|
452
|
+
* @example
|
|
453
|
+
* ```ts
|
|
454
|
+
* isUndefined(undefined) // true
|
|
455
|
+
* isUndefined(void 0) // true
|
|
456
|
+
* isUndefined(null) // false
|
|
457
|
+
* isUndefined('') // false
|
|
458
|
+
* ```
|
|
459
|
+
*/
|
|
111
460
|
declare const isUndefined: (val: any) => val is undefined;
|
|
461
|
+
/**
|
|
462
|
+
* 判断值是否不为 undefined
|
|
463
|
+
* @param v - 要判断的值
|
|
464
|
+
* @returns 是否不为 undefined
|
|
465
|
+
* @example
|
|
466
|
+
* ```ts
|
|
467
|
+
* notUndefined('hello') // true
|
|
468
|
+
* notUndefined(0) // true
|
|
469
|
+
* notUndefined(null) // true
|
|
470
|
+
* notUndefined(undefined) // false
|
|
471
|
+
* ```
|
|
472
|
+
*/
|
|
112
473
|
declare const notUndefined: <T>(v: T) => v is Exclude<T, undefined>;
|
|
474
|
+
/**
|
|
475
|
+
* 判断值是否为 null
|
|
476
|
+
* @param val - 要判断的值
|
|
477
|
+
* @returns 是否为 null
|
|
478
|
+
* @example
|
|
479
|
+
* ```ts
|
|
480
|
+
* isNull(null) // true
|
|
481
|
+
* isNull(undefined) // false
|
|
482
|
+
* isNull('') // false
|
|
483
|
+
* isNull(0) // false
|
|
484
|
+
* ```
|
|
485
|
+
*/
|
|
113
486
|
declare const isNull: (val: any) => val is null;
|
|
487
|
+
/**
|
|
488
|
+
* 判断值是否不为 null
|
|
489
|
+
* @param v - 要判断的值
|
|
490
|
+
* @returns 是否不为 null
|
|
491
|
+
* @example
|
|
492
|
+
* ```ts
|
|
493
|
+
* notNull('hello') // true
|
|
494
|
+
* notNull(0) // true
|
|
495
|
+
* notNull(undefined) // true
|
|
496
|
+
* notNull(null) // false
|
|
497
|
+
* ```
|
|
498
|
+
*/
|
|
114
499
|
declare const notNull: <T>(v: T | null) => v is Exclude<T, null>;
|
|
500
|
+
/**
|
|
501
|
+
* 判断值是否不为 null 且不为 undefined
|
|
502
|
+
* @param v - 要判断的值
|
|
503
|
+
* @returns 是否为非空值
|
|
504
|
+
* @example
|
|
505
|
+
* ```ts
|
|
506
|
+
* notNullish('hello') // true
|
|
507
|
+
* notNullish(0) // true
|
|
508
|
+
* notNullish(false) // true
|
|
509
|
+
* notNullish(null) // false
|
|
510
|
+
* notNullish(undefined) // false
|
|
511
|
+
* ```
|
|
512
|
+
*/
|
|
115
513
|
declare const notNullish: <T>(v: T | null | undefined) => v is NonNullable<T>;
|
|
514
|
+
/**
|
|
515
|
+
* 判断值是否已定义(不是 undefined)
|
|
516
|
+
* @param val - 要判断的值
|
|
517
|
+
* @returns 是否已定义
|
|
518
|
+
* @example
|
|
519
|
+
* ```ts
|
|
520
|
+
* isDefined('hello') // true
|
|
521
|
+
* isDefined(0) // true
|
|
522
|
+
* isDefined(null) // true
|
|
523
|
+
* isDefined(false) // true
|
|
524
|
+
* isDefined(undefined) // false
|
|
525
|
+
* ```
|
|
526
|
+
*/
|
|
116
527
|
declare const isDefined: <T = any>(val?: T) => val is T;
|
|
117
528
|
//#endregion
|
|
118
529
|
//#region src/base/number.d.ts
|
|
530
|
+
/**
|
|
531
|
+
* 判断值是否为数字类型
|
|
532
|
+
* @param val - 要判断的值
|
|
533
|
+
* @returns 是否为数字
|
|
534
|
+
* @example
|
|
535
|
+
* ```ts
|
|
536
|
+
* isNumber(0) // true
|
|
537
|
+
* isNumber(1) // true
|
|
538
|
+
* isNumber(1.5) // true
|
|
539
|
+
* isNumber(Number.NaN) // true
|
|
540
|
+
* isNumber(Number.POSITIVE_INFINITY) // true
|
|
541
|
+
* isNumber('123') // false
|
|
542
|
+
* isNumber(null) // false
|
|
543
|
+
* ```
|
|
544
|
+
*/
|
|
119
545
|
declare const isNumber: (val: any) => val is number;
|
|
546
|
+
/**
|
|
547
|
+
* 将值限制在指定范围内
|
|
548
|
+
* @param n - 要限制的值
|
|
549
|
+
* @param min - 最小值
|
|
550
|
+
* @param max - 最大值
|
|
551
|
+
* @returns 限制后的值
|
|
552
|
+
* @example
|
|
553
|
+
* ```ts
|
|
554
|
+
* clamp(5, 0, 10) // 5 (在范围内)
|
|
555
|
+
* clamp(-5, 0, 10) // 0 (小于最小值,返回最小值)
|
|
556
|
+
* clamp(15, 0, 10) // 10 (大于最大值,返回最大值)
|
|
557
|
+
* clamp(1.5, 0, 1) // 1
|
|
558
|
+
* clamp(0.5, 0, 1) // 0.5
|
|
559
|
+
* ```
|
|
560
|
+
*/
|
|
120
561
|
declare const clamp: (n: number, min: number, max: number) => number;
|
|
562
|
+
/**
|
|
563
|
+
* 线性插值,返回两个值之间的插值
|
|
564
|
+
* @param min - 起始值
|
|
565
|
+
* @param max - 结束值
|
|
566
|
+
* @param t - 插值参数(0-1 之间)
|
|
567
|
+
* @returns 插值结果
|
|
568
|
+
* @example
|
|
569
|
+
* ```ts
|
|
570
|
+
* lerp(0, 100, 0.5) // 50 (中点)
|
|
571
|
+
* lerp(0, 100, 0) // 0 (起始点)
|
|
572
|
+
* lerp(0, 100, 1) // 100 (结束点)
|
|
573
|
+
* lerp(0, 100, -0.5) // 0 (t 被限制到 0)
|
|
574
|
+
* lerp(0, 100, 1.5) // 100 (t 被限制到 1)
|
|
575
|
+
* lerp(-100, 0, 0.5) // -50
|
|
576
|
+
* lerp(0, 10, 0.25) // 2.5
|
|
577
|
+
* ```
|
|
578
|
+
*/
|
|
121
579
|
declare function lerp(min: number, max: number, t: number): number;
|
|
122
580
|
//#endregion
|
|
123
581
|
//#region src/base/object.d.ts
|
|
582
|
+
/**
|
|
583
|
+
* 判断值是否为普通对象
|
|
584
|
+
* @param val - 要判断的值
|
|
585
|
+
* @returns 是否为对象
|
|
586
|
+
* @example
|
|
587
|
+
* ```ts
|
|
588
|
+
* isObject({}) // true
|
|
589
|
+
* isObject({ a: 1 }) // true
|
|
590
|
+
* isObject([]) // false
|
|
591
|
+
* isObject(null) // false
|
|
592
|
+
* isObject(new Date()) // false
|
|
593
|
+
* ```
|
|
594
|
+
*/
|
|
124
595
|
declare const isObject: (val: any) => val is object;
|
|
596
|
+
/**
|
|
597
|
+
* 判断键是否为对象的键
|
|
598
|
+
* @param val - 对象
|
|
599
|
+
* @param key - 键
|
|
600
|
+
* @returns 是否为对象的键
|
|
601
|
+
* @example
|
|
602
|
+
* ```ts
|
|
603
|
+
* const obj = { a: 1, b: 2, c: 3 }
|
|
604
|
+
* isKeyOf(obj, 'a') // true
|
|
605
|
+
* isKeyOf(obj, 'b') // true
|
|
606
|
+
* isKeyOf(obj, 'd') // false
|
|
607
|
+
* // 注意:isKeyOf 使用 'key in val',所以继承属性也会返回 true
|
|
608
|
+
* isKeyOf(obj, 'toString') // true
|
|
609
|
+
* ```
|
|
610
|
+
*/
|
|
125
611
|
declare const isKeyOf: <T extends object>(val: T, key: keyof any) => key is keyof T;
|
|
612
|
+
/**
|
|
613
|
+
* 根据条件过滤对象的属性
|
|
614
|
+
* @param obj - 输入对象
|
|
615
|
+
* @param fn - 过滤函数
|
|
616
|
+
* @returns 过滤后的对象
|
|
617
|
+
* @example
|
|
618
|
+
* ```ts
|
|
619
|
+
* const obj = { a: 1, b: 2, c: 3, d: 4 }
|
|
620
|
+
* objectFilter(obj, (key, value) => value > 2) // { c: 3, d: 4 }
|
|
621
|
+
* objectFilter(obj, (key, value) => key === 'a') // { a: 1 }
|
|
622
|
+
* ```
|
|
623
|
+
*/
|
|
126
624
|
declare function objectFilter<O extends object>(obj: O, fn: (key: keyof O, value: O[keyof O]) => boolean): O;
|
|
625
|
+
/**
|
|
626
|
+
* 从对象中选取指定的属性
|
|
627
|
+
* @param obj - 输入对象
|
|
628
|
+
* @param keys - 要选取的键
|
|
629
|
+
* @returns 包含指定属性的新对象
|
|
630
|
+
* @example
|
|
631
|
+
* ```ts
|
|
632
|
+
* const obj = { a: 1, b: 2, c: 3, d: 4 }
|
|
633
|
+
* objectPick(obj, 'a', 'c') // { a: 1, c: 3 }
|
|
634
|
+
* objectPick(obj, 'a', 'x') // { a: 1 }
|
|
635
|
+
* objectPick(obj) // {}
|
|
636
|
+
* ```
|
|
637
|
+
*/
|
|
127
638
|
declare function objectPick<O extends object, T extends keyof O>(obj: O, ...keys: T[]): Pick<O, T>;
|
|
639
|
+
/**
|
|
640
|
+
* 从对象中排除指定的属性
|
|
641
|
+
* @param obj - 输入对象
|
|
642
|
+
* @param keys - 要排除的键
|
|
643
|
+
* @returns 不包含指定属性的新对象
|
|
644
|
+
* @example
|
|
645
|
+
* ```ts
|
|
646
|
+
* const obj = { a: 1, b: 2, c: 3, d: 4 }
|
|
647
|
+
* objectOmit(obj, 'a', 'c') // { b: 2, d: 4 }
|
|
648
|
+
* objectOmit(obj, 'a', 'x') // { b: 2, c: 3, d: 4 }
|
|
649
|
+
* objectOmit(obj) // { a: 1, b: 2, c: 3, d: 4 }
|
|
650
|
+
* ```
|
|
651
|
+
*/
|
|
128
652
|
declare function objectOmit<O extends object, T extends keyof O>(obj: O, ...keys: T[]): Omit<O, T>;
|
|
653
|
+
/**
|
|
654
|
+
* 清除对象中值为 undefined 的属性
|
|
655
|
+
* @param obj - 输入对象
|
|
656
|
+
* @returns 清除 undefined 后的对象
|
|
657
|
+
* @example
|
|
658
|
+
* ```ts
|
|
659
|
+
* clearUndefined({ a: 1, b: undefined, c: 3 }) // { a: 1, c: 3 }
|
|
660
|
+
* clearUndefined({ a: undefined }) // {}
|
|
661
|
+
* clearUndefined({ a: null, b: undefined }) // { a: null } // null 不会被清除
|
|
662
|
+
* ```
|
|
663
|
+
*/
|
|
129
664
|
declare function clearUndefined<O extends object>(obj: O): O;
|
|
130
665
|
//#endregion
|
|
131
666
|
//#region src/base/regexp.d.ts
|
|
667
|
+
/**
|
|
668
|
+
* 判断值是否为正则表达式
|
|
669
|
+
* @param val - 要判断的值
|
|
670
|
+
* @returns 是否为正则表达式
|
|
671
|
+
* @example
|
|
672
|
+
* ```ts
|
|
673
|
+
* isRegExp(/test/) // true
|
|
674
|
+
* isRegExp(new RegExp('test')) // true
|
|
675
|
+
* isRegExp('test') // false
|
|
676
|
+
* isRegExp(null) // false
|
|
677
|
+
* ```
|
|
678
|
+
*/
|
|
132
679
|
declare const isRegExp: (val: any) => val is RegExp;
|
|
133
680
|
//#endregion
|
|
134
681
|
//#region src/base/string.d.ts
|
|
682
|
+
/**
|
|
683
|
+
* 获取值的字符串表示形式
|
|
684
|
+
* @param val - 输入值
|
|
685
|
+
* @returns 字符串表示
|
|
686
|
+
* @example
|
|
687
|
+
* ```ts
|
|
688
|
+
* toString({}) // '[object Object]'
|
|
689
|
+
* toString([]) // '[object Array]'
|
|
690
|
+
* toString(null) // '[object Null]'
|
|
691
|
+
* toString(undefined) // '[object Undefined]'
|
|
692
|
+
* toString(123) // '[object Number]'
|
|
693
|
+
* ```
|
|
694
|
+
*/
|
|
135
695
|
declare const toString: (val: any) => string;
|
|
696
|
+
/**
|
|
697
|
+
* 判断值是否为字符串
|
|
698
|
+
* @param val - 要判断的值
|
|
699
|
+
* @returns 是否为字符串
|
|
700
|
+
* @example
|
|
701
|
+
* ```ts
|
|
702
|
+
* isString('hello') // true
|
|
703
|
+
* isString('') // true
|
|
704
|
+
* isString(123) // false
|
|
705
|
+
* isString(null) // false
|
|
706
|
+
* ```
|
|
707
|
+
*/
|
|
136
708
|
declare const isString: (val: unknown) => val is string;
|
|
709
|
+
/**
|
|
710
|
+
* 将反斜杠转换为正斜杠
|
|
711
|
+
* @param str - 输入字符串
|
|
712
|
+
* @returns 转换后的字符串
|
|
713
|
+
* @example
|
|
714
|
+
* ```ts
|
|
715
|
+
* slash('C:\\Users\\test') // 'C:/Users/test'
|
|
716
|
+
* slash('path\\to\\file') // 'path/to/file'
|
|
717
|
+
* slash('path/to/file') // 'path/to/file'
|
|
718
|
+
* ```
|
|
719
|
+
*/
|
|
137
720
|
declare const slash: (str: string) => string;
|
|
721
|
+
/**
|
|
722
|
+
* 确保字符串以指定前缀开头
|
|
723
|
+
* @param str - 输入字符串
|
|
724
|
+
* @param prefix - 前缀
|
|
725
|
+
* @returns 处理后的字符串
|
|
726
|
+
* @example
|
|
727
|
+
* ```ts
|
|
728
|
+
* ensurePrefix('example.com', 'https://') // 'https://example.com'
|
|
729
|
+
* ensurePrefix('https://example.com', 'https://') // 'https://example.com'
|
|
730
|
+
* ensurePrefix('path', '/') // '/path'
|
|
731
|
+
* ```
|
|
732
|
+
*/
|
|
138
733
|
declare const ensurePrefix: (str: string, prefix: string) => string;
|
|
734
|
+
/**
|
|
735
|
+
* 确保字符串以指定后缀结尾
|
|
736
|
+
* @param str - 输入字符串
|
|
737
|
+
* @param suffix - 后缀
|
|
738
|
+
* @returns 处理后的字符串
|
|
739
|
+
* @example
|
|
740
|
+
* ```ts
|
|
741
|
+
* ensureSuffix('file', '.json') // 'file.json'
|
|
742
|
+
* ensureSuffix('file.json', '.json') // 'file.json'
|
|
743
|
+
* ensureSuffix('path', '/') // 'path/'
|
|
744
|
+
* ```
|
|
745
|
+
*/
|
|
139
746
|
declare const ensureSuffix: (str: string, suffix: string) => string;
|
|
747
|
+
/**
|
|
748
|
+
* 将字符串首字母大写,其余字母小写
|
|
749
|
+
* @param str - 输入字符串
|
|
750
|
+
* @returns 处理后的字符串
|
|
751
|
+
* @example
|
|
752
|
+
* ```ts
|
|
753
|
+
* capitalize('hello') // 'Hello'
|
|
754
|
+
* capitalize('HELLO') // 'Hello'
|
|
755
|
+
* capitalize('hELLO') // 'Hello'
|
|
756
|
+
* capitalize('hello world') // 'Hello world'
|
|
757
|
+
* // ⚠️ 空字符串会返回 'undefined'
|
|
758
|
+
* ```
|
|
759
|
+
*/
|
|
140
760
|
declare const capitalize: (str: string) => string;
|
|
141
761
|
//#endregion
|
|
142
762
|
export { Arrayable, Awaitable, ElementOf, Fn, Nullable, assert, capitalize, clamp, clearUndefined, createArray, ensurePrefix, ensureSuffix, getHttpStatusMessage, isArray, isBoolean, isDate, isDeepEqual, isDefined, isFunction, isKeyOf, isNull, isNumber, isObject, isRegExp, isSameDay, isString, isToDay, isTruthy, isUndefined, lerp, naturalCompare, noop, notNull, notNullish, notUndefined, objectFilter, objectOmit, objectPick, range, shuffle, singleOrNull, slash, status, timestamp, toArray, toString, transform, uniq };
|