From a2ab077325116cd986de8dd5b4bfca769779f354 Mon Sep 17 00:00:00 2001 From: James Collins Date: Wed, 22 Feb 2023 20:23:23 +1000 Subject: [PATCH] seperate uuid helpers --- resources/js/helpers/types.ts | 12 ------------ resources/js/helpers/uuid.ts | 24 ++++++++++++++++++++++++ 2 files changed, 24 insertions(+), 12 deletions(-) create mode 100644 resources/js/helpers/uuid.ts diff --git a/resources/js/helpers/types.ts b/resources/js/helpers/types.ts index e00ac7e..562129f 100644 --- a/resources/js/helpers/types.ts +++ b/resources/js/helpers/types.ts @@ -38,18 +38,6 @@ export function isString(target: unknown): boolean { return typeof target === "string" && target !== null; } -/** - * Test if target is a UUID - * - * @param {string} uuid The variable to test - * @returns {boolean} If the varible is a UUID - */ -export const isUUID = (uuid: string): boolean => { - return /^[0-9a-f]{8}-[0-9a-f]{4}-[0-5][0-9a-f]{3}-[089ab][0-9a-f]{3}-[0-9a-f]{12}$/i.test( - uuid - ); -}; - /** * Convert bytes to a human readable string. * diff --git a/resources/js/helpers/uuid.ts b/resources/js/helpers/uuid.ts new file mode 100644 index 0000000..6bc794a --- /dev/null +++ b/resources/js/helpers/uuid.ts @@ -0,0 +1,24 @@ +/** + * Test if target is a UUID + * + * @param {string} uuid The variable to test + * @returns {boolean} If the varible is a UUID + */ +export const isUUID = (uuid: string): boolean => { + return /^[0-9a-f]{8}-[0-9a-f]{4}-[0-5][0-9a-f]{3}-[089ab][0-9a-f]{3}-[0-9a-f]{12}$/i.test( + uuid + ); +}; + +/** + * Generates a random UUID. + * + * @returns {string} A random UUID. + */ +export const randomUUID = (): string => { + return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, (c) => { + const r = (Math.random() * 16) | 0; + const v = c === "x" ? r : (r & 0x3) | 0x8; + return v.toString(16); + }); +};