|
1 | 1 | // 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. |
2 | | -var libUUID = (function () { |
3 | | - 'use strict'; |
4 | | - |
5 | | - class libUUID { |
6 | | - static base64Chars = "-0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz"; |
7 | | - static base = 64; |
8 | | - static previousTime = 0; |
9 | | - static counter = new Array(12).fill(0); |
10 | | - static toBase64(num, length) { |
11 | | - let result = ""; |
12 | | - for (let i = 0; i < length; i++) { |
13 | | - result = this.base64Chars[num % this.base] + result; |
14 | | - num = Math.floor(num / this.base); |
15 | | - } |
16 | | - return result; |
| 2 | +class libUUID { |
| 3 | + static base64Chars = "-0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz"; |
| 4 | + static base = 64; |
| 5 | + static previousTime = 0; |
| 6 | + static counter = new Array(12).fill(0); |
| 7 | + static toBase64(num, length) { |
| 8 | + let result = ""; |
| 9 | + for (let i = 0; i < length; i++) { |
| 10 | + result = this.base64Chars[num % this.base] + result; |
| 11 | + num = Math.floor(num / this.base); |
17 | 12 | } |
18 | | - ; |
19 | | - static generateRandomBase64(length) { |
20 | | - let result = ""; |
21 | | - for (let i = 0; i < length; i++) { |
22 | | - result += this.base64Chars[Math.floor(Math.random() * this.base)]; |
23 | | - } |
24 | | - return result; |
| 13 | + return result; |
| 14 | + } |
| 15 | + ; |
| 16 | + static generateRandomBase64(length) { |
| 17 | + let result = ""; |
| 18 | + for (let i = 0; i < length; i++) { |
| 19 | + result += this.base64Chars[Math.floor(Math.random() * this.base)]; |
25 | 20 | } |
26 | | - ; |
27 | | - static generateUUID() { |
28 | | - const currentTime = Date.now(); |
29 | | - const timeBase64 = this.toBase64(currentTime, 8); |
30 | | - let randomOrCounterBase64 = ""; |
31 | | - if (currentTime === this.previousTime) { |
32 | | - // Increment the counter |
33 | | - for (let i = this.counter.length - 1; i >= 0; i--) { |
34 | | - this.counter[i]++; |
35 | | - if (this.counter[i] < this.base) { |
36 | | - break; |
37 | | - } |
38 | | - else { |
39 | | - this.counter[i] = 0; |
40 | | - } |
| 21 | + return result; |
| 22 | + } |
| 23 | + ; |
| 24 | + static generateUUID() { |
| 25 | + const currentTime = Date.now(); |
| 26 | + const timeBase64 = this.toBase64(currentTime, 8); |
| 27 | + let randomOrCounterBase64 = ""; |
| 28 | + if (currentTime === this.previousTime) { |
| 29 | + // Increment the counter |
| 30 | + for (let i = this.counter.length - 1; i >= 0; i--) { |
| 31 | + this.counter[i]++; |
| 32 | + if (this.counter[i] < this.base) { |
| 33 | + break; |
41 | 34 | } |
42 | | - randomOrCounterBase64 = this.counter.map(index => this.base64Chars[index]).join(""); |
43 | | - } |
44 | | - else { |
45 | | - // Generate new random values and initialize counter with random starting values |
46 | | - randomOrCounterBase64 = this.generateRandomBase64(12); |
47 | | - // Initialize counter with random values instead of zeros to avoid hyphen-heavy sequences |
48 | | - for (let i = 0; i < this.counter.length; i++) { |
49 | | - this.counter[i] = Math.floor(Math.random() * this.base); |
| 35 | + else { |
| 36 | + this.counter[i] = 0; |
50 | 37 | } |
51 | | - this.previousTime = currentTime; |
52 | 38 | } |
53 | | - return timeBase64 + randomOrCounterBase64; |
| 39 | + randomOrCounterBase64 = this.counter.map(index => this.base64Chars[index]).join(""); |
54 | 40 | } |
55 | | - ; |
56 | | - static generateRowID() { |
57 | | - return this.generateUUID().replace(/_/g, "Z"); |
| 41 | + else { |
| 42 | + // Generate new random values and initialize counter with random starting values |
| 43 | + randomOrCounterBase64 = this.generateRandomBase64(12); |
| 44 | + // Initialize counter with random values instead of zeros to avoid hyphen-heavy sequences |
| 45 | + for (let i = 0; i < this.counter.length; i++) { |
| 46 | + this.counter[i] = Math.floor(Math.random() * this.base); |
| 47 | + } |
| 48 | + this.previousTime = currentTime; |
58 | 49 | } |
59 | | - ; |
| 50 | + return timeBase64 + randomOrCounterBase64; |
| 51 | + } |
| 52 | + ; |
| 53 | + static generateRowID() { |
| 54 | + return this.generateUUID().replace(/_/g, "Z"); |
60 | 55 | } |
61 | | - |
62 | | - return libUUID; |
63 | | - |
64 | | -})(); |
| 56 | + ; |
| 57 | +} |
0 commit comments