Skip to main content

apps/gateway/src/utils/uuid.ts

Metadata

Indexed Symbols

  • isUuid (line 5, function) - Implements is uuid for module behavior.
  • ensureUuid (line 9, function) - Implements ensure uuid for module behavior.

Markdown Headings (if applicable)

No markdown headings detected.

Source Preview

import { randomUUID } from "node:crypto";

const uuidRegex = /^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;

export function isUuid(value: string): boolean {
return uuidRegex.test(value);
}

export function ensureUuid(value: unknown, fallback?: string): string {
if (typeof value === "string" && isUuid(value)) {
return value;
}

if (fallback && isUuid(fallback)) {
return fallback;
}

return randomUUID();
}