-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuuid.js
31 lines (26 loc) · 1.01 KB
/
uuid.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
function* randomNumberGenerator(buffer) {
while(true) {
for(const byte of crypto.getRandomValues(buffer)) {
yield Math.trunc(byte/16);
yield byte%16;
}
}
}
function uuidV7Timestamp() {
const unpaddedTimestamp = new Date().getTime().toString(16);
const timestamp = ('000000000000' + unpaddedTimestamp).substring(unpaddedTimestamp.length);
return `${timestamp.substring(0,8)}-${timestamp.substring(8)}`
}
const hexAlphabet = '0123456789abcdef';
const randomNumbers = randomNumberGenerator(new Uint8Array(256));
const hexDigitGenerators = {
x: () => hexAlphabet[randomNumbers.next().value],
y: () => hexAlphabet[randomNumbers.next().value % 4 + 8],
}
function withHexDigit(character) {
return hexDigitGenerators[character]();
}
export const randomUUIDV4 = crypto.randomUUID
? (()=>crypto.randomUUID())
: (()=>'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, withHexDigit))
export const randomUUIDV7 = (() => uuidV7Timestamp() + '-7xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, withHexDigit))