@base-web-kits/base-tools-ts 0.9.8 → 0.9.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.
Files changed (48) hide show
  1. package/dist/array/index.d.ts +9 -0
  2. package/dist/array/index.d.ts.map +1 -0
  3. package/dist/async/index.d.ts +14 -0
  4. package/dist/async/index.d.ts.map +1 -0
  5. package/dist/bean/EventBus.d.ts +37 -0
  6. package/dist/bean/EventBus.d.ts.map +1 -0
  7. package/dist/bean/index.d.ts +2 -0
  8. package/dist/bean/index.d.ts.map +1 -0
  9. package/dist/day/index.d.ts +100 -0
  10. package/dist/day/index.d.ts.map +1 -0
  11. package/dist/index.d.ts +14 -1215
  12. package/dist/index.d.ts.map +1 -0
  13. package/dist/lodash/index.d.ts +8 -0
  14. package/dist/lodash/index.d.ts.map +1 -0
  15. package/dist/number/big.d.ts +137 -0
  16. package/dist/number/big.d.ts.map +1 -0
  17. package/dist/number/format.d.ts +88 -0
  18. package/dist/number/format.d.ts.map +1 -0
  19. package/dist/number/index.d.ts +4 -0
  20. package/dist/number/index.d.ts.map +1 -0
  21. package/dist/number/random.d.ts +33 -0
  22. package/dist/number/random.d.ts.map +1 -0
  23. package/dist/object/index.d.ts +11 -0
  24. package/dist/object/index.d.ts.map +1 -0
  25. package/dist/string/format.d.ts +33 -0
  26. package/dist/string/format.d.ts.map +1 -0
  27. package/dist/string/index.d.ts +4 -0
  28. package/dist/string/index.d.ts.map +1 -0
  29. package/dist/string/other.d.ts +15 -0
  30. package/dist/string/other.d.ts.map +1 -0
  31. package/dist/string/random.d.ts +27 -0
  32. package/dist/string/random.d.ts.map +1 -0
  33. package/dist/typing/index.d.ts +134 -0
  34. package/dist/typing/index.d.ts.map +1 -0
  35. package/dist/url/file/index.d.ts +22 -0
  36. package/dist/url/file/index.d.ts.map +1 -0
  37. package/dist/url/index.d.ts +5 -0
  38. package/dist/url/index.d.ts.map +1 -0
  39. package/dist/url/oss/index.d.ts +77 -0
  40. package/dist/url/oss/index.d.ts.map +1 -0
  41. package/dist/url/param/index.d.ts +49 -0
  42. package/dist/url/param/index.d.ts.map +1 -0
  43. package/dist/url/qn/index.d.ts +58 -0
  44. package/dist/url/qn/index.d.ts.map +1 -0
  45. package/dist/validator/index.d.ts +296 -0
  46. package/dist/validator/index.d.ts.map +1 -0
  47. package/package.json +1 -1
  48. package/dist/index.d.cts +0 -1216
