ph-utils 0.20.0 → 1.0.0

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/lib/index.d.ts CHANGED
@@ -1,15 +1,160 @@
1
+ //#region src/common.d.ts
1
2
  /**
2
3
  * 验证参数是否为空
3
4
  * @param str 待验证的参数
4
5
  * @param ignoreWhitespace 如果是字符串是否忽略空格(包括空白字符串以及[\r\t\n]之类的制表符),默认为true
5
6
  */
6
- export declare function isBlank(str?: any, ignoreWhitespace?: boolean): boolean;
7
+ declare function isBlank(str?: any, ignoreWhitespace?: boolean): boolean;
7
8
  /**
8
- * 屏蔽手机号,中间部分用 * 展示
9
+ * 屏蔽手机号,中间部分用 * 展示(保持向后兼容)
9
10
  * @param mobile 待屏蔽的手机号
10
- * @returns 屏蔽后的手机号,例如:123 **** 1234
11
+ * @param placeholder 遮掩字符,默认: '*'
12
+ * @returns 屏蔽后的手机号,例如:138****5678
11
13
  */
12
- export declare function shieldMobile(mobile: string): string;
14
+ declare function shieldMobile(mobile: string, placeholder?: string): string;
15
+ /**
16
+ * 通用的字符串遮掩配置
17
+ */
18
+ interface ShieldOptions {
19
+ /** 需要遮掩的字符串 */
20
+ str: string;
21
+ /** 开始保留的字符数(从开头计数),默认: 3 */
22
+ startKeep?: number;
23
+ /** 结束保留的字符数(从结尾计数),默认: 4 */
24
+ endKeep?: number;
25
+ /** 遮掩字符,默认: '*' */
26
+ placeholder?: string;
27
+ /** 是否在遮掩部分前后添加空格,默认: false */
28
+ addSpaces?: boolean;
29
+ /** 自定义遮掩逻辑函数,如果提供则忽略其他配置 */
30
+ customShield?: (str: string) => string;
31
+ /** 最大输出长度,超过则截断,默认: 不限制 */
32
+ maxLength?: number;
33
+ /** 截断时是否显示省略号,默认: true */
34
+ showEllipsis?: boolean;
35
+ /** 省略号字符,默认: '...' */
36
+ ellipsis?: string;
37
+ }
38
+ /**
39
+ * 通用字符串遮掩函数
40
+ * @param options 配置参数
41
+ * @returns 遮掩后的字符串
42
+ *
43
+ * @example
44
+ * // 基本遮掩: 13812345678 -> 138****5678
45
+ * shieldString({ str: '13812345678' })
46
+ *
47
+ * @example
48
+ * // 限制长度: 64位字符串 -> 只显示前20位
49
+ * shieldString({
50
+ * str: '1234567890123456789012345678901234567890123456789012345678901234',
51
+ * startKeep: 8,
52
+ * endKeep: 8,
53
+ * maxLength: 20
54
+ * })
55
+ * // 结果: 12345678****87654321
56
+ *
57
+ * @example
58
+ * // 带省略号截断
59
+ * shieldString({
60
+ * str: '1234567890123456789012345678901234567890123456789012345678901234',
61
+ * startKeep: 8,
62
+ * endKeep: 8,
63
+ * maxLength: 15,
64
+ * showEllipsis: true
65
+ * })
66
+ * // 结果: 12345678...8765
67
+ */
68
+ declare function shieldString(options: ShieldOptions): string;
69
+ /**
70
+ * 简化版:遮掩并限制长度
71
+ * @param str 待遮掩的字符串
72
+ * @param options 配置选项
73
+ * @returns 遮掩并截断后的字符串
74
+ *
75
+ * @example
76
+ * // 遮掩手机号并限制显示长度
77
+ * shieldWithLimit('13812345678', { maxLength: 8 }) // 138****78
78
+ *
79
+ * @example
80
+ * // 遮掩长字符串
81
+ * shieldWithLimit('12345678901234567890', {
82
+ * startKeep: 4,
83
+ * endKeep: 4,
84
+ * maxLength: 12
85
+ * }) // 1234****7890
86
+ */
87
+ declare function shieldWithLimit(str: string, options: Omit<ShieldOptions, "str"> & {
88
+ maxLength: number;
89
+ }): string;
90
+ /**
91
+ * 智能摘要:根据重要性自动调整保留位数
92
+ * @param str 待处理的字符串
93
+ * @param maxLength 最大输出长度
94
+ * @param placeholder 遮掩字符
95
+ * @returns 摘要后的字符串
96
+ *
97
+ * @example
98
+ * // 长字符串自动摘要
99
+ * smartSummary('12345678901234567890', 10) // 1234****90
100
+ *
101
+ * @example
102
+ * // 短字符串不变
103
+ * smartSummary('12345', 10) // 12345
104
+ */
105
+ declare function smartSummary(str: string, maxLength: number, placeholder?: string): string;
106
+ /**
107
+ * 屏蔽邮箱
108
+ * @param email 待屏蔽的邮箱
109
+ * @param placeholder 遮掩字符,默认: '*'
110
+ * @returns 屏蔽后的邮箱,例如:exa****@email.com
111
+ */
112
+ declare function shieldEmail(email: string, placeholder?: string): string;
113
+ /**
114
+ * 屏蔽身份证号
115
+ * @param idCard 待屏蔽的身份证号
116
+ * @param placeholder 遮掩字符,默认: '*'
117
+ * @returns 屏蔽后的身份证号,例如:110101********1234
118
+ */
119
+ declare function shieldIdCard(idCard: string, placeholder?: string): string;
120
+ /**
121
+ * 屏蔽银行卡号(带空格格式化)
122
+ * @param bankCard 待屏蔽的银行卡号
123
+ * @param placeholder 遮掩字符,默认: '*'
124
+ * @returns 屏蔽后的银行卡号,例如:622848 **** **** 9018
125
+ */
126
+ declare function shieldBankCard(bankCard: string, placeholder?: string): string;
127
+ /**
128
+ * 屏蔽姓名
129
+ * @param name 待屏蔽的姓名
130
+ * @param placeholder 遮掩字符,默认: '*'
131
+ * @returns 屏蔽后的姓名,例如:张** 或 张*三
132
+ */
133
+ declare function shieldName(name: string, placeholder?: string): string;
134
+ /**
135
+ * 检测敏感信息并智能遮掩(支持长度控制)
136
+ * @param str 待检测的字符串
137
+ * @param placeholder 遮掩字符,默认: '*'
138
+ * @param maxLength 最大输出长度,默认: 不限制
139
+ * @returns 遮掩后的字符串,自动识别类型
140
+ *
141
+ * @example
142
+ * // 手机号:138****5678
143
+ * smartShield('13812345678')
144
+ *
145
+ * @example
146
+ * // 邮箱:exa****@email.com
147
+ * smartShield('example@email.com')
148
+ *
149
+ * @example
150
+ * // 身份证:110101********1234
151
+ * smartShield('110101199001011234')
152
+ *
153
+ * @example
154
+ * // 限制长度
155
+ * smartShield('13812345678', '*', 8) // 138****78
156
+ */
157
+ declare function smartShield(str: string, placeholder?: string, maxLength?: number): string;
13
158
  /**
14
159
  * 验证参数是否是数字
15
160
  * @param str 待验证的字符串
@@ -18,34 +163,34 @@ export declare function shieldMobile(mobile: string): string;
18
163
  * @param numericParam.isFloat 是否是小数, 默认: true
19
164
  * @returns true 是数字, false 不是数字
20
165
  */
