// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
// This module is browser compatible.

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

/**
 * Determines whether a string is a valid
 * {@link https://www.rfc-editor.org/rfc/rfc9562.html#section-5.4 | UUIDv4}.
 *
 * @param id UUID value.
 *
 * @returns `true` if the UUID is valid UUIDv4, otherwise `false`.
 *
 * @example Usage
 * ```ts
 * import { validate } from "@std/uuid/v4";
 * import { assert, assertFalse } from "@std/assert";
 *
 * assert(validate(crypto.randomUUID()));
 * assertFalse(validate("this-is-not-a-uuid"));
 * ```
 */
export function validate(
  id: string,
): id is ReturnType<typeof crypto.randomUUID> {
  return UUID_RE.test(id);
}

// denoCacheMetadata={"headers":{"set-cookie":"__cf_bm=5v4LgVMulcFOJ_twtwJpMQc6.ixku3obRRVhXLrDuKY-1785241875.2000713-1.0.1.1-jTETDbGiiUPE2BZfwIqxH3kZI2dXOl.DZ3RblAyKNsCNbu6ev1lloRCXWtoPnQwaZtRwTFXZeyLy_hEI99IQoFfpO3CIfeElB_hSv8wXtUMMISMwqEVrwLNthLiAPYh7; HttpOnly; SameSite=None; Secure; Path=/; Domain=jsr.io; Expires=Tue, 28 Jul 2026 13:01:15 GMT","nel":"{\"report_to\":\"cf-nel\",\"success_fraction\":0.0,\"max_age\":604800}","cross-origin-resource-policy":"cross-origin","x-jsr-backend":"modules","server":"cloudflare","alt-svc":"h3=\":443\"; ma=86400","report-to":"{\"group\":\"cf-nel\",\"max_age\":604800,\"endpoints\":[{\"url\":\"https://a.nel.cloudflare.com/report/v4?s=fx7ixuxDLDxlP%2B%2BnFrE3%2FNHlS6jVYTSWnbhCi%2FHbSPWwGILXiGJl9rZy52eqSvocIp4j7za%2FxGQQeJnXk2OiLZ3zPHJB1KC8NJwCmcISPa9YRj%2FE%2FUteUKk%3D\"}]}","access-control-allow-origin":"*","cf-ray":"a223e657fb107db6-LAX","access-control-expose-headers":"*","content-type":"text/typescript","x-robots-tag":"noindex","x-content-type-options":"nosniff","content-length":"777","vary":"Origin","etag":"\"1337bc00d920119f0ae15378c8659779\"","content-security-policy":"default-src 'none'; script-src 'none'; style-src 'none'; img-src 'none'; font-src 'none'; connect-src 'none'; frame-src 'none'; object-src 'none'; frame-ancestors 'none'; sandbox; form-action 'none';","cache-control":"public, max-age=31536000, immutable","date":"Tue, 28 Jul 2026 12:31:15 GMT"},"url":"https://jsr.io/@std/uuid/0.224.3/v4.ts","time":1785241875}