summaryrefslogtreecommitdiff
path: root/shared/utils/src/uuid.ts
diff options
context:
space:
mode:
authorrxliuli <rxliuli@gmail.com>2025-11-04 05:03:50 +0800
committerrxliuli <rxliuli@gmail.com>2025-11-04 05:03:50 +0800
commitbce557cc2dc767628bed6aac87301a1be7c5431b (patch)
treeb51a051228d01fe3306cd7626d4a96768aadb944 /shared/utils/src/uuid.ts
init commit
Diffstat (limited to 'shared/utils/src/uuid.ts')
-rw-r--r--shared/utils/src/uuid.ts22
1 files changed, 22 insertions, 0 deletions
diff --git a/shared/utils/src/uuid.ts b/shared/utils/src/uuid.ts
new file mode 100644
index 0000000..0afa5ee
--- /dev/null
+++ b/shared/utils/src/uuid.ts
@@ -0,0 +1,22 @@
+/**
+ * Generate a variant 1 UUIDv4.
+ *
+ * @return the UUID
+ */
+export function generateUuid(): string {
+ return 'xxxxxxxx-xxxx-4xxx-Vxxx-xxxxxxxxxxxx'.replace(
+ /[xV]/g,
+ (placeholder) => {
+ let nibble = (Math.random() * 16) | 0;
+
+ if (placeholder === 'V') {
+ // Per RFC, the two MSB of byte 8 must be 0b10 (0x8).
+ // 0x3 (0b11) masks out the bottom two bits.
+ // See: https://tools.ietf.org/html/rfc4122.html#section-4.1.1
+ nibble = (nibble & 0x3) | 0x8;
+ }
+
+ return nibble.toString(16);
+ },
+ );
+}