Beginner
clideployweb
Generating & Validating UUIDs
UUIDs (universally unique identifier) can be used to uniquely identify some object or data.
A random UUID can be generated using the builtin Web Cryptography API. This type of UUID is also known as UUID v4.
!--frsh-copybutton:1-->
const myUUID = crypto.randomUUID();
console.log("Random UUID:", myUUID);
The standard library contains some more functions for working with UUIDs.
!--frsh-copybutton:2-->
import * as uuid from "https://deno.land/std@0.175.0/uuid/mod.ts";
You can validate that a given string is a valid UUID.
!--frsh-copybutton:3-->
console.log(uuid.validate("not a UUID")); // false
console.log(uuid.validate("6ec0bd7f-11c0-43da-975e-2a8ad9ebae0b")); // true
You can also generate a time-based (v1) UUID. By default this uses system time as the time source.
!--frsh-copybutton:4-->
console.log(uuid.v1.generate());
SHA-1 namespaced (v5) UUIDs can also be generated. For this you need to specify a namespace and data:
!--frsh-copybutton:5-->
const NAMESPACE_URL = "6ba7b810-9dad-11d1-80b4-00c04fd430c8";
const data = new TextEncoder().encode("deno.land");
console.log(await uuid.v5.generate(NAMESPACE_URL, data));
Run this example locally using the Deno CLI:
deno run https://byexample-wwv03xf36j0g.deno.dev/uuids.ts
Additional resources: