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