diff --git a/package.json b/package.json index abc0da9..a5b28bd 100644 --- a/package.json +++ b/package.json @@ -39,7 +39,8 @@ "author": "Hein (Warkanum) Puth", "license": "MIT", "dependencies": { - "semver": "^7.6.3" + "semver": "^7.6.3", + "uuid": "^11.0.3" }, "devDependencies": { "@changesets/cli": "^2.27.10", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 19366f8..360e84b 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -11,6 +11,9 @@ importers: semver: specifier: ^7.6.3 version: 7.6.3 + uuid: + specifier: ^11.0.3 + version: 11.0.3 devDependencies: '@changesets/cli': specifier: ^2.27.10 @@ -1637,6 +1640,10 @@ packages: uri-js@4.4.1: resolution: {integrity: sha512-7rKUyy33Q1yc98pQ1DAmLtwX109F7TIfWlW1Ydo8Wl1ii1SeHieeh0HHfPeL2fMXK6z0s8ecKs9frCuLJvndBg==} + uuid@11.0.3: + resolution: {integrity: sha512-d0z310fCWv5dJwnX1Y/MncBAqGMKEzlBb1AOf7z9K8ALnd0utBX/msg/fA0+sbyN1ihbMsLhrBlnl1ak7Wa0rg==} + hasBin: true + vite-node@2.1.8: resolution: {integrity: sha512-uPAwSr57kYjAUux+8E2j0q0Fxpn8M9VoyfGiRI8Kfktz9NcYMCenwY5RnZxnF1WTu3TGiYipirIzacLL3VVGFg==} engines: {node: ^18.0.0 || >=20.0.0} @@ -3368,6 +3375,8 @@ snapshots: dependencies: punycode: 2.3.1 + uuid@11.0.3: {} + vite-node@2.1.8: dependencies: cac: 6.7.14 diff --git a/src/index.ts b/src/index.ts index 2e4cd26..b38db81 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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' \ No newline at end of file +export * from "./base64"; +export * from "./strings"; +export * from "./mime"; +export * from "./promise"; +export * from "./i18n"; +export * from "./dataqueue"; +//export * from './logger' diff --git a/src/strings/index.ts b/src/strings/index.ts index 4731f52..5641767 100644 --- a/src/strings/index.ts +++ b/src/strings/index.ts @@ -1,6 +1,7 @@ -export * from './trim' -export * from './replace' -export * from './caseConversion' -export * from './locale' -export * from './fileSize' -export * from './legacy' \ No newline at end of file +export * from "./trim"; +export * from "./replace"; +export * from "./caseConversion"; +export * from "./locale"; +export * from "./fileSize"; +export * from "./legacy"; +export * from "./uuid"; diff --git a/src/strings/uuid.ts b/src/strings/uuid.ts new file mode 100644 index 0000000..cf5c672 --- /dev/null +++ b/src/strings/uuid.ts @@ -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(); +}