21
- export declare function isNumeric(str: string, numericParam?: {
22
- isPositive?: boolean;
23
- isFloat?: boolean;
166
+ declare function isNumeric(str: string, numericParam?: {
167
+ isPositive?: boolean;
168
+ isFloat?: boolean;
24
169
  }): boolean;
25
170
  /**
26
171
  * 验证参数是否是Boolean 类型
27
172
  * @param str 待验证的字符串
28
173
  * @returns
29
174
  */
30
- export declare function isBoolean(str: string): boolean;
175
+ declare function isBoolean(str: string): boolean;
31
176
  /** 生成随机数的选项 */
32
177
  interface RandomStringOption {
33
- /** 生成指定长度的随机字符串 */
34
- length: number;
35
- /** 是否包含英文字母, 默认为: true */
36
- hasLetter?: boolean;
37
- /** 生成纯数字的随机数时, 首位是否允许为 0, 默认为: true */
38
- firstIsZero?: boolean;
178
+ /** 生成指定长度的随机字符串 */
179
+ length: number;
180
+ /** 是否包含英文字母, 默认为: true */
181
+ hasLetter?: boolean;
182
+ /** 生成纯数字的随机数时, 首位是否允许为 0, 默认为: true */
183
+ firstIsZero?: boolean;
39
184
  }
40
185
  interface RangeRandomOption {
41
- /** 配合 max 生成 [min~max] 之间的随机数 */
42
- min: number;
43
- /** 配合 min 生成 [min~max] 之间的随机数 */
44
- max: number;
45
- /** 生成的随机数,是否包含 max, 默认: false */
46
- hasEnd?: boolean;
47
- /** 生成的随机数是否是整数, 默认: true */
48
- isInteger?: boolean;
186
+ /** 配合 max 生成 [min~max] 之间的随机数 */
187
+ min: number;
188
+ /** 配合 max 生成 [min~max] 之间的随机数 */
189
+ max: number;
190
+ /** 生成的随机数,是否包含 max, 默认: false */
191
+ hasEnd?: boolean;
192
+ /** 生成的随机数是否是整数, 默认: true */
193
+ isInteger?: boolean;
49
194
  }
50
195
  /**
51
196
  * 生成指定长度的随机数
@@ -55,7 +200,7 @@ interface RangeRandomOption {
55
200
  * @example <caption>1. 生成指定长度的随机字符串</caption>
56
201
  * random(1); // 长度为 1 的随机字符串
57
202
  */
58
- export declare function random(len: number): string;
203
+ declare function random(len: number): string;
59
204
  /**
60
205
  * 生成介于 [min, max] 之间的随机数
61
206
  *
@@ -65,7 +210,7 @@ export declare function random(len: number): string;
65
210
  * @param option.hasEnd 生成的随机数,是否包含 max, 默认: false
66
211
  * @param option.isInteger 生成的随机数是否是整数, 默认: true
67
212
  */
68
- export declare function random(option: RangeRandomOption): number;
213
+ declare function random(option: RangeRandomOption): number;
69
214
  /**
70
215
  * 生成指定长度随机数
71
216
  *
@@ -77,40 +222,19 @@ export declare function random(option: RangeRandomOption): number;
77
222
  * @example <caption>2. 生成纯数字且首位不能为0长度为1的随机字符</caption>
78
223
  * random({ length: 1, hasLetter: false, firstIsZero: false })
79
224
  */
80
- export declare function random(option: RandomStringOption): string;
81
- /**
82
- * 带有错误名称标记的错误类型
83
- */
84
- export declare class BaseError extends Error {
85
- /**
86
- * 错误名称,类似于 Java 中的不同的 Exception[NullPointerException];
87
- * 增加 name 字段,表明不同的错误,当需要根据不同的错误执行不同的处理的时候,会很有用
88
- */
89
- name: string;
90
- /**
91
- * 构造一个 name = BaseError 的错误信息
92
- * @param message 错误描述
93
- */
94
- constructor(message: string);
95
- /**
96
- *
97
- * @param name 错误名称
98
- * @param message 错误描述
99
- */
100
- constructor(name: string, message: string);
101
- }
225
+ declare function random(option: RandomStringOption): string;
102
226
  /**
103
227
  * 将金额数字格式化为金额格式显示并且会保留两位小数[去除多余的位数,不是四舍五入,而是直接舍去] 1234523432.23 => 123,123,123.23
104
228
  * @param {number} number 待转换的金额数字
105
229
  * @return string
106
230
  */
107
- export declare function formatMoney(number: number): string;
231
+ declare function formatMoney(number: number): string;
108
232
  /**
109
233
  * 将风格由大写风格转换为下划线风格: HelloWorld -> hello-world
110
234
  * @param name 命名, 例如: HelloWorld
111
235
  * @param connector 连接符, 默认为: _
112
236
  */
113
- export declare function snakeCaseStyle(name: string, connector?: string): string;
237
+ declare function snakeCaseStyle(name: string, connector?: string): string;
114
238
  /**
115
239
  * 对数字进行四舍五入处理
116
240
  * @param num 需要进行四舍五入的数字
@@ -121,21 +245,19 @@ export declare function snakeCaseStyle(name: string, connector?: string): string
121
245
  * 2: 向下取整
122
246
  * @returns 返回经过指定方式舍入后的数字
123
247
  */
124
- export declare function round(num: number, precision?: number, roundType?: 0 | 1 | 2): number;
248
+ declare function round(num: number, precision?: number, roundType?: 0 | 1 | 2): number;
125
249
  /**
126
250
  * 反转字符串
127
251
  */
128
- export declare function reverseStr(str: string): string;
252
+ declare function reverseStr(str: string): string;
129
253
  /** 数据格式化配置 */
130
254
  interface FormDataConfig<T> {
131
- /** 配置需要转换为数字的字段 */
132
- numberFields?: (keyof T)[];
133
- /** 配置需要转换为字符串的字段 */
134
- stringFields?: (keyof T)[];
135
- /** 自定义的格式化 */
136
- formatter?: {
137
- [K in keyof T]?: "number" | "string" | ((value: any) => number | string);
138
- };
255
+ /** 配置需要转换为数字的字段 */
256
+ numberFields?: (keyof T)[];
257
+ /** 配置需要转换为字符串的字段 */
258
+ stringFields?: (keyof T)[];
259
+ /** 自定义的格式化 */
260
+ formatter?: { [K in keyof T]?: "number" | "string" | ((value: any) => number | string); };
139
261
  }
140
262
  /**
141
263
  * 嵌套的 json 指定 key 数据
@@ -143,12 +265,444 @@ interface FormDataConfig<T> {
143
265
  * @param keys 待获取的数据 key, 可以通过 [.] 获取嵌套数据, 例如: a.b.c
144
266
  * @returns
145
267
  */
146
- export declare function getJSONValue(data: Record<string, any>, keystr: string): Record<string, any> | null;
268
+ declare function getJSONValue(data: Record<string, any>, keystr: string): Record<string, any> | null;
147
269
  /**
148
270
  * 数据格式化主要用于数据类型转换
149
271
  * @param data 待转换数据类型的数据
150
272
  * @param config 转换配置
151
273
  * @returns
152
274
  */
153
- export declare function formatData<T extends Record<string, any>>(data: T, config?: FormDataConfig<T>): T;
154
- export {};
275
+ declare function formatData<T extends Record<string, any>>(data: T, config?: FormDataConfig<T>): T;
276
+ //#endregion
277
+ //#region src/date.d.ts
278
+ /**
279
+ * 将日期格式化为指定形式的字符串
280
+ * @param date 日期
281
+ * @param pattern 格式化字符串 yyyy - 年, mm - 月, dd - 日, HH - 小时(24时制), MM - 分钟, ss - 秒, S - 毫秒, 默认: yyyy-mm-dd HH:MM:ss
282
+ */
283
+ declare function format(date?: Date | string | number | null, pattern?: string): string;
284
+ /**
285
+ * 将指定的参数解析为日期对象(Date)
286
+ * 参考 dayjs 实现, 也可以参考 https://github.com/nomiddlename/date-format
287
+ * @param date 待解析的日期参数
288
+ */
289
+ declare function parse(date?: Date | string | number | null): Date;
290
+ /**
291
+ * 设置日期的开始或者结束的点
292
+ * @param date 日期,能够被 parse 解析的日期
293
+ * @param unit 单位,Date[D]、Minute[M], 默认为 Date
294
+ * @param isEnd true则为 endOf
295
+ */
296
+ declare function dateOf(date?: Date | string | number | null, unit?: string, isEnd?: boolean): Date;
297
+ /**
298
+ * 设置日期的开始的点
299
+ * @param date 日期,能够被 parse 解析的日期
300
+ * @param unit 单位,Date[D]、Minute[M], 默认为 Date
301
+ * @returns
302
+ */
303
+ declare function startOf(date?: Date | string | number | null, unit?: string): Date;
304
+ /**
305
+ * 设置日期的结束点
306
+ * @param date 日期,能够被 parse 解析的日期
307
+ * @param unit 单位,Date[D]、Minute[M], 默认为 Date
308
+ * @returns
309
+ */
310
+ declare function endOf(date?: Date | string | number | null, unit?: string): Date;
311
+ /**
312
+ * 获取时间戳
313
+ * @param ctime 时间
314
+ * @param pre 精度, s - 精确到秒, ms - 精确到毫秒, 默认: s
315
+ * @returns
316
+ */
317
+ declare function timestamp(ctime?: Date | string | number, pre?: "s" | "ms"): number;
318
+ /**
319
+ * 日期加上指定时间后的日期
320
+ * @param date 指定的日期
321
+ * @param num 需要添加的数字, 如果这个参数传递一个小于0的数字,则就是日期减去相应的数字
322
+ * @param unit 需要添加的单位,date、month、year、hours、minute、second
323
+ *
324
+ * 查阅文档: {@link https://gitee.com/towardly/ph/wikis/utils/date ph-utils}
325
+ *
326
+ * @example <caption>1. 分钟加1并格式化显示时间</caption>
327
+ *
328
+ * add(new Date(), 1, 'minute', 'HHMMss')
329
+ */
330
+ declare function add(date: Date | string | number | null, num: number, unit: string): Date;
331
+ /**
332
+ * 日期加上指定时间后的日期
333
+ * @param date 指定的日期, 传递为 null ,则表示为当前日期
334
+ * @param num 需要添加的数字, 如果这个参数传递一个小于0的数字,则就是日期减去相应的数字
335
+ * @param unit 需要添加的单位,date - 加减天数
336
+ * @param fmt 如果传递了格式化的单位,则返回格式化后的日期, 格式化字符串 yyyy - 年, mm - 月, dd - 日, HH - 小时, MM - 分钟, ss - 秒
337
+ *
338
+ * #### 1. 前一天的日期字符串形式
339
+ * ```javascript
340
+ * add(new Date(), -1, 'Date', 'yyyy-mm-dd')
341
+ * ```
342
+ */
343
+ declare function add(date: Date | string | number | null, num: number, unit: string, fmt: string): string;
344
+ //#endregion
345
+ //#region src/color.d.ts
346
+ type RGBColorObject = {
347
+ r: number;
348
+ g: number;
349
+ b: number;
350
+ a?: number;
351
+ };
352
+ type HSVColorObject = {
353
+ h: number;
354
+ s: number;
355
+ v: number;
356
+ };
357
+ type ColorType = string | RGBColorObject | HSVColorObject;
358
+ /**
359
+ * 将输入的颜色值转换为RGB对象格式。
360
+ * @param color 可以是字符串, 也可以是一个 HSV 对象[一个包含 h、s、v 属性的对象]
361
+ * @returns 返回一个包含r、g、b和a(透明度)属性的RGB对象。
362
+ * @throws 如果输入的字符串不是有效的颜色表示,则抛出错误。
363
+ */
364
+ declare function toRgb(color: ColorType): RGBColorObject;
365
+ /**
366
+ * 将颜色转换为HSV颜色模型。
367
+ * @param color - 字符串或者RGB对象
368
+ * @returns 返回一个包含h、s、v属性的对象,代表HSV颜色值,其中h是色相(取值范围0到360),s是饱和度(取值范围0到1),v是明度(取值范围0到1)。
369
+ */
370
+ declare function toHsv(color: ColorType): HSVColorObject;
371
+ /**
372
+ * 将RGB颜色对象转换为十六进制颜色字符串。
373
+ * @param rgb - 包含红色(r), 绿色(g), 蓝色(b)成分的对象。
374
+ * @returns 返回一个表示RGB颜色的十六进制字符串,例如"#FF0000"。
375
+ */
376
+ declare function rgbToHex(rgb: RGBColorObject): string;
377
+ /**
378
+ * 将颜色转换为 16 进制字符串
379
+ * @param color - 颜色, 可以 rgb(0,0,0),rgba(0,0,0,0)字符串, 也可以是 rgb、hsv对象
380
+ * @returns 返回颜色的十六进制字符串,例如"#FF0000"
381
+ */
382
+ declare function toHex(color: ColorType): string;
383
+ /**
384
+ * 调整给定颜色深[darken]浅[lighten]
385
+ * @param color - 输入的颜色,可以是任意颜色表示方式
386
+ * @param level - 调整深浅级别, 可以是小数。默认: 1
387
+ * @param light - 控制调整的方向。如果为true,[lighten] 变浅,如果为false,[darken] 变深。默认: true
388
+ * 1. 颜色变浅
389
+ * adjust('#4998f4', 3, true)
390
+ * @returns 返回调整后颜色的十六进制字符串表示。
391
+ */
392
+ declare function adjust(color: ColorType, level?: number, light?: boolean): string;
393
+ //#endregion
394
+ //#region src/array.d.ts
395
+ /**
396
+ * 数组排序(不修改原数组,返回新数组)
397
+ * @param arr 待排序数组
398
+ * @param order 排序方向: "asc" - 升序(默认), "desc" - 降序
399
+ * @param orderKey 如果数组元素是对象,指定按哪个字段排序;为 null 时按元素本身排序
400
+ * @returns 排序后的新数组
401
+ */
402
+ declare function order<T>(arr: T[], order?: "asc" | "desc", orderKey?: keyof T | null): T[];
403
+ /** 重载:所有参数为 Set 时,返回 Set */
404
+ declare function intersection<T>(...arrs: Set<T>[]): Set<T>;
405
+ /** 重载:所有参数为 Array 时,返回 Array */
406
+ declare function intersection<T>(...arrs: T[][]): T[];
407
+ /** 重载:所有参数为 Set 时,返回 Set */
408
+ declare function difference<T>(...arrs: Set<T>[]): Set<T>;
409
+ /** 重载:所有参数为 Array 时,返回 Array */
410
+ declare function difference<T>(...arrs: T[][]): T[];
411
+ /** 重载:所有参数为 Set 时,返回 Set */
412
+ declare function union<T>(...arrs: Set<T>[]): Set<T>;
413
+ /** 重载:所有参数为 Array 时,返回 Array */
414
+ declare function union<T>(...arrs: T[][]): T[];
415
+ /** 重载:所有参数为 Set 时,返回 Set */
416
+ declare function symmetricDifference<T>(...arrs: Set<T>[]): Set<T>;
417
+ /** 重载:所有参数为 Array 时,返回 Array */
418
+ declare function symmetricDifference<T>(...arrs: T[][]): T[];
419
+ /**
420
+ * 判断 a1 是否是 a2 的子集(a1 的所有元素是否都在 a2 中)
421
+ * - 优先使用原生 ES2025 Set.prototype.isSubsetOf
422
+ * - 数组模式使用 Set.has 替代 includes,性能优化
423
+ * @param a1 待判断集合(可能是子集)
424
+ * @param a2 目标集合(可能是超集)
425
+ * @returns a1 ⊆ a2 时返回 true
426
+ */
427
+ declare function isSubsetOf<T>(a1: T[] | Set<T>, a2: T[] | Set<T>): boolean;
428
+ /**
429
+ * 判断 a1 是否是 a2 的超集(a2 的所有元素是否都在 a1 中)
430
+ * 逻辑等价于:a2 是 a1 的子集
431
+ * @param a1 待判断集合(可能是超集)
432
+ * @param a2 目标集合(可能是子集)
433
+ * @returns a1 ⊇ a2 时返回 true
434
+ */
435
+ declare function isSupersetOf<T>(a1: T[] | Set<T>, a2: T[] | Set<T>): boolean;
436
+ /**
437
+ * 判断两个集合是否没有公共元素(不相交)
438
+ * - 优先使用原生 ES2025 Set.prototype.isDisjointFrom
439
+ * - 数组模式使用 Set.has 替代 includes,性能优化
440
+ * @param a1 集合 1
441
+ * @param a2 集合 2
442
+ * @returns 两集合无交集时返回 true
443
+ */
444
+ declare function isDisjointFrom<T>(a1: T[] | Set<T>, a2: T[] | Set<T>): boolean;
445
+ //#endregion
446
+ //#region src/id.d.ts
447
+ type SnowflakeIDInfo = {
448
+ value: string;
449
+ timeOffset: bigint;
450
+ timestamp: number;
451
+ machineId: bigint;
452
+ sequence: bigint;
453
+ epoch: number;
454
+ version: string | undefined;
455
+ };
456
+ /** 雪花ID, 推荐在全局构造一个对象用于生成id */
457
+ declare class SnowflakeID {
458
+ private static readonly SEQUENCE_MASK;
459
+ private static readonly TIMESTAMP_MASK;
460
+ private static readonly MACHINE_MASK;
461
+ private static readonly SHIFT_TIMESTAMP;
462
+ private static readonly SHIFT_MACHINE;
463
+ private static readonly MAX_MACHINE_ID;
464
+ private static readonly DEFAULT_MAX_CLOCK_TOLERANCE;
465
+ private static readonly MAX_ID;
466
+ private static readonly STATES;
467
+ /** 机器码, 默认为: 1 */
468
+ readonly machineId: bigint;
469
+ /** 起始时间戳, 默认为:1288834974657 */
470
+ readonly epoch: bigint;
471
+ /** 版本号, 默认为: 0 */
472
+ private _version;
473
+ /** 允许的最大时钟回拨毫秒数, 默认为: 5 */
474
+ readonly maxClockTolerance: number;
475
+ private readonly _state;
476
+ /**
477
+ * 构造函数
478
+ *
479
+ * @param machineId 机器标识,默认为1
480
+ * @param epoch 时间戳起始值,默认为1288834974657
481
+ * @param maxClockTolerance 允许的最大时钟回拨毫秒数,默认为5
482
+ */
483
+ constructor(machineId?: number, epoch?: number, maxClockTolerance?: number);
484
+ get version(): number;
485
+ set version(version: number);
486
+ /**
487
+ * 生成雪花ID
488
+ *
489
+ * @returns 返回生成的唯一ID字符串
490
+ * @throws 如果时钟回拨超过容差阈值,抛出错误
491
+ */
492
+ generate(): string;
493
+ parse(snowflakeID: string, epoch?: number, includeVersion?: boolean): SnowflakeIDInfo;
494
+ }
495
+ /** 将uuid转换为更简单的唯一标记id */
496
+ declare class ShortUUID {
497
+ private alphabet;
498
+ /**
499
+ * 构造函数,用于初始化字母表
500
+ * @param {string} [alphabet] - 可选参数,用于指定自定义字母表
501
+ * 如果提供了alphabet参数,则将其设置为实例的字母表属性
502
+ * 如果未提供alphabet参数,则使用默认值
503
+ */
504
+ constructor(alphabet?: string);
505
+ /**
506
+ * 将UUID字符串进行编码处理
507
+ * @param uuid - 需要编码的UUID字符串
508
+ * @returns 编码后的字符串
509
+ */
510
+ encode(uuid: string, alphabet?: string): string;
511
+ /**
512
+ * 解码短UUID字符串,将其转换为UUID整数和十六进制格式
513
+ * @param shortUUID - 需要解码的短UUID字符串
514
+ * @returns 返回包含UUID整数和十六进制格式的对象
515
+ */
516
+ decode(shortUUID: string, alphabet?: string): {
517
+ uuidInt: bigint;
518
+ uuid: string;
519
+ };
520
+ private _stringToInt;
521
+ private _intToString;
522
+ private _uuidHexToInt;
523
+ private _uuidIntToHex;
524
+ }
525
+ //#endregion
526
+ //#region src/crypto.d.ts
527
+ /**
528
+ * 将原始的二进制数据转换为 Hex String
529
+ * @param bf 待转换的原始数据
530
+ * @param upper 是否需要转换为大写
531
+ * @returns
532
+ */
533
+ declare function bufferToHex(bf: ArrayBuffer | Uint8Array, upper?: boolean): string;
534
+ /**
535
+ * SHA 哈希算法
536
+ * @param message 待进行 hash 的数据
537
+ * @param upper 是否转换为大写, 默认为: false
538
+ * @param algorithm hash算法, 支持: SHA-1、SHA-256、SHA-384、SHA-512; 默认为: SHA-256
539
+ * @returns
540
+ */
541
+ declare function sha(message: string | ArrayBuffer, upper?: boolean, algorithm?: string): Promise<string>;
542
+ /**
543
+ * 哈希算法
544
+ * @param message 待进行 hash 的数据
545
+ * @param upper 是否转换为大写, 默认为: false
546
+ * @param algorithm hash算法, 支持: SHA-1、SHA-256、SHA-384、SHA-512; 默认为: SHA-256
547
+ * @returns
548
+ */
549
+ declare function hash(message: string | ArrayBuffer, upper?: boolean, algorithm?: string): Promise<string>;
550
+ type HMACAlgorithm = "SHA-256" | "SHA-512";
551
+ /**
552
+ * 使用 HMAC 算法计算消息的哈希值
553
+ * @param message - 需要计算哈希的消息字符串
554
+ * @param secret - 用于生成 HMAC 的密钥
555
+ * @param algorithm - HMAC 使用的哈希算法,默认为 "SHA-256"
556
+ * @param upper - 是否将结果转换为大写,默认为 false
557
+ * @returns 返回十六进制格式的 HMAC 哈希值
558
+ */
559
+ declare function hmacHash(message: string, secret: string, algorithm?: HMACAlgorithm, upper?: boolean): Promise<string>;
560
+ /** 返回结果类似 */
561
+ type AlgorithmResType = "hex" | "hexUpper" | "base64" | "raw";
562
+ /**
563
+ * AES 加密
564
+ * @param message 待加密的数据
565
+ * @param key 加解密密钥
566
+ * @param encode 加密后的数据转换的形式, hex - 转换为16进制字符串, hexUpper - 转换为16进制且大写, base64 - 转换为 base64 形式
567
+ * @param iv 加解密向量
568
+ * @returns [加密数据,向量]
569
+ */
570
+ declare function aesEncrypt(message: string, key: string, encode?: AlgorithmResType, iv?: null | Uint8Array | string): Promise<{
571
+ ciphertext: string | ArrayBuffer;
572
+ iv: string;
573
+ key: string;
574
+ }>;
575
+ /**
576
+ * AES 解密
577
+ * @param message 加密后的数据
578
+ * @param key 解密密钥
579
+ * @param iv 向量
580
+ * @param encode 加密后数据的形式: hex | base64
581
+ * @returns
582
+ */
583
+ declare function aesDecrypt(message: Uint8Array | string, key: string, iv: string, encode?: AlgorithmResType): Promise<string>;
584
+ /**
585
+ * RSA 加密
586
+ * @param key 公钥
587
+ * @param message 待加密数据
588
+ * @param encode 返回类型
589
+ * @returns
590
+ */
591
+ declare function rsaEncrypt(message: string, publicKey: string, encode?: AlgorithmResType): Promise<string | ArrayBuffer>;
592
+ /**
593
+ * RSA 解密
594
+ * @param key 私钥, 根据私钥解密
595
+ * @param message 加密后的数据
596
+ * @param encode 加密后的数据形式
597
+ * @returns
598
+ */
599
+ declare function rsaDecrypt(privateKey: string, message: Uint8Array | string, encode?: AlgorithmResType): Promise<string>;
600
+ //#endregion
601
+ //#region src/base-codec.d.ts
602
+ declare class BaseCodec {
603
+ protected readonly alphabet: string;
604
+ protected readonly base: bigint;
605
+ protected readonly name: string;
606
+ constructor(name: string, alphabet: string);
607
+ protected intToBytes(value: bigint): Uint8Array;
608
+ protected bytesToInt(bytes: Uint8Array): bigint;
609
+ protected concatBytes(...arrays: Uint8Array[]): Uint8Array;
610
+ protected bytesToUtf8(bytes: Uint8Array): string;
611
+ protected utf8ToBytes(str: string): Uint8Array;
612
+ protected bytesToHex(bytes: Uint8Array): string;
613
+ protected encodeFromBytes(input: Uint8Array): string;
614
+ protected decodeToBytes(input: string): Uint8Array;
615
+ /** 将非负 bigint 编码为字符串 */
616
+ encodeFromInt(value: bigint): string;
617
+ /** 先将字符串转换为 UTF-8 字节,再编码 */
618
+ encodeFromStr(value: string): string;
619
+ /** 根据参数类型将 bigint 或字符串编码 */
620
+ encode(value: bigint | string): string;
621
+ /** 解码为非负 bigint */
622
+ decodeToInt(value: string): bigint;
623
+ /** 解码为 UTF-8 字符串 */
624
+ decodeToStr(value: string): string;
625
+ /** 默认解码为 UTF-8 字符串 */
626
+ decode(value: string): string;
627
+ /** 将字节数组转换为十六进制字符串 */
628
+ bufferToHex(value: Uint8Array): string;
629
+ }
630
+ declare class Base32 extends BaseCodec {
631
+ constructor();
632
+ }
633
+ declare class Base62 extends BaseCodec {
634
+ constructor();
635
+ }
636
+ //#endregion
637
+ //#region src/validator.d.ts
638
+ interface RuleItem {
639
+ rule: RegExp | ((v: any) => boolean) | "required";
640
+ message: string;
641
+ sameKey?: string;
642
+ }
643
+ type RuleType = string | RegExp | ((v: any) => boolean) | (RegExp | string | ((v: any) => boolean) | {
644
+ rule: string | RegExp | ((v: any) => boolean);
645
+ message?: string;
646
+ });
647
+ interface SchemaType {
648
+ /** 数据字段 */
649
+ key: string;
650
+ /** 是否必须 */
651
+ required?: boolean;
652
+ /** 验证规则列表 */
653
+ rules?: RuleType[];
654
+ /** 错误信息 */
655
+ message?: string;
656
+ }
657
+ /**
658
+ * 数据验证器
659
+ */
660
+ declare class Validator {
661
+ rules: Record<string, RuleItem[]>;
662
+ /**
663
+ * 构造数据验证转换器
664
+ *
665
+ * See {@link https://gitee.com/towardly/ph/wikis/utils/validator|Validator文档}.
666
+ *
667
+ * @param schemas 配置验证转换规则
668
+ *
669
+ * @example
670
+ *
671
+ * const validator = new Validator([
672
+ * { key: 'mobile', rules: ['required', 'mobile'] },
673
+ * { key: 'code': rules: /^\d{6}$/, message: '请输入正确的验证码' },
674
+ * { key: 'confirmPassword', rules: ['required', 'same:password'] }
675
+ * ])
676
+ * // 验证某一个字段
677
+ * validator.validateKey().then(res => {})
678
+ */
679
+ constructor(schemas: SchemaType[]);
680
+ addSchemas(schemas: SchemaType[]): void;
681
+ addSchema(schema: SchemaType): void;
682
+ setSchema(schema: SchemaType): void;
683
+ setSchemas(schemas: SchemaType[]): void;
684
+ removeSchema(key: string): void;
685
+ hasSchema(key: string): boolean;
686
+ /**
687
+ * 进行数据验证
688
+ * @param data 待验证的数据
689
+ * @param all 是否全部验证, false - 只要验证错误一个则停止验证
690
+ * @returns
691
+ */
692
+ validate(data: any, all?: boolean): boolean;
693
+ /**
694
+ * 只验证指定 key 的数据格式
695
+ * @param key 指定待验证的 key
696
+ * @param value 待验证的数据
697
+ * @param data 原始数据,当验证确认密码时需要使用
698
+ */
699
+ validateKey(key: string, value: any, data?: any): {
700
+ key: string;
701
+ value: any;
702
+ };
703
+ private _validateRule;
704
+ private _parseSchemaRules;
705
+ private _parseStringRule;
706
+ }
707
+ //#endregion
708
+ export { Base32, Base62, type RuleType, type SchemaType, ShortUUID, SnowflakeID, Validator, add, adjust, aesDecrypt, aesEncrypt, bufferToHex, dateOf, difference, endOf, format, formatData, formatMoney, getJSONValue, hash, hmacHash, intersection, isBlank, isBoolean, isDisjointFrom, isNumeric, isSubsetOf, isSupersetOf, order, parse, random, reverseStr, rgbToHex, round, rsaDecrypt, rsaEncrypt, sha, shieldBankCard, shieldEmail, shieldIdCard, shieldMobile, shieldName, shieldString, shieldWithLimit, smartShield, smartSummary, snakeCaseStyle, startOf, symmetricDifference, timestamp, toHex, toHsv, toRgb, union };