/ shared / utils / src / uuid.ts
uuid.ts
 1  /**
 2   * Generate a variant 1 UUIDv4.
 3   *
 4   * @return the UUID
 5   */
 6  export function generateUuid(): string {
 7      return 'xxxxxxxx-xxxx-4xxx-Vxxx-xxxxxxxxxxxx'.replace(
 8          /[xV]/g,
 9          (placeholder) => {
10              let nibble = (Math.random() * 16) | 0;
11  
12              if (placeholder === 'V') {
13                  // Per RFC, the two MSB of byte 8 must be 0b10 (0x8).
14                  // 0x3 (0b11) masks out the bottom two bits.
15                  // See: https://tools.ietf.org/html/rfc4122.html#section-4.1.1
16                  nibble = (nibble & 0x3) | 0x8;
17              }
18  
19              return nibble.toString(16);
20          },
21      );
22  }