Skip to content

Commit caa6536

Browse files
authored
refactor(@142vip/utils): 优化VipNanoId类核心逻辑 (#733)
1 parent c68c3e0 commit caa6536

File tree

1 file changed

+40
-21
lines changed

1 file changed

+40
-21
lines changed

packages/utils/src/pkgs/nanoid.ts

Lines changed: 40 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -13,28 +13,47 @@ export enum Alphabet {
1313
ONLY_CHAR = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',
1414
COMPLEX = '-0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',
1515
}
16-
/**
17-
* 获取NanoId对象
18-
* @param alphabet
19-
*/
20-
function getNanoId(alphabet?: string): (size?: number) => string {
21-
return customAlphabet(alphabet ?? Alphabet.DEFAULT)
22-
}
2316

24-
/**
25-
* 获取随机字符串
26-
* - 按照制定规则生成默认字符串
27-
*/
28-
function getRandomId(size?: number): string {
29-
return nanoid(size)
30-
}
17+
export class VipNanoId {
18+
/**
19+
* 获取NanoId对象,默认字符集为 Alphabet.DEFAULT,只有小写字母和数字
20+
* @param alphabet
21+
*/
22+
public getNanoId(alphabet?: string): (size?: number) => string {
23+
return customAlphabet(alphabet ?? Alphabet.DEFAULT)
24+
}
3125

32-
export interface IVipNanoId {
33-
getRandomId: typeof getRandomId
34-
getNanoId: typeof getNanoId
35-
}
26+
/**
27+
* 获取随机字符串,默认长度21
28+
* - 按照制定规则生成默认字符串
29+
*/
30+
public getRandomId(size?: number): string {
31+
return nanoid(size)
32+
}
33+
34+
/**
35+
* 获取纯数字的随机字符串
36+
* @param size
37+
*/
38+
public getRandomNumberId(size?: number): string {
39+
return this.getNanoId(Alphabet.ONLY_NUMBER)(size)
40+
}
3641

37-
export const VipNanoId: IVipNanoId = {
38-
getRandomId,
39-
getNanoId,
42+
/**
43+
* 获取纯字母的随机字符串,包含小写字母和大写字母
44+
* @param size
45+
*/
46+
public getRandomCharId(size?: number): string {
47+
return this.getNanoId(Alphabet.ONLY_CHAR)(size)
48+
}
49+
50+
/**
51+
* 获取复杂的随机字符串,包含数字、小写字母、大写字母和特殊字符
52+
* @param size
53+
*/
54+
public getRandomComplexId(size?: number): string {
55+
return this.getNanoId(Alphabet.COMPLEX)(size)
56+
}
4057
}
58+
59+
export const vipNanoId = new VipNanoId()

0 commit comments

Comments
 (0)