seperate uuid helpers

This commit is contained in:
2023-02-22 20:23:23 +10:00
parent 4adb0c953b
commit a2ab077325
2 changed files with 24 additions and 12 deletions

View File

@@ -38,18 +38,6 @@ export function isString(target: unknown): boolean {
return typeof target === "string" && target !== null; 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. * Convert bytes to a human readable string.
* *

View File

@@ -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);
});
};