2023-01-03 10:47:44 +00:00
|
|
|
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
|
2023-03-18 12:36:00 +00:00
|
|
|
// This module is browser compatible.
|
2022-06-14 09:20:56 +00:00
|
|
|
|
2022-05-04 10:34:37 +00:00
|
|
|
/**
|
2023-05-02 14:51:43 +00:00
|
|
|
* Generators and validators for UUIDs for versions v1, v3, v4 and v5.
|
2022-08-11 11:51:20 +00:00
|
|
|
*
|
|
|
|
* Consider using the web platform
|
|
|
|
* [`crypto.randomUUID`](https://developer.mozilla.org/en-US/docs/Web/API/Crypto/randomUUID)
|
|
|
|
* for v4 UUIDs instead.
|
|
|
|
*
|
|
|
|
* Based on https://github.com/kelektiv/node-uuid -> https://www.ietf.org/rfc/rfc4122.txt
|
|
|
|
*
|
2023-05-02 14:51:43 +00:00
|
|
|
* Support for RFC4122 version 1, 3, 4, and 5 UUIDs
|
2022-08-11 11:51:20 +00:00
|
|
|
*
|
|
|
|
* This module is browser compatible.
|
|
|
|
*
|
2022-05-04 10:34:37 +00:00
|
|
|
* @module
|
|
|
|
*/
|
2021-07-09 12:27:41 +00:00
|
|
|
|
2023-05-02 14:51:43 +00:00
|
|
|
export * from "./constants.ts";
|
|
|
|
|
2020-04-15 14:38:05 +00:00
|
|
|
import * as v1 from "./v1.ts";
|
2023-05-02 14:51:43 +00:00
|
|
|
import * as v3 from "./v3.ts";
|
2019-12-23 09:36:25 +00:00
|
|
|
import * as v4 from "./v4.ts";
|
2020-04-27 12:49:34 +00:00
|
|
|
import * as v5 from "./v5.ts";
|
2019-07-03 15:13:22 +00:00
|
|
|
|
|
|
|
export const NIL_UUID = "00000000-0000-0000-0000-000000000000";
|
|
|
|
|
2020-10-01 09:40:40 +00:00
|
|
|
/**
|
2021-07-09 12:27:41 +00:00
|
|
|
* Check if the passed UUID is the nil UUID.
|
|
|
|
*
|
|
|
|
* ```js
|
2022-10-10 14:05:56 +00:00
|
|
|
* import { isNil } from "https://deno.land/std@$STD_VERSION/uuid/mod.ts";
|
2021-07-09 12:27:41 +00:00
|
|
|
*
|
|
|
|
* isNil("00000000-0000-0000-0000-000000000000") // true
|
|
|
|
* isNil(crypto.randomUUID()) // false
|
|
|
|
* ```
|
2020-10-01 09:40:40 +00:00
|
|
|
*/
|
2021-07-09 12:27:41 +00:00
|
|
|
export function isNil(id: string): boolean {
|
|
|
|
return id === NIL_UUID;
|
2019-07-03 15:13:22 +00:00
|
|
|
}
|
|
|
|
|
2021-12-18 14:22:37 +00:00
|
|
|
/**
|
|
|
|
* Test a string to see if it is a valid UUID.
|
|
|
|
*
|
|
|
|
* ```js
|
2022-10-10 14:05:56 +00:00
|
|
|
* import { validate } from "https://deno.land/std@$STD_VERSION/uuid/mod.ts"
|
2021-12-18 14:22:37 +00:00
|
|
|
*
|
|
|
|
* validate("not a UUID") // false
|
|
|
|
* validate("6ec0bd7f-11c0-43da-975e-2a8ad9ebae0b") // true
|
|
|
|
* ```
|
|
|
|
*/
|
2021-12-23 04:54:23 +00:00
|
|
|
export function validate(uuid: string): boolean {
|
2021-12-18 14:22:37 +00:00
|
|
|
return /^(?:[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}|00000000-0000-0000-0000-000000000000)$/i
|
|
|
|
.test(
|
|
|
|
uuid,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-12-28 15:28:23 +00:00
|
|
|
/**
|
|
|
|
* Detect RFC version of a UUID.
|
|
|
|
*
|
|
|
|
* ```js
|
2022-10-10 14:05:56 +00:00
|
|
|
* import { version } from "https://deno.land/std@$STD_VERSION/uuid/mod.ts"
|
2021-12-28 15:28:23 +00:00
|
|
|
*
|
|
|
|
* version("d9428888-122b-11e1-b85c-61cd3cbb3210") // 1
|
|
|
|
* version("109156be-c4fb-41ea-b1b4-efe1671c5836") // 4
|
|
|
|
* ```
|
|
|
|
*/
|
|
|
|
export function version(uuid: string): number {
|
|
|
|
if (!validate(uuid)) {
|
2022-09-13 21:22:50 +00:00
|
|
|
throw new TypeError("Invalid UUID");
|
2021-12-28 15:28:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return parseInt(uuid[14], 16);
|
|
|
|
}
|
|
|
|
|
2023-05-02 14:51:43 +00:00
|
|
|
export { v1, v3, v4, v5 };
|