1
2
3
4
const hexList: string[] = [];
for (let i = 0; i <= 15; i++) {
hexList[i] = i.toString(16);
}
vben
authored
4 years ago
5
6
7
8
9
10
11
12
13
export function buildUUID(): string {
let uuid = '';
for (let i = 1; i <= 36; i++) {
if (i === 9 || i === 14 || i === 19 || i === 24) {
uuid += '-';
} else if (i === 15) {
uuid += 4;
} else if (i === 20) {
vben
authored
5 years ago
14
uuid += hexList[(Math.random() * 4) | 8];
15
16
17
18
19
20
} else {
uuid += hexList[(Math.random() * 16) | 0];
}
}
return uuid.replace(/-/g, '');
}
vben
authored
5 years ago
21
22
let unique = 0;
Vben
authored
4 years ago
23
export function buildShortUUID(prefix = ''): string {
vben
authored
5 years ago
24
25
26
27
28
const time = Date.now();
const random = Math.floor(Math.random() * 1000000000);
unique++;
return prefix + '_' + random + unique + String(time);
}