-
Notifications
You must be signed in to change notification settings - Fork 599
Expand file tree
/
Copy pathlibUUID.js
More file actions
57 lines (57 loc) · 2.1 KB
/
Copy pathlibUUID.js
File metadata and controls
57 lines (57 loc) · 2.1 KB
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// libUUID v0.0.1 by GUD Team | Tired of copying and pasting a UUID function over and over? Me too. This script provides a couple of functions to generate UUIDs.
class libUUID {
static base64Chars = "-0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz";
static base = 64;
static previousTime = 0;
static counter = new Array(12).fill(0);
static toBase64(num, length) {
let result = "";
for (let i = 0; i < length; i++) {
result = this.base64Chars[num % this.base] + result;
num = Math.floor(num / this.base);
}
return result;
}
;
static generateRandomBase64(length) {
let result = "";
for (let i = 0; i < length; i++) {
result += this.base64Chars[Math.floor(Math.random() * this.base)];
}
return result;
}
;
static generateUUID() {
const currentTime = Date.now();
const timeBase64 = this.toBase64(currentTime, 8);
let randomOrCounterBase64 = "";
if (currentTime === this.previousTime) {
// Increment the counter
for (let i = this.counter.length - 1; i >= 0; i--) {
this.counter[i]++;
if (this.counter[i] < this.base) {
break;
}
else {
this.counter[i] = 0;
}
}
randomOrCounterBase64 = this.counter.map(index => this.base64Chars[index]).join("");
}
else {
// Generate new random values and initialize counter with random starting values
randomOrCounterBase64 = this.generateRandomBase64(12);
// Initialize counter with random values instead of zeros to avoid hyphen-heavy sequences
for (let i = 0; i < this.counter.length; i++) {
this.counter[i] = Math.floor(Math.random() * this.base);
}
this.previousTime = currentTime;
}
return timeBase64 + randomOrCounterBase64;
}
;
static generateRowID() {
return this.generateUUID().replace(/_/g, "Z");
}
;
}