mirror of
https://github.com/denoland/std.git
synced 2024-11-21 20:50:22 +00:00
refactor(uuid): factor out common symbols into common
module (#4749)
This commit is contained in:
parent
6c77972053
commit
89d95a5633
56
uuid/common.ts
Normal file
56
uuid/common.ts
Normal file
@ -0,0 +1,56 @@
|
||||
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
||||
// This module is browser compatible.
|
||||
|
||||
import { NIL_UUID } from "./constants.ts";
|
||||
|
||||
/**
|
||||
* Check if the passed UUID is the nil UUID.
|
||||
*
|
||||
* @example
|
||||
* ```ts
|
||||
* import { isNil } from "@std/uuid";
|
||||
*
|
||||
* isNil("00000000-0000-0000-0000-000000000000"); // true
|
||||
* isNil(crypto.randomUUID()); // false
|
||||
* ```
|
||||
*/
|
||||
export function isNil(id: string): boolean {
|
||||
return id === NIL_UUID;
|
||||
}
|
||||
|
||||
/**
|
||||
* Test a string to see if it is a valid UUID.
|
||||
*
|
||||
* @example
|
||||
* ```ts
|
||||
* import { validate } from "@std/uuid";
|
||||
*
|
||||
* validate("not a UUID"); // false
|
||||
* validate("6ec0bd7f-11c0-43da-975e-2a8ad9ebae0b"); // true
|
||||
* ```
|
||||
*/
|
||||
export function validate(uuid: string): boolean {
|
||||
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,
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Detect RFC version of a UUID.
|
||||
*
|
||||
* @example
|
||||
* ```ts
|
||||
* import { version } from "@std/uuid";
|
||||
*
|
||||
* version("d9428888-122b-11e1-b85c-61cd3cbb3210"); // 1
|
||||
* version("109156be-c4fb-41ea-b1b4-efe1671c5836"); // 4
|
||||
* ```
|
||||
*/
|
||||
export function version(uuid: string): number {
|
||||
if (!validate(uuid)) {
|
||||
throw new TypeError("Invalid UUID");
|
||||
}
|
||||
|
||||
return parseInt(uuid[14]!, 16);
|
||||
}
|
@ -45,3 +45,8 @@ export const NAMESPACE_OID = "6ba7b812-9dad-11d1-80b4-00c04fd430c8";
|
||||
* ```
|
||||
*/
|
||||
export const NAMESPACE_X500 = "6ba7b814-9dad-11d1-80b4-00c04fd430c8";
|
||||
/**
|
||||
* The nil UUID is special form of UUID that is specified to have all 128 bits
|
||||
* set to zero.
|
||||
*/
|
||||
export const NIL_UUID = "00000000-0000-0000-0000-000000000000";
|
||||
|
@ -3,6 +3,7 @@
|
||||
"version": "0.224.0",
|
||||
"exports": {
|
||||
".": "./mod.ts",
|
||||
"./common": "./common.ts",
|
||||
"./constants": "./constants.ts",
|
||||
"./v1": "./v1.ts",
|
||||
"./v3": "./v3.ts",
|
||||
|
59
uuid/mod.ts
59
uuid/mod.ts
@ -15,66 +15,9 @@
|
||||
* @module
|
||||
*/
|
||||
|
||||
export * from "./common.ts";
|
||||
export * from "./constants.ts";
|
||||
export * as v1 from "./v1.ts";
|
||||
export * as v3 from "./v3.ts";
|
||||
export * as v4 from "./v4.ts";
|
||||
export * as v5 from "./v5.ts";
|
||||
|
||||
/**
|
||||
* The nil UUID is special form of UUID that is specified to have all 128 bits
|
||||
* set to zero.
|
||||
*/
|
||||
export const NIL_UUID = "00000000-0000-0000-0000-000000000000";
|
||||
|
||||
/**
|
||||
* Check if the passed UUID is the nil UUID.
|
||||
*
|
||||
* @example
|
||||
* ```ts
|
||||
* import { isNil } from "@std/uuid";
|
||||
*
|
||||
* isNil("00000000-0000-0000-0000-000000000000"); // true
|
||||
* isNil(crypto.randomUUID()); // false
|
||||
* ```
|
||||
*/
|
||||
export function isNil(id: string): boolean {
|
||||
return id === NIL_UUID;
|
||||
}
|
||||
|
||||
/**
|
||||
* Test a string to see if it is a valid UUID.
|
||||
*
|
||||
* @example
|
||||
* ```ts
|
||||
* import { validate } from "@std/uuid";
|
||||
*
|
||||
* validate("not a UUID"); // false
|
||||
* validate("6ec0bd7f-11c0-43da-975e-2a8ad9ebae0b"); // true
|
||||
* ```
|
||||
*/
|
||||
export function validate(uuid: string): boolean {
|
||||
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,
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Detect RFC version of a UUID.
|
||||
*
|
||||
* @example
|
||||
* ```ts
|
||||
* import { version } from "@std/uuid";
|
||||
*
|
||||
* version("d9428888-122b-11e1-b85c-61cd3cbb3210"); // 1
|
||||
* version("109156be-c4fb-41ea-b1b4-efe1671c5836"); // 4
|
||||
* ```
|
||||
*/
|
||||
export function version(uuid: string): number {
|
||||
if (!validate(uuid)) {
|
||||
throw new TypeError("Invalid UUID");
|
||||
}
|
||||
|
||||
return parseInt(uuid[14]!, 16);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user