1
>
/*---------------------------------------------------------------------------------------------
uuid.ts
2
>
* Copyright (c) Microsoft Corporation. All rights reserved.
3
>
* Licensed under the MIT License. See License.txt in the project root for license information.
4
>
*--------------------------------------------------------------------------------------------*/
5
>
6
>
7
>
const _UUIDPattern = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
8
>
9
>
export function isUUID(value: string): boolean {
10
return _UUIDPattern.test(value);
11
}
13
>
export const generateUuid = (function (): () => string {
14
>
15
>
// use `randomUUID` if possible
16
>
if (typeof crypto.randomUUID === 'function') {
17
>
// see https://developer.mozilla.org/en-US/docs/Web/API/Window/crypto
18
>
// > Although crypto is available on all windows, the returned Crypto object only has one
19
>
// > usable feature in insecure contexts: the getRandomValues() method.
20
>
// > In general, you should use this API only in secure contexts.
21
>
22
>
return crypto.randomUUID.bind(crypto);
23
>
}
24
25
// prep-work