From a5b969478469711277122566049dc1edce4f32dd Mon Sep 17 00:00:00 2001 From: David Luis <95457148+babiabeo@users.noreply.github.com> Date: Tue, 2 May 2023 21:51:43 +0700 Subject: [PATCH] feat(uuid): add pre-defined namespace UUIDs (#3352) --- uuid/constants.ts | 47 ++++++++++++++++++++++++++++++++++++++++++ uuid/constants_test.ts | 16 ++++++++++++++ uuid/mod.ts | 9 +++++--- 3 files changed, 69 insertions(+), 3 deletions(-) create mode 100644 uuid/constants.ts create mode 100644 uuid/constants_test.ts diff --git a/uuid/constants.ts b/uuid/constants.ts new file mode 100644 index 000000000..7460978a0 --- /dev/null +++ b/uuid/constants.ts @@ -0,0 +1,47 @@ +// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. +// This module is browser compatible. + +/** + * Name string is a fully-qualified domain name. + * + * @example + * ```ts + * import { NAMESPACE_DNS } from "https://deno.land/std@$STD_VERSION/uuid/constants.ts"; + * + * console.log(NAMESPACE_DNS); // => 6ba7b810-9dad-11d1-80b4-00c04fd430c8 + * ``` + */ +export const NAMESPACE_DNS = "6ba7b810-9dad-11d1-80b4-00c04fd430c8"; +/** + * Name string is a URL. + * + * @example + * ```ts + * import { NAMESPACE_URL } from "https://deno.land/std@$STD_VERSION/uuid/constants.ts"; + * + * console.log(NAMESPACE_URL); // => 6ba7b811-9dad-11d1-80b4-00c04fd430c8 + * ``` + */ +export const NAMESPACE_URL = "6ba7b811-9dad-11d1-80b4-00c04fd430c8"; +/** + * Name string is an ISO OID. + * + * @example + * ```ts + * import { NAMESPACE_OID } from "https://deno.land/std@$STD_VERSION/uuid/constants.ts"; + * + * console.log(NAMESPACE_OID); // => 6ba7b812-9dad-11d1-80b4-00c04fd430c8 + * ``` + */ +export const NAMESPACE_OID = "6ba7b812-9dad-11d1-80b4-00c04fd430c8"; +/** + * Name string is an X.500 DN (in DER or a text output format). + * + * @example + * ```ts + * import { NAMESPACE_X500 } from "https://deno.land/std@$STD_VERSION/uuid/constants.ts"; + * + * console.log(NAMESPACE_X500); // => 6ba7b814-9dad-11d1-80b4-00c04fd430c8 + * ``` + */ +export const NAMESPACE_X500 = "6ba7b814-9dad-11d1-80b4-00c04fd430c8"; diff --git a/uuid/constants_test.ts b/uuid/constants_test.ts new file mode 100644 index 000000000..da911f8ba --- /dev/null +++ b/uuid/constants_test.ts @@ -0,0 +1,16 @@ +// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. +import { assertEquals } from "../testing/asserts.ts"; +import { + NAMESPACE_DNS, + NAMESPACE_OID, + NAMESPACE_URL, + NAMESPACE_X500, +} from "./constants.ts"; +import { validate } from "./mod.ts"; + +Deno.test("[UUID] validate_namespaces", () => { + assertEquals(validate(NAMESPACE_DNS), true); + assertEquals(validate(NAMESPACE_URL), true); + assertEquals(validate(NAMESPACE_OID), true); + assertEquals(validate(NAMESPACE_X500), true); +}); diff --git a/uuid/mod.ts b/uuid/mod.ts index b25ae752a..a329b947a 100644 --- a/uuid/mod.ts +++ b/uuid/mod.ts @@ -2,7 +2,7 @@ // This module is browser compatible. /** - * Generators and validators for UUIDs for versions v1, v4 and v5. + * Generators and validators for UUIDs for versions v1, v3, v4 and v5. * * Consider using the web platform * [`crypto.randomUUID`](https://developer.mozilla.org/en-US/docs/Web/API/Crypto/randomUUID) @@ -10,14 +10,17 @@ * * Based on https://github.com/kelektiv/node-uuid -> https://www.ietf.org/rfc/rfc4122.txt * - * Support for RFC4122 version 1, 4, and 5 UUIDs + * Support for RFC4122 version 1, 3, 4, and 5 UUIDs * * This module is browser compatible. * * @module */ +export * from "./constants.ts"; + import * as v1 from "./v1.ts"; +import * as v3 from "./v3.ts"; import * as v4 from "./v4.ts"; import * as v5 from "./v5.ts"; @@ -72,4 +75,4 @@ export function version(uuid: string): number { return parseInt(uuid[14], 16); } -export { v1, v4, v5 }; +export { v1, v3, v4, v5 };