Added newUUID, getUUID

This commit is contained in:
2024-12-17 23:23:01 +02:00
parent f0be5d3028
commit 04fb164c8d
5 changed files with 53 additions and 15 deletions

View File

@@ -1,8 +1,7 @@
export * from './base64'
export * from './strings'
export * from './mime'
export * from './promise'
export * from './i18n'
export * from './dataqueue'
//export * from './logger'
export * from "./base64";
export * from "./strings";
export * from "./mime";
export * from "./promise";
export * from "./i18n";
export * from "./dataqueue";
//export * from './logger'

View File

@@ -1,6 +1,7 @@
export * from './trim'
export * from './replace'
export * from './caseConversion'
export * from './locale'
export * from './fileSize'
export * from './legacy'
export * from "./trim";
export * from "./replace";
export * from "./caseConversion";
export * from "./locale";
export * from "./fileSize";
export * from "./legacy";
export * from "./uuid";

28
src/strings/uuid.ts Normal file
View File

@@ -0,0 +1,28 @@
import { v4 as uuidv4 } from "uuid";
/**
* Generates a UUID (Universally Unique Identifier) using the crypto.randomUUID() method if available,
* or creates a fallback UUID using the current timestamp and random numbers.
*
* @returns {string} Returns a UUID string.
*/
export function getUUID(): string {
let uuid = "";
try {
uuid = crypto.randomUUID();
// eslint-disable-next-line @typescript-eslint/no-unused-vars
} catch (e) {
const d = new Date();
const rnd = Math.random() * 100 * (Math.random() * 10);
uuid = `${d.getTime()}${rnd}`;
}
return uuid;
}
/**
* Generates a random UUID using the uuidv4 library.
*
* @returns {string} Returns a UUID string.
*/
export function newUUID(): string {
return uuidv4();
}