package/dist/index.d.cts DELETED
@@ -1,1216 +0,0 @@
1
- import { EventType } from 'mitt';
2
- import dayjs from 'dayjs';
3
- export { default as dayjs } from 'dayjs';
4
- export * from 'lodash-es';
5
- import BigNumber from 'bignumber.js';
6
- export { default as BigNumber } from 'bignumber.js';
7
-
8
- /**
9
- * 拖拽排序 (不改变原数组)
10
- * @param list 原始数组
11
- * @param fromIndex 要移动的元素的原始索引
12
- * @param toIndex 要移动到的目标索引
13
- * @returns 移动元素后的新数组
14
- */
15
- declare function arrayMove<T>(list: T[], fromIndex: number, toIndex: number): T[];
16
-
17
- /**
18
- * 将 Promise 包装为 [data, error] 形式, 减少 try-catch 代码量
19
- * @param p 要包装的 Promise
20
- * @returns 一个 Promise,其结果为 [data, error] 形式
21
- * @example
22
- * const [data, err] = await toAsync(fetch('https://api.example.com/data'));
23
- * if (err) {
24
- * console.error(err);
25
- * return;
26
- * }
27
- * console.log(data);
28
- */
29
- declare function toAsync<T>(p: Promise<T>): Promise<[T | null, unknown]>;
30
-
31
- type Events = Record<EventType, unknown>;
32
- /**
33
- * 总线式发布订阅
34
- * @example
35
- * const emitter = new EventBus(); // 支持链式调用
36
- * emitter.on('xx', fn); // 订阅事件 xx
37
- * emitter.once('xx', fn); // 订阅事件 xx 一次
38
- * emitter.emit('xx', any); // 发布事件 xx,参数任意
39
- * emitter.off('xx'); // 移除事件 xx 下全部监听
40
- * emitter.off('xx', fn); // 移除事件 xx 下指定监听
41
- * emitter.clear(); // 移除所有事件
42
- *
43
- * @example 类型约束
44
- * type T = { a: number; b: string };
45
- * const emitter = new EventBus<{ xx: T; yy: void }>();
46
- * const fn = (arg: T) => {}
47
- * emitter.on('xx', fn);
48
- * emitter.off('xx', fn);
49
- * emitter.emit('xx', { a: 123, b: '123' });
50
- * emitter.emit('yy');
51
- */
52
- declare class EventBus<T extends Events = Events> {
53
- private readonly _emitter;
54
- /** 订阅 */
55
- on<K extends keyof T>(type: K, fn: (event: T[K]) => void): this;
56
- /** 订阅一次 */
57
- once<K extends keyof T>(type: K, fn: (event: T[K]) => void): this;
58
- /** 发布 */
59
- emit<K extends keyof T>(type: K, event?: T[K]): this;
60
- /** 移除 */
61
- off<K extends keyof T>(type: K, fn?: (event: T[K]) => void): this;
62
- /** 清空 */
63
- clear(): this;
64
- }
65
-
66
- type BaseTime = number | string | Date | dayjs.Dayjs | null | undefined;
67
- /**
68
- * 创建 dayjs 实例
69
- * 文档: https://day.js.org/zh-CN/
70
- * @param t 各种规范或不规范的时间
71
- * @returns dayjs 实例
72
- * @example
73
- * const d = toDayjs('2021-01-01'); // dayjs 实例
74
- * d.format('YYYY-MM-DD HH:mm:ss'); // "2025-12-10 11:33:16"
75
- * d.valueOf(); // 毫秒时间戳,如 1765337596913
76
- * d.unix(); // 秒时间戳,如 1765337596
77
- * d.millisecond(); // 毫秒 913
78
- * d.second(); // 秒 16
79
- * d.minute(); // 分 33
80
- * d.hour(); // 时 11
81
- * d.date(); // 日 10
82
- * d.day(); // 星期几 5(周日=0)
83
- * d.month() + 1; // 月 12
84
- * d.year(); // 年 2025
85
- * d.startOf('day').valueOf(); // 当日零点
86
- * d.startOf('month').format('YYYY-MM-DD HH:mm:ss'); // 月初 "2025-12-01 00:00:00"
87
- * d.endOf('month').format('YYYY-MM-DD HH:mm:ss'); // 月末 "2025-12-31 23:59:59"
88
- * d.fromNow(); // “刚刚”、“x分钟前/后”、“x小时前/后”、“x天前/后”、“x月前/后”、“x年前/后”
89
- * d.isSame(t, 'day'); // 是否与t在同一天
90
- * d.diff(); // 与当前时间相差的毫秒数
91
- * d.diff(t); // 与t相差的毫秒数
92
- * d.diff(t, 'second'); // 与t相差的秒数
93
- * d.diff(t, 'minute'); // 与t相差的分钟数
94
- * d.diff(t, 'hour'); // 与t相差的小时数
95
- * d.diff(t, 'day'); // 与t相差的天数
96
- * d.diff(t, 'week'); // 与t相差的周数
97
- * d.diff(t, 'month'); // 与t相差的月数
98
- * d.diff(t, 'quarter'); // 与t相差的季度数
99
- * d.diff(t, 'year'); // 与t相差的年数
100
- */
101
- declare function toDayjs(t: BaseTime, fmt?: dayjs.OptionType): dayjs.Dayjs;
102
- /**
103
- * 获取“前几天”的日期范围
104
- * @param offset 正整数天数
105
- * @param fmt 日期格式,默认 `YYYY-MM-DD`
106
- * @returns `[start, end]` 日期字符串数组
107
- * @example
108
- * 若今天为 2025-11-19:
109
- * getDateRangeBefore(1) // ['2025-11-18', '2025-11-19']
110
- * getDateRangeBefore(1, 'YYYY-MM-DD HH:mm:ss') // ['2025-11-18 00:00:00', '2025-11-19 23:59:59']
111
- */
112
- declare function getDateRangeBefore(offset: number, fmt?: string): string[];
113
- /**
114
- * 获取“后几天”的日期范围
115
- * - 起点:今天;终点:`offset` 天后
116
- * - 若 `fmt` 含时间令牌(如 `HH:mm:ss`),则返回整日范围:起点为当日零点,终点为当日末尾
117
- * @param offset 正整数天数
118
- * @param fmt 日期格式,默认 `YYYY-MM-DD`
119
- * @returns `[start, end]` 日期字符串数组
120
- * @example
121
- * 若今天为 2025-11-19:
122
- * getDateRangeAfter(1) // ['2025-11-19', '2025-11-20']
123
- * getDateRangeAfter(1, 'YYYY-MM-DD HH:mm:ss') // ['2025-11-19 00:00:00', '2025-11-20 23:59:59']
124
- */
125
- declare function getDateRangeAfter(offset: number, fmt?: string): string[];
126
- /**
127
- * 获取倒计时的时间分解(零填充字符串)
128
- * @param diff 毫秒差值(正数表示剩余时间,负数/0表示已到期)
129
- * @returns 包含天、时、分、秒、毫秒的零填充对象
130
- * @example
131
- * const diff = toDayjs(t).diff(); // 毫秒差值
132
- * const parts = getCountdownParts(diff); // { d: '01', h: '02', m: '03', s: '04', ms: '567' }
133
- */
134
- declare function getCountdownParts(diff: number): {
135
- d: string;
136
- h: string;
137
- m: string;
138
- s: string;
139
- ms: string;
140
- };
141
- /**
142
- * 通过出生日期计算年龄
143
- * @param birthdate 生日日期,支持多种格式(会被自动解析)
144
- * @returns 年龄对象,包含 `age`(年龄数值)和 `type`(年龄单位,'year' 表示年,'month' 表示月)
145
- * @example
146
- * // 假设当前日期为 2025-11-19
147
- * getAgeByBirthdate('2025-05-10'); // { age: 6, type: 'month' }
148
- * getAgeByBirthdate('2020-11-19'); // { age: 5, type: 'year' }
149
- * getAgeByBirthdate('2020-12-01'); // { age: 4, type: 'year' }(生日还没到, 所以年龄是4岁)
150
- */
151
- declare function getAgeByBirthdate(birthdate: string): {
152
- age: number;
153
- type: string;
154
- };
155
-
156
- /**
157
- * 数值入参类型。
158
- * 支持原生 `number`、`string`(包含小数、科学计数法等)以及 `BigNumber`。
159
- */
160
- type NumLike = string | number | BigNumber;
161
- /**
162
- * 将任意 `NumLike` 统一转换为 `BigNumber` 实例。
163
- * @param x 任意支持的数值入参。
164
- * @returns `BigNumber` 实例。
165
- * @example
166
- * big('0.1'); // => BigNumber
167
- * big(0.2); // => BigNumber
168
- */
169
- declare function big(x: NumLike): BigNumber;
170
- /**
171
- * 高精度加法(支持多个参数连加)。
172
- * @example
173
- * bigAdd(0.1, 0.2); // => 0.3
174
- * bigAdd('0.1', '0.2'); // => 0.3
175
- * bigAdd(1, 2, 3, 4); // => 10 // 多参数连加: 1+2+3+4
176
- */
177
- declare function bigAdd(a: NumLike, ...rest: NumLike[]): number;
178
- /**
179
- * 高精度减法(支持多个参数连减)。
180
- * @example
181
- * bigSub(1, 0.9); // => 0.1
182
- * bigSub('1.1', '0.2'); // => 0.9
183
- * bigSub(10, 1, 2, 3); // => 4 // 多参数连减: 10-1-2-3
184
- */
185
- declare function bigSub(a: NumLike, ...rest: NumLike[]): number;
186
- /**
187
- * 高精度乘法(支持多个参数连乘)。
188
- * @example
189
- * bigMul(0.1, 0.2); // => 0.02
190
- * bigMul('1.5', '3'); // => 4.5
191
- * bigMul(2, 3, 4); // => 24 // 多参数连乘: 2*3*4
192
- */
193
- declare function bigMul(a: NumLike, ...rest: NumLike[]): number;
194
- /**
195
- * 高精度除法(支持多个参数连除)。
196
- * @example
197
- * bigDiv(1, 3); // => 0.333333...
198
- * bigDiv('10', '4'); // => 2.5
199
- * bigDiv(100, 5, 2); // => 10 // 多参数连除: 100/5/2
200
- */
201
- declare function bigDiv(a: NumLike, ...rest: NumLike[]): number;
202
- /**
203
- * 指数运算
204
- * @param x 底数。
205
- * @param y 指数。
206
- * @returns 计算结果。
207
- * @example
208
- * bigPow(2, 3); // => 8
209
- * bigPow('2.5', 2); // => 6.25
210
- */
211
- declare function bigPow(x: NumLike, y: NumLike): number;
212
- /**
213
- * 四舍五入到指定位数
214
- * @param x 需要舍入的数值。
215
- * @param dp 保留的小数位数,默认 `0`(取整)。
216
- * @param rm 舍入模式,默认 `ROUND_HALF_UP`(四舍五入)。
217
- * @returns 舍入后的数值。
218
- * @example
219
- * bigRound(1.6); // => 2
220
- * bigRound('1.234', 2); // => 1.23
221
- * bigRound('1.235', 2); // => 1.24
222
- * bigRound('1.299', 2, BigNumber.ROUND_DOWN); // => 1.29
223
- */
224
- declare function bigRound(x: NumLike, dp?: number, rm?: BigNumber.RoundingMode): number;
225
- /**
226
- * 将数值按指定位数格式化为字符串(保留小数位)。
227
- * @param x 需要格式化的数值。
228
- * @param dp 保留的小数位数,默认 `2`。
229
- * @param rm 舍入模式,默认 `ROUND_HALF_UP`(四舍五入)。
230
- * @returns 格式化后的字符串。
231
- * @example
232
- * toFixed('1'); // => '1.00'
233
- * +toFixed('1'); // => 1
234
- * toFixed(1.2345); // => '1.23'
235
- * toFixed(1.2345, 3); // => '1.235'
236
- * toFixed('1.2345', 0, BigNumber.ROUND_UP); // => '2'
237
- */
238
- declare function toFixed(x: NumLike, dp?: number, rm?: BigNumber.RoundingMode): string;
239
- /**
240
- * 比较两个数值大小。
241
- * @example
242
- * bigCompare('2', '10'); // => -1
243
- * bigCompare(3, 3); // => 0
244
- * bigCompare('10', 2); // => 1
245
- */
246
- declare function bigCompare(a: NumLike, b: NumLike): -1 | 0 | 1 | null;
247
- /**
248
- * 判断两个数值是否相等。
249
- * @example
250
- * bigEqual('1.0', 1); // => true
251
- * bigEqual(2, 1); // => false
252
- */
253
- declare function bigEqual(a: NumLike, b: NumLike): boolean;
254
- /**
255
- * 判断 a 是否大于 b。
256
- * @example
257
- * bigGt(2, 1); // => true
258
- * bigGt(1, 2); // => false
259
- */
260
- declare function bigGt(a: NumLike, b: NumLike): boolean;
261
- /**
262
- * 判断 a 是否大于等于 b。
263
- * @example
264
- * bigGte(2, 2); // => true
265
- * bigGte(1, 2); // => false
266
- */
267
- declare function bigGte(a: NumLike, b: NumLike): boolean;
268
- /**
269
- * 判断 a 是否小于 b。
270
- * @example
271
- * bigLt(1, 2); // => true
272
- * bigLt(2, 1); // => false
273
- */
274
- declare function bigLt(a: NumLike, b: NumLike): boolean;
275
- /**
276
- * 判断 a 是否小于等于 b。
277
- * @example
278
- * bigLte(2, 2); // => true
279
- * bigLte(1, 2); // => true
280
- * bigLte(2, 1); // => false
281
- */
282
- declare function bigLte(a: NumLike, b: NumLike): boolean;
283
-
284
- /**
285
- * 开头补零
286
- * @param n 数字
287
- * @param len 总长度,默认 2
288
- * @returns 零填充后的字符串
289
- * @example
290
- * zeroPad(1) // '01'
291
- * zeroPad(12) // '12'
292
- * zeroPad(12, 4) // '0012'
293
- */
294
- declare function zeroPad(n: number | string, len?: number): string;
295
- /**
296
- * 给数字添加指定单位 (支持数字字符串,其他非法字符返回原值)
297
- * @param unit 单位
298
- * @param num 数字
299
- * @example
300
- * withUnit(0, 'px') // "0px"
301
- * withUnit(1, 'px') // "1px"
302
- * withUnit('1', 'rpx') // "1rpx"
303
- * withUnit('1', '%') // "1%"
304
- * withUnit('auto', 'px') // "auto"
305
- * withUnit(null | undefined | '') // ""
306
- */
307
- declare function withUnit(num?: number | string, unit?: string): string;
308
- /**
309
- * 给数字添加px单位 (支持数字字符串,其他非法字符返回原值)
310
- * @example
311
- * withUnitPx(10) // "10px"
312
- * withUnitPx('10') // "10px"
313
- * withUnitPx('10px') // "10px"
314
- * withUnitPx("auto") // "auto"
315
- * withUnitPx("30%") // "30%"
316
- * withUnitPx(null | undefined | '') // ""
317
- * withUnitPx(0) // "0px"
318
- */
319
- declare function withUnitPx(num?: string | number): string;
320
- /**
321
- * 给数字添加距离单位:当数值大于等于 1000m 时转换为 km,否则显示 m(最多两位小数、无无意义补零)
322
- * @example
323
- * withDistance(5); // => '5m'
324
- * withDistance(999.456); // => '999.46m'
325
- * withDistance(1000); // => '1km'
326
- * withDistance('1500'); // => '1.5km'
327
- * withDistance('1728'); // => '1.73km'
328
- */
329
- declare function withDistance(m: number | string): string;
330
- /**
331
- * 数字转千分位字符串(保留小数与符号)。
332
- * @example
333
- * toThousandth(1234567); // => '1,234,567'
334
- * toThousandth('1234567.89'); // => '1,234,567.89'
335
- * toThousandth('-987654'); // => '-987,654'
336
- */
337
- declare function toThousandth(str: number | string): string;
338
- /**
339
- * 阿拉伯数字转中文整数(忽略小数;支持负数)。
340
- * @param num 输入的数字或数字字符串(小数部分将被丢弃)
341
- * @returns 中文数字字符串;非法输入返回空字符串
342
- * @example
343
- * toChineseNum(123456); // "十二万三千四百五十六"
344
- * toChineseNum(-10008); // "负一万零八"
345
- * `第${toChineseNum(123)}条` // "第一百二十三条"
346
- */
347
- declare function toChineseNum(num: number | string): string;
348
- /**
349
- * 金额转中文大写(支持角/分/厘,精度控制)。
350
- * @param amount 金额(支持 number | string),非法或非有限数返回空串
351
- * @param opts 配置项
352
- * @param opts.precision 保留小数位(0~3),对应:0无小数、1角、2角分、3角分厘;默认 2
353
- * @param opts.rm 舍入模式,默认 `BigNumber.ROUND_HALF_UP`(四舍五入)
354
- * @param opts.yuanChar 元单位字符(`'元' | '圆'`),默认 `'元'`
355
- * @returns 中文大写金额字符串;示例:`壹佰贰拾叁元肆角伍分`
356
- * @example
357
- * toChineseCurrency(0) // '零元整'
358
- * toChineseCurrency(10) // '拾元整'
359
- * toChineseCurrency(101) // '壹佰零壹元整'
360
- * toChineseCurrency(1001000) // '壹佰万零壹仟元整'
361
- * toChineseCurrency(1001.01) // '壹仟零壹元壹分'
362
- * toChineseCurrency('1234.5679', { precision: 3 }) // '壹仟贰佰叁拾肆元伍角陆分捌厘'
363
- * toChineseCurrency(-1.2) // '负壹元贰角'
364
- */
365
- declare function toChineseCurrency(amount: number | string, opts?: {
366
- precision?: 0 | 1 | 2 | 3;
367
- rm?: BigNumber.RoundingMode;
368
- yuanChar?: '元' | '圆';
369
- }): string;
370
-
371
- /**
372
- * 随机生成 `a` 到 `b` 的整数(闭区间,包含两端)。
373
- * - 自动交换边界,按从小到大处理。
374
- * - 下界向上取整、上界向下取整后再取值。
375
- * @param a 边界值。
376
- * @param b 边界值。
377
- * @returns 闭区间内的随机整数。
378
- * @example
379
- * randomInt(0, 10); // => 0..10 之间的随机整数(含 0 与 10)
380
- * randomInt(10, 0); // => 0..10 之间的随机整数(含 0 与 10)
381
- * randomInt(5.2, 10.8); // => 6..10 之间取整随机数(含 6 与 10)
382
- */
383
- declare function randomInt(a: number, b: number): number;
384
- /**
385
- * 随机生成 `a` 到 `b` 的浮点数(半开区间,包含下界不包含上界)。
386
- * - 自动交换边界,按从小到大处理。
387
- * @param a 边界值。
388
- * @param b 边界值。
389
- * @returns 半开区间内的随机浮点数。
390
- * @example
391
- * randomFloat(0, 10); // => [0, 10) 内的随机浮点数
392
- * randomFloat(10, 0); // => [0, 10) 内的随机浮点数
393
- * randomFloat(5.2, 10.8); // => [5.2, 10.8) 内的随机浮点数
394
- */
395
- declare function randomFloat(a: number, b: number): number;
396
- /**
397
- * 随机生成一个布尔值。
398
- * @returns 随机布尔值。
399
- * @example
400
- * randomBoolean(); // => 随机返回 true 或 false
401
- */
402
- declare function randomBoolean(): boolean;
403
-
404
- /**
405
- * 获取对象键名数组(类型安全)。
406
- * 注:内置 `Object.keys` 与 `lodash-es` 的 `keys` 在 TS 中通常返回 `string[]`,无法精确到 `keyof T`。
407
- * @param obj 目标对象
408
- * @returns 类型精确的 `Array<keyof T>`
409
- * @example
410
- * const o = { a: 1, b: 'x' };
411
- * const keys = getObjectKeys(o); // type: ('a' | 'b')[], value: ['a','b']
412
- */
413
- declare function getObjectKeys<T extends object>(obj: T): Array<keyof T>;
414
-
415
- /**
416
- * 文本脱敏
417
- * @param s 原始文本
418
- * @param keepLeft 保留左侧字符数(默认 1)
419
- * @param keepRight 保留右侧字符数(默认 0)
420
- * @param maskChar 脱敏字符(默认 `*`)
421
- * @returns 脱敏文本
422
- * @example
423
- * toMaskText('王小明', 1, 0) // => '王*'
424
- * toMaskText('王小明', 1, 1) // => '王*明'
425
- * toMaskText('13800138000', 3, 4) // => '138****8000'
426
- */
427
- declare function toMaskText(s: string, keepLeft?: number, keepRight?: number, maskChar?: string): string;
428
- /**
429
- * 手机号中间打星:保留前三位与后四位,中间打 `*`。
430
- * @param phone 手机号字符串
431
- * @returns 遮蔽后的手机号
432
- * @example
433
- * toMaskPhone('13800138000') // => '138****8000'
434
- */
435
- declare function toMaskPhone(phone: string): string;
436
- /**
437
- * 姓名打星:
438
- * - 长度 ≤ 2:保留首字,其余打 `*`
439
- * - 长度 ≥ 3:保留首尾,各打星中间
440
- * @param name 姓名字符串
441
- * @returns 遮蔽后的姓名
442
- * @example
443
- * toMaskName('张三') // => '张*'
444
- * toMaskName('王小明') // => '王*明'
445
- */
446
- declare function toMaskName(name: string): string;
447
-
448
- /**
449
- * 生成UUID
450
- * @returns UUID字符串
451
- * @example
452
- * const uuid = createUUID() // '7982fcfe-5721-4632-bede-6000885be57d'
453
- */
454
- declare function createUUID(): string;
455
- /**
456
- * 生成随机字符串id
457
- * - 常用于生成元素标签的id (默认加上'id_'前缀, 避免小程序中数字开头的id导致查询节点信息失败)
458
- * @param prefix 前缀, 默认 'id_'
459
- * @returns 随机字符串
460
- * @example
461
- * const id = createRandId(); // 'id_0rjuuuqv60xi'
462
- * const id = createRandId('canvas_'); // 'canvas_v82a7ctm09q'
463
- */
464
- declare function createRandId(prefix?: string): string;
465
- /**
466
- * 时间+固定位数的随机数字字符串
467
- * @param digits 随机部分的位数,默认 6
468
- * @returns 时间+随机数字字符串
469
- * @example
470
- * const traceId = createTimeRandId(); // '1763002648039123456'
471
- * const traceId = createTimeRandId(8); // '176300264803912345678'
472
- */
473
- declare function createTimeRandId(digits?: number): string;
474
-
475
- /**
476
- * 计算字符串在 UTF-8 编码下的字节长度。
477
- * 使用场景:
478
- * 1) 按字节限制表单输入(避免超过后端/数据库字段上限)
479
- * 2) 评估网络传输、缓存(Redis/消息队列)开销
480
- * 3) 根据字节数截断或提示用户(而非按字符数)
481
- * @param str 输入的字符串
482
- * @returns 字符串的字节长度
483
- * @example
484
- * getStringByteLength('abc') // 3
485
- * getStringByteLength('中文') // 6
486
- * getStringByteLength('😊') // 4
487
- */
488
- declare function getStringByteLength(str: string): number;
489
-
490
- /**
491
- * 将指定属性设为必填(不改变其他属性)。
492
- * - 所有属性必填请使用内置的Required<T>
493
- * @example
494
- * type User = { id?: number; name?: string; age?: number };
495
- * type U1 = SetRequired<User, 'id' | 'name'>; // { id: number; name: string; age?: number }
496
- */
497
- type SetRequired<T, K extends keyof T> = {
498
- [P in K]-?: T[P];
499
- } & Omit<T, K>;
500
- /**
501
- * 将指定属性设为可选(不改变其他属性)。
502
- * - 所有属性可选请使用内置的Partial<T>
503
- * @example
504
- * type User = { id: number; name: string; age: number };
505
- * type U2 = SetOptional<User, 'age'>; // { id: number; name: string; age?: number }
506
- */
507
- type SetOptional<T, K extends keyof T> = {
508
- [P in K]+?: T[P];
509
- } & Omit<T, K>;
510
- /**
511
- * 深度可选(递归将所有属性设为可选)。
512
- * @example
513
- * type T = { a: { b: number }; list: Array<{ id: string }> };
514
- * type R = DeepPartial<T>;
515
- */
516
- type DeepPartial<T> = T extends (infer U)[] ? DeepPartial<U>[] : T extends (...args: unknown[]) => unknown ? T : T extends object ? {
517
- [K in keyof T]?: DeepPartial<T[K]>;
518
- } : T;
519
- /**
520
- * 深度必填(递归移除所有可选标记)。
521
- * @example
522
- * type T = { a?: { b?: number } };
523
- * type R = DeepRequired<T>; // { a: { b: number } }
524
- */
525
- type DeepRequired<T> = T extends (infer U)[] ? DeepRequired<U>[] : T extends (...args: unknown[]) => unknown ? T : T extends object ? {
526
- [K in keyof T]-?: DeepRequired<T[K]>;
527
- } : T;
528
- /**
529
- * 取消只读(顶层移除 `readonly`)。
530
- * @example
531
- * type R = Mutable<Readonly<{ a: number }>>; // { a: number }
532
- */
533
- type Mutable<T> = {
534
- -readonly [K in keyof T]: T[K];
535
- };
536
- /**
537
- * 深度只读(递归添加 `readonly`)。
538
- * @example
539
- * type R = ReadonlyDeep<{ a: { b: number }; list: { id: string }[] }>;
540
- */
541
- type ReadonlyDeep<T> = T extends (infer U)[] ? ReadonlyArray<ReadonlyDeep<U>> : T extends (...args: unknown[]) => unknown ? T : T extends object ? {
542
- readonly [K in keyof T]: ReadonlyDeep<T[K]>;
543
- } : T;
544
- /**
545
- * 对象值联合类型。
546
- * @example
547
- * type V = ValueOf<{ a: 1; b: 'x' }>; // 1 | 'x'
548
- */
549
- type ValueOf<T> = T[keyof T];
550
- /**
551
- * 根据值类型获取键名联合。
552
- * @example
553
- * type Keys = KeysOfType<{ a: string; b: number; c: string }, string>; // 'a' | 'c'
554
- */
555
- type KeysOfType<T, V> = {
556
- [K in keyof T]-?: T[K] extends V ? K : never;
557
- }[keyof T];
558
- /**
559
- * 按值类型挑选属性。
560
- * @example
561
- * type R = PickByType<{ a: string; b: number; c: string }, string>; // { a: string; c: string }
562
- */
563
- type PickByType<T, V> = Pick<T, KeysOfType<T, V>>;
564
- /**
565
- * 可选键名联合。
566
- * @example
567
- * type K = OptionalKeys<{ a?: number; b: string }>; // 'a'
568
- */
569
- type OptionalKeys<T> = {
570
- [K in keyof T]-?: Pick<T, K> extends Required<Pick<T, K>> ? never : K;
571
- }[keyof T];
572
- /**
573
- * 必填键名联合。
574
- * @example
575
- * type K = RequiredKeys<{ a?: number; b: string }>; // 'b'
576
- */
577
- type RequiredKeys<T> = {
578
- [K in keyof T]-?: Pick<T, K> extends Required<Pick<T, K>> ? K : never;
579
- }[keyof T];
580
- /**
581
- * 联合类型转交叉类型。
582
- * @example
583
- * type I = UnionToIntersection<{ a: 1 } | { b: 2 }>; // { a: 1 } & { b: 2 }
584
- */
585
- type UnionToIntersection<U> = (U extends unknown ? (x: U) => unknown : never) extends (x: infer I) => unknown ? I : never;
586
- /**
587
- * 合并属性(以 `U` 覆盖 `T` 的同名属性)。
588
- * @example
589
- * type R = Merge<{ a: 1; b: 2 }, { b: 3; c: 4 }>; // { a: 1; b: 3; c: 4 }
590
- */
591
- type Merge<T, U> = Omit<T, keyof U> & U;
592
- /**
593
- * 名义类型(品牌化)。
594
- * @example
595
- * type UserId = Brand<number, 'UserId'>;
596
- */
597
- type Brand<T, B extends string> = T & {
598
- readonly __brand: B;
599
- };
600
- /**
601
- * 可空(包含 `null | undefined`)。
602
- * @example
603
- * type R = Nullable<string>; // string | null | undefined
604
- */
605
- type Nullable<T> = T | null | undefined;
606
- /**
607
- * 可序列化为 JSON 的值类型。
608
- */
609
- type JsonPrimitive = string | number | boolean | null;
610
- type JsonObject = {
611
- [k: string]: JsonValue;
612
- };
613
- type JsonArray = Array<JsonValue>;
614
- type JsonValue = JsonPrimitive | JsonObject | JsonArray;
615
- /**
616
- * 精确匹配形状(不允许多余属性)。
617
- * @example
618
- * type Shape = { a: number };
619
- * type OK = Exact<{ a: number }, Shape>; // { a: number }
620
- * type NG = Exact<{ a: number; b: 1 }, Shape>; // never
621
- */
622
- type Exact<T, Shape> = T extends Shape ? Exclude<keyof T, keyof Shape> extends never ? T : never : never;
623
-
624
- /**
625
- * 获取文件后缀(不含点,返回小写)。
626
- * 当文件名不包含点('.')时,返回空字符串。
627
- * @param fileName 文件名,例如 `avatar.PNG`
628
- * @returns 后缀字符串,例如 `png`
629
- * @example getFileSuffix('avatar.PNG') // 'png'
630
- * @example getFileSuffix('a.tar.gz') // 'gz'
631
- * @example getFileSuffix('.ignore') // ''
632
- * @example getFileSuffix('abc') // ''
633
- */
634
- declare function getFileSuffix(fileName: string): string;
635
- /**
636
- * 根据文件后缀判断文件类型。
637
- * 会将后缀转换为小写后与 `FILE_TYPE` 映射匹配;若未匹配到则返回 `'unknown'`。
638
- * @param fileName 文件名
639
- * @returns 文件类型字符串(如 'img' | 'video' | 'voice' | 'excel' | 'word' | 'zip' | 'ppt' | 'app' | 'unknown')
640
- * @example getFileType('avatar.PNG') // 'img'
641
- * @example getFileType('archive.tar') // 'zip'
642
- * @example getFileType('abc') // 'unknown'
643
- */
644
- declare function getFileType(fileName: string): "zip" | "ppt" | "unknown" | "img" | "video" | "voice" | "excel" | "word" | "app";
645
-
646
- // 兼容oss后期添加的参数
647
- type OtherOption = Record<string, string | number | boolean | undefined>;
648
-
649
- type OSSGravity = 'nw' | 'north' | 'ne' | 'west' | 'center' | 'east' | 'sw' | 'south' | 'se';
650
-
651
- type OSSFormat = 'png' | 'jpg' | 'jpeg' | 'webp' | 'bmp' | 'gif' | 'tiff' | 'heic' | 'avif';
652
-
653
- type OSSResizeOption = {
654
- p?: number;
655
- w?: number;
656
- h?: number;
657
- m?: 'lfit' | 'mfit' | 'fill' | 'pad' | 'fixed';
658
- l?: number;
659
- s?: number;
660
- limit?: 1 | 0;
661
- color?: string;
662
- } & OtherOption;
663
-
664
- type OSSCropOption = {
665
- w?: number;
666
- h?: number;
667
- x?: number;
668
- y?: number;
669
- g?: OSSGravity;
670
- p?: number;
671
- } & OtherOption;
672
-
673
- type OSSIndexCropOption = {
674
- x?: number;
675
- y?: number;
676
- i?: number;
677
- } & OtherOption;
678
-
679
- type OSSQualityOption = {
680
- q?: number;
681
- Q?: number;
682
- } & OtherOption;
683
-
684
- type OSSWatermarkOption = {
685
- type?: string;
686
- text?: string;
687
- size?: number;
688
- color?: string;
689
- shadow?: number;
690
- t?: number;
691
- g?: OSSGravity;
692
- x?: number;
693
- y?: number;
694
- voffset?: number;
695
- fill?: 0 | 1;
696
- padx?: number;
697
- pady?: number;
698
- image?: string;
699
- P?: number;
700
- } & OtherOption;
701
-
702
- type OSSBlurOption = {
703
- r: number;
704
- s: number;
705
- g?: 'face' | 'faces';
706
- p?: number;
707
- } & OtherOption;
708
-
709
- type OSSRoundedCornersOption = {
710
- r: number;
711
- } & OtherOption;
712
-
713
- type OSSCircleOption = {
714
- r: number;
715
- } & OtherOption;
716
-
717
- type OSSImgOption = {
718
- resize?: OSSResizeOption;
719
- watermark?: OSSWatermarkOption;
720
- flip?: 0 | 1 | 2;
721
- crop?: OSSCropOption;
722
- quality?: OSSQualityOption;
723
- format?: OSSFormat;
724
- info?: boolean;
725
- 'auto-orient'?: 0 | 1;
726
- circle?: OSSCircleOption;
727
- indexcrop?: OSSIndexCropOption;
728
- 'rounded-corners'?: OSSRoundedCornersOption;
729
- blur?: OSSBlurOption;
730
- rotate?: number;
731
- interlace?: 0 | 1;
732
- 'average-hue'?: boolean;
733
- bright?: number;
734
- sharpen?: number;
735
- contrast?: number;
736
- } & {
737
- [action: string]: number | string | boolean | Record<string, unknown> | undefined;
738
- };
739
-
740
- type OSSVideoOption = {
741
- convert?: Record<string, unknown>;
742
- animation?: Record<string, unknown>;
743
- sprite?: Record<string, unknown>;
744
- snapshots?: Record<string, unknown>;
745
- concat?: Record<string, unknown>;
746
- info?: boolean;
747
- } & {
748
- [action: string]: number | string | boolean | Record<string, unknown> | undefined;
749
- };
750
-
751
- type OSSAudioOption = {
752
- convert?: Record<string, unknown>;
753
- concat?: Record<string, unknown>;
754
- info?: boolean;
755
- } & {
756
- [action: string]: number | string | boolean | Record<string, unknown> | undefined;
757
- };
758
-
759
- type OSSHlsOption = {
760
- m3u8?: boolean | Record<string, unknown>;
761
- } & {
762
- [action: string]: number | string | boolean | Record<string, unknown> | undefined;
763
- };
764
-
765
- type OSSOption = OSSImgOption | OSSVideoOption | OSSAudioOption | OSSHlsOption;
766
-
767
- /**
768
- * 获取url的查询参数值
769
- * - 采用纯JS解析,因为小程序不支持URLSearchParams
770
- * @param key 参数名
771
- * @param url 完整 URL 或仅查询串(如 "a=1&b=2")
772
- * @returns 解码后的参数值 (若不存在|"null"|"undefined",则返回 null)
773
- * @example
774
- * const q = getUrlParam('q', 'https://a.com/?q=%E6%B5%8B%E8%AF%95'); // "测试"
775
- * const a = getUrlParam('a', 'a=1'); // "1"
776
- * const list = getUrlParam('list', 'list=[1,2]'); // "[1,2]"
777
- * const list = getUrlParam('list', 'list=null'); // null
778
- * const list = getUrlParam('list', 'list=undefined'); // null
779
- */
780
- declare function getUrlParam(key: string, url: string): string | null;
781
- /**
782
- * 获取url的查询参数值,并转为number类型
783
- * @param key 参数名
784
- * @param url 完整 URL 或仅查询串(如 "a=1&b=2")
785
- * @returns 解码后的参数值 (若不存在|"非数字字符串",则返回 null)
786
- * @example
787
- * const a = getUrlNumber('a', 'https://a.com/?a=1'); // 1
788
- * const a = getUrlNumber('a', 'a=1'); // 1
789
- * const a = getUrlNumber('a', 'a=1.2'); // 1.2
790
- * const a = getUrlNumber('a', 'a=abc'); // null
791
- */
792
- declare function getUrlNumber(key: string, url: string): number | null;
793
- /**
794
- * 获取url的所有查询参数值
795
- * - 采用纯JS解析,因为小程序不支持URLSearchParams
796
- * @param url 完整 URL 或仅查询串(如 "a=1&b=2")
797
- * @returns 解码后的键值对象(无参数返回空对象; "null"|"undefined"的参数会被忽略)
798
- * @example
799
- * const params = getUrlParamAll('a=1&b=2'); // { a: "1", b: "2" }
800
- * const params = getUrlParamAll('https://a.com/?a=1&b=2'); // { a: "1", b: "2" }
801
- * const params = getUrlParamAll('a=1&b=null'); // { a: "1" }
802
- * const params = getUrlParamAll('a=1&b=undefined'); // { a: "1" }
803
- */
804
- declare function getUrlParamAll(url: string): Record<string, string>;
805
- /**
806
- * 将对象参数拼接到 URL
807
- * - 采用纯JS拼接,因为小程序不支持URLSearchParams
808
- * @param url 基础地址
809
- * @param param 将要追加的参数对象;`null/undefined` 值会被忽略,Object 会使用 `JSON.stringify`
810
- * @returns 拼接后的完整 URL(保留原有哈希片段)
811
- * @example
812
- * const url = appendUrlParam('https://a.com', { q: '测试', list: [1, 2], a: null, b: undefined }); // 'https://a.com/?q=%E6%B5%8B%E8%AF%95&list=[1,2]'
813
- */
814
- declare function appendUrlParam(url: string, param: Record<string, unknown>): string;
815
-
816
- type QnOtherOption = Record<string, string | number | boolean | undefined>;
817
-
818
- type QnImageView2Option = {
819
- mode?: 0 | 1 | 2 | 3 | 4 | 5;
820
- w?: number;
821
- h?: number;
822
- l?: number;
823
- limit?: 0 | 1;
824
- q?: number;
825
- format?: string;
826
- } & QnOtherOption;
827
-
828
- type QnMogr2Option = {
829
- thumbnail?: string;
830
- crop?: string;
831
- rotate?: number;
832
- 'auto-orient'?: boolean;
833
- format?: string;
834
- interlace?: 0 | 1;
835
- background?: string;
836
- q?: number;
837
- blur?: string | { r: number; s: number };
838
- colors?: number;
839
- } & QnOtherOption;
840
-
841
- type QnWatermarkOption = {
842
- type?: 'image' | 'text' | number;
843
- image?: string;
844
- text?: string;
845
- font?: string;
846
- fontsize?: number;
847
- fill?: string;
848
- gravity?: string;
849
- dx?: number;
850
- dy?: number;
851
- dissolve?: number;
852
- } & QnOtherOption;
853
-
854
- type QnImgOption = {
855
- imageView2?: QnImageView2Option;
856
- imageMogr2?: QnMogr2Option;
857
- watermark?: QnWatermarkOption;
858
- imageslim?: boolean;
859
- imageInfo?: boolean;
860
- } & {
861
- thumbnail?: string;
862
- crop?: string;
863
- rotate?: number;
864
- 'auto-orient'?: boolean;
865
- format?: string;
866
- interlace?: 0 | 1;
867
- background?: string;
868
- q?: number;
869
- blur?: string | { r: number; s: number };
870
- colors?: number;
871
- [action: string]: number | string | boolean | Record<string, unknown> | undefined;
872
- };
873
-
874
- type QnAvthumbOption = {
875
- format?: string;
876
- s?: string;
877
- vcodec?: 'libx264' | 'libx265' | 'copy' | string;
878
- vb?: string; // e.g. '1.25m', '128k', support '!'
879
- r?: number; // frame rate
880
- acodec?: 'libmp3lame' | 'libfdk_aac' | 'copy' | string;
881
- ab?: string; // e.g. '128k', support '!'
882
- ar?: number; // sampling rate
883
- } & QnOtherOption;
884
-
885
- type QnVframeOption = {
886
- format?: 'jpg' | 'png';
887
- offset?: number; // seconds, support decimals
888
- w?: number;
889
- h?: number;
890
- } & QnOtherOption;
891
-
892
- type QnVideoOption = {
893
- avthumb?: QnAvthumbOption;
894
- vframe?: QnVframeOption;
895
- } & {
896
- [action: string]: number | string | boolean | Record<string, unknown> | undefined;
897
- };
898
-
899
- type QnAudioOption = {
900
- avthumb?: QnAvthumbOption;
901
- } & {
902
- [action: string]: number | string | boolean | Record<string, unknown> | undefined;
903
- };
904
-
905
- type QnHlsOption = {
906
- level?: number; // e.g. 3 for 720p preset
907
- format?: 'm3u8';
908
- segtime?: number; // 2-10 seconds
909
- t?: string; // duration like '1.500s'
910
- vcodec?: 'libx264' | 'libx265' | 'copy' | string;
911
- vb?: string; // bitrate e.g. '1.25m', '128k', support '!'
912
- r?: number; // frame rate [1,60]
913
- s?: string; // resolution '1280x720'
914
- acodec?: 'libmp3lame' | 'libfdk_aac' | 'copy' | string;
915
- ab?: string; // audio bitrate
916
- ar?: number; // audio sampling rate
917
- output?: string; // m3u8 filename (url-safe base64)
918
- } & QnOtherOption;
919
-
920
- /**
921
- * 纯字母(不含空格与符号)。
922
- * @param s 字符串
923
- * @returns 是否为字母
924
- * @example
925
- * isLetter('abc') // true
926
- * isLetter('123') // false
927
- * isLetter('abc123') // false
928
- */
929
- declare function isLetter(s: string): boolean;
930
- /**
931
- * 纯中文(不含空格与符号)。
932
- * @param s 字符串
933
- * @returns 是否为纯中文
934
- * @example
935
- * isChinese('你好') // true
936
- */
937
- declare function isChinese(s: string): boolean;
938
- /**
939
- * 纯数字(非负整数,不含空格与符号)。
940
- * @param s 字符串
941
- * @returns 是否为数字
942
- * @example
943
- * isDigits('12') // true
944
- * isDigits('1.2') // false
945
- * isDigits('-12') // false
946
- * isDigits('a12') // false
947
- */
948
- declare function isDigits(s: string): boolean;
949
- /**
950
- * 数字字符串格式校验
951
- * @param value 待验证值(字符串或数字)
952
- * @param options 可选项
953
- * @param options.negative 是否允许负数,默认 false
954
- * @param options.decimal 小数位数,默认 2(0 表示必须整数)
955
- * @param options.thousands 是否允许千分位逗号,默认 false
956
- * @param options.leadZero 是否允许整数部分出现多余前导0 (默认false, 即禁止'007',但允许'0.50')
957
- * @example
958
- * isNumeric('123.45'); // true
959
- * isNumeric('123.45', { decimal: 0 }); // false
960
- * isNumeric('-1,234.5', { negative: true, thousands: true }); // true
961
- * isNumeric('0123', { leadZero: true }); // true(现在允许)
962
- * isNumeric('0123'); // false(默认禁止)
963
- * isNumeric('0.50'); // true(始终允许)
964
- * isNumeric('.5'); // false(整数部分不能省)
965
- * isNumeric('123.'); // false(小数部分不能省)
966
- */
967
- declare function isNumeric(value: string | number, options?: {
968
- negative?: boolean;
969
- decimal?: number;
970
- thousands?: boolean;
971
- leadZero?: boolean;
972
- }): boolean;
973
- /**
974
- * 是否为中国大陆手机号(11 位,以 1 开头,第二位 3-9)。
975
- * @param s 待校验的号码
976
- * @returns 是否为合法手机号
977
- * @example
978
- * isMobilePhone('13800138000') // true
979
- * isMobilePhone('12800138000') // false
980
- */
981
- declare function isMobilePhone(s: string): boolean;
982
- /**
983
- * 是否为中国大陆座机号(区号-号码,可选分机)。
984
- * 格式:`0AA-BBBBBBBB(-EXT)`,其中区号 2-3 位、号码 7-8 位、分机 1-6 位。
985
- * @param s 待校验的号码
986
- * @returns 是否为合法座机号
987
- * @example
988
- * isLandline('010-88888888') // true
989
- * isLandline('0371-12345678-123') // true
990
- */
991
- declare function isLandline(s: string): boolean;
992
- /**
993
- * 联系电话:是否为中国大陆“手机号或座机号”。
994
- * @param s 待校验的号码
995
- * @returns 是否为合法的手机号或座机号
996
- * @example
997
- * isPhone('13800138000') // true
998
- * isPhone('010-88888888') // true
999
- */
1000
- declare function isPhone(s: string): boolean;
1001
- /**
1002
- * 校验邮箱地址(基于 RFC 5322 的常用子集)。
1003
- * @param s 待校验的邮箱字符串
1004
- * @returns 是否为合法邮箱
1005
- * @example
1006
- * isEmail('user@example.com') // true
1007
- * isEmail('invalid@') // false
1008
- */
1009
- declare function isEmail(s: string): boolean;
1010
- /**
1011
- * 中文姓名(允许中间点 `·`),长度 2-20。
1012
- * @param s 姓名
1013
- * @returns 是否为合法中文姓名
1014
- * @example
1015
- * isChineseName('张三') // true
1016
- * isChineseName('阿·娜') // true
1017
- */
1018
- declare function isChineseName(s: string): boolean;
1019
- /**
1020
- * 身份证校验(支持中国大陆严格校验;台湾/香港/澳门做格式校验)。
1021
- * 规则:
1022
- * - 中国大陆(严格):18 位校验位 + 出生日期合法性;兼容 15 位旧号(日期校验);
1023
- * - 台湾(格式):`^[A-Z][12]\d{8}$`(首字母 + 性别位 1/2 + 8 位数字);不含校验位算法;
1024
- * - 香港(格式):`^[A-Z]{1,2}\d{6}\(?[0-9A]\)?$`(1-2 字母 + 6 位数字 + 校验位,可带括号);
1025
- * - 澳门(格式):常见为 `^[157]\d{6}\(?\d\)?$`(类别位 1/5/7 + 7 位数字 + 校验位,可带括号)。
1026
- * @param code 身份证号码
1027
- * @returns 是否为合法身份证号
1028
- * @example
1029
- * isIdentityCard('11010519491231002X') // true(大陆)
1030
- * isIdentityCard('A123456789') // true(台湾格式)
1031
- * isIdentityCard('A123456(3)') // true(香港格式)
1032
- * isIdentityCard('1234567(8)') // true(澳门格式)
1033
- */
1034
- declare function isIdentityCard(code: string): boolean;
1035
- /**
1036
- * 护照号码校验(宽松通用格式,适合单输入框无法确认国家的场景)。
1037
- * 说明:各国护照格式差异较大,此函数提供常见模式的格式校验;不做校验位算法。
1038
- * 包含:
1039
- * - 中国护照常见:`E/G` 开头 + 8 位数字;`D/P/S` 开头 + 7 位数字;
1040
- * - 台湾护照常见:首字母 + 8 位数字;
1041
- * - 通用兜底:6-9 位的字母数字组合;移除输入中的空格与 `-` 后再校验。
1042
- * @param s 护照号码
1043
- * @returns 是否匹配常见护照格式
1044
- * @example
1045
- * isPassport('E12345678') // true
1046
- * isPassport('P1234567') // true
1047
- * isPassport('A12345678') // true(台湾常见)
1048
- * isPassport('AB-1234567') // true(移除分隔符后匹配)
1049
- */
1050
- declare function isPassport(s: string): boolean;
1051
- /**
1052
- * 港澳通行证(回乡证)号码校验。
1053
- * 说明:常见为 `H/M` 开头 + 8~10 位数字;自动移除输入中的空格与 `-`。
1054
- * @param s 证件号
1055
- * @returns 是否为港澳通行证格式
1056
- * @example
1057
- * isHKMOPermit('H12345678') // true
1058
- * isHKMOPermit('M1234567890') // true
1059
- */
1060
- declare function isHKMOPermit(s: string): boolean;
1061
- /**
1062
- * 台湾居民来往大陆通行证(台胞证)号码校验。
1063
- * 说明:常见为 8 位纯数字;或首字母 + 8 位数字;部分场景存在 10 位数字。
1064
- * @param s 证件号
1065
- * @returns 是否为台胞证格式
1066
- * @example
1067
- * isTaiwanPermit('12345678') // true
1068
- * isTaiwanPermit('T12345678') // true
1069
- * isTaiwanPermit('1234567890') // true
1070
- */
1071
- declare function isTaiwanPermit(s: string): boolean;
1072
- /**
1073
- * 军官证号码校验:字母数字组合,长度 7-18。
1074
- * @param s 证件号
1075
- * @returns 是否为军官证宽松格式
1076
- * @example
1077
- * isOfficerIdLoose('JX1234567') // true
1078
- */
1079
- declare function isOfficerId(s: string): boolean;
1080
- /**
1081
- * 士兵证号码校验:字母数字组合,长度 7-18。
1082
- * @param s 证件号
1083
- * @returns 是否为士兵证格式
1084
- * @example
1085
- * isSoldierId('SB12345678') // true
1086
- */
1087
- declare function isSoldierId(s: string): boolean;
1088
- /**
1089
- * 中国军证(军官证/士兵证)组合校验。
1090
- * @param s 证件号
1091
- * @returns 是否为军官证或士兵证格式
1092
- * @example
1093
- * isCnMilitaryId('JX1234567') // true
1094
- */
1095
- declare function isMilitaryId(s: string): boolean;
1096
- /**
1097
- * 银行卡号校验(Luhn 校验)。
1098
- * 说明:移除空格与 `-` 后进行 Luhn 校验;长度通常为 12-19 位。
1099
- * @param s 银行卡号
1100
- * @returns 是否通过 Luhn 校验
1101
- * @example
1102
- * isBankCard('6222 0201 2345 6789') // true
1103
- */
1104
- declare function isBankCard(s: string): boolean;
1105
- /**
1106
- * 中国车牌号校验(含普通与新能源)。
1107
- * @param s 车牌号码
1108
- * @returns 是否为合法中国车牌
1109
- * @example
1110
- * isLicensePlate('京A12345') // true
1111
- * isLicensePlate('沪A12345D') // 新能源(末位 D/F)
1112
- * isLicensePlate('粤BDF12345') // 新能源(第三位 D/F)
1113
- */
1114
- declare function isLicensePlate(s: string): boolean;
1115
- /**
1116
- * 校验统一社会信用代码(中国税号常用:18 位,含校验位)。
1117
- * 规则:
1118
- * - 字符集:数字与大写字母(不含 I/O/Z/S/V),即 `[0-9A-HJ-NPQRTUWXY]`;
1119
- * - 前 17 位参与加权求和,最后一位为校验码(取值 0-9 或 大写字母)。
1120
- * @param code 税号/统一社会信用代码
1121
- * @returns 是否为合法税号
1122
- * @example
1123
- * isTaxID('91350100M000100Y43') // true/false 取决于校验位
1124
- */
1125
- declare function isTaxID(code: string): boolean;
1126
- /**
1127
- * 判断字符串是否为合法 JSON 文本。
1128
- * 说明:传入字符串时尝试 `JSON.parse`;传入对象/数组则视为合法。
1129
- * @param input 待判定的值或字符串
1130
- * @returns 是否为合法 JSON
1131
- * @example
1132
- * isJSON('{"a":1}') // true
1133
- * isJSON('[1,2]') // true
1134
- * isJSON('abc') // false
1135
- */
1136
- declare function isJSON(input: unknown): boolean;
1137
- /**
1138
- * HEX 颜色值(支持 `#RGB`、`#RRGGBB`、`#RRGGBBAA`)。
1139
- * @param s 颜色字符串
1140
- * @returns 是否为合法 HEX 颜色
1141
- * @example
1142
- * isHexColor('#fff') // true
1143
- * isHexColor('#00ff00') // true
1144
- * isHexColor('#11223344') // true
1145
- */
1146
- declare function isHexColor(s: string): boolean;
1147
- /**
1148
- * 校验 URL(要求含协议,支持 http/https/ftp)。
1149
- * @param s 待校验的地址
1150
- * @returns 是否为合法 URL
1151
- * @example
1152
- * isURL('https://example.com/path?a=1') // true
1153
- * isURL('example.com') // false(缺少协议)
1154
- */
1155
- declare function isURL(s: string): boolean;
1156
- /**
1157
- * 判断是否为合法 IPv6 地址(支持压缩表示与 IPv4 映射)。
1158
- * 规则:
1159
- * - 由 8 组 1~4 位十六进制数组成,允许一次 `::` 压缩;
1160
- * - 允许最后一组使用 IPv4 映射(如 `::ffff:192.168.0.1`)。
1161
- * @param s IP 字符串
1162
- * @returns 是否为合法 IPv6
1163
- * @example
1164
- * isIPv6('2001:0db8:85a3:0000:0000:8a2e:0370:7334') // true
1165
- * isIPv6('2001:db8::8a2e:370:7334') // true
1166
- * isIPv6('2001:::370:7334') // false
1167
- */
1168
- declare function isIPv6(s: string): boolean;
1169
- /**
1170
- * 校验 IP(支持 IPv4 与 IPv6)。
1171
- * @param s IP 字符串
1172
- * @param version 指定版本:传 `4` 仅校验 IPv4,传 `6` 仅校验 IPv6;缺省同时校验两者
1173
- * @returns 是否为合法 IP
1174
- * @example
1175
- * isIP('127.0.0.1') // true
1176
- * isIP('::1') // true
1177
- * isIP('127.0.0.1', 6) // false
1178
- */
1179
- declare function isIP(s: string, version?: 4 | 6 | '4' | '6'): boolean;
1180
- /**
1181
- * 校验 CIDR IP 段(支持 IPv4/IPv6),形如 `IP/前缀长度`。
1182
- * @param s CIDR 字符串,如 `192.168.0.0/24`、`2001:db8::/32`
1183
- * @returns 是否为合法 CIDR
1184
- * @example
1185
- * isIPRange('10.0.0.0/8') // true
1186
- * isIPRange('2001:db8::/129') // false
1187
- */
1188
- declare function isIPRange(s: string): boolean;
1189
- /**
1190
- * 端口号校验(0 ~ 65535,整数)。
1191
- * @param s 端口(字符串或数字)
1192
- * @returns 是否为合法端口范围
1193
- * @example
1194
- * isPortNumber(80) // true
1195
- * isPortNumber('65535') // true
1196
- * isPortNumber(70000) // false
1197
- */
1198
- declare function isPortNumber(s: string | number): boolean;
1199
- /**
1200
- * 纬度校验(-90 ~ 90)。
1201
- * @param s 纬度值(字符串或数字)
1202
- * @returns 是否为合法纬度
1203
- * @example
1204
- * isLatitude('31.2304') // true
1205
- */
1206
- declare function isLatitude(s: string | number): boolean;
1207
- /**
1208
- * 经度校验(-180 ~ 180)。
1209
- * @param s 经度值(字符串或数字)
1210
- * @returns 是否为合法经度
1211
- * @example
1212
- * isLongitude('121.4737') // true
1213
- */
1214
- declare function isLongitude(s: string | number): boolean;
1215
-
1216
- export { type Brand, type DeepPartial, type DeepRequired, EventBus, type Exact, type JsonArray, type JsonObject, type JsonPrimitive, type JsonValue, type KeysOfType, type Merge, type Mutable, type Nullable, type NumLike, type OSSAudioOption, type OSSBlurOption, type OSSCircleOption, type OSSCropOption, type OSSFormat, type OSSGravity, type OSSHlsOption, type OSSImgOption, type OSSIndexCropOption, type OSSOption, type OSSQualityOption, type OSSResizeOption, type OSSRoundedCornersOption, type OSSVideoOption, type OSSWatermarkOption, type OptionalKeys, type PickByType, type QnAudioOption, type QnAvthumbOption, type QnHlsOption, type QnImageView2Option, type QnImgOption, type QnMogr2Option, type QnVframeOption, type QnVideoOption, type QnWatermarkOption, type ReadonlyDeep, type RequiredKeys, type SetOptional, type SetRequired, type UnionToIntersection, type ValueOf, appendUrlParam, arrayMove, big, bigAdd, bigCompare, bigDiv, bigEqual, bigGt, bigGte, bigLt, bigLte, bigMul, bigPow, bigRound, bigSub, createRandId, createTimeRandId, createUUID, getAgeByBirthdate, getCountdownParts, getDateRangeAfter, getDateRangeBefore, getFileSuffix, getFileType, getObjectKeys, getStringByteLength, getUrlNumber, getUrlParam, getUrlParamAll, isBankCard, isChinese, isChineseName, isDigits, isEmail, isHKMOPermit, isHexColor, isIP, isIPRange, isIPv6, isIdentityCard, isJSON, isLandline, isLatitude, isLetter, isLicensePlate, isLongitude, isMilitaryId, isMobilePhone, isNumeric, isOfficerId, isPassport, isPhone, isPortNumber, isSoldierId, isTaiwanPermit, isTaxID, isURL, randomBoolean, randomFloat, randomInt, toAsync, toChineseCurrency, toChineseNum, toDayjs, toFixed, toMaskName, toMaskPhone, toMaskText, toThousandth, withDistance, withUnit, withUnitPx, zeroPad };