-
Notifications
You must be signed in to change notification settings - Fork 578
/
v4.ts
31 lines (28 loc) · 860 Bytes
/
v4.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
const UUID_RE =
/^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;
/**
* Validate that the passed UUID is an RFC4122 v4 UUID.
*
* ```ts
* import { validate } from "./v4.ts";
* import { generate as generateV1 } from "./v1.ts";
*
* validate(crypto.randomUUID()); // true
* validate(generateV1() as string); // false
* validate("this-is-not-a-uuid"); // false
* ```
*/
export function validate(id: string): boolean {
return UUID_RE.test(id);
}
/**
* @deprecated v4 UUID generation is deprecated and will be removed in a future
* std/uuid release. Use the web standard `globalThis.crypto.randomUUID()`
* function instead.
*
* Generate a RFC4122 v4 UUID (pseudo-random).
*/
export function generate(): string {
return crypto.randomUUID();
}