{"version":3,"file":"v4.mjs","names":["uuidv4: UuidV4"],"sources":["../../src/uuid/v4.ts"],"sourcesContent":["import { rng } from '../common/random'\nimport { BufferError, InvalidInputError } from '../errors'\nimport { formatUuid, parseUuid } from './common/uuid'\n\nconst randomUUID = /*@__PURE__*/ globalThis.crypto.randomUUID.bind(globalThis.crypto)\n\nexport type UuidV4Options = {\n  /**\n   * 16 bytes of random data to use for UUID generation.\n   * Note: Bytes at index 6 and 8 will be modified in-place to set version/variant bits.\n   */\n  random?: Uint8Array\n}\n\nexport type UuidV4 = {\n  (): string\n  <TBuf extends Uint8Array = Uint8Array>(options: UuidV4Options | undefined, buf: TBuf, offset?: number): TBuf\n  (options?: UuidV4Options, buf?: undefined, offset?: number): string\n  toBytes(id: string): Uint8Array\n  fromBytes(bytes: Uint8Array): string\n  isValid(id: unknown): id is string\n  /** The nil UUID (all zeros) */\n  NIL: string\n  /** The max UUID (all ones) */\n  MAX: string\n}\n\nconst UUID_V4_REGEX = /^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i\n\nfunction v4Bytes(rnds: Uint8Array, buf?: Uint8Array, offset = 0): Uint8Array {\n  if (rnds.length < 16) {\n    throw new InvalidInputError('UUID_RANDOM_BYTES_TOO_SHORT', 'Random bytes length must be >= 16')\n  }\n\n  // Set RFC 4122 version (4) and variant (10xx) bits.\n  // Note: This modifies the input array in-place.\n  rnds[6] = (rnds[6] & 0x0f) | 0x40\n  rnds[8] = (rnds[8] & 0x3f) | 0x80\n\n  if (!buf) {\n    // No output buffer provided - return the modified random bytes directly\n    return rnds\n  }\n\n  if (offset < 0 || offset + 16 > buf.length) {\n    throw new BufferError(\n      'UUID_BUFFER_OUT_OF_BOUNDS',\n      `UUID byte range ${offset}:${offset + 15} is out of buffer bounds`,\n    )\n  }\n\n  // Copy 16 UUID bytes into the provided buffer slice.\n  for (let i = 0; i < 16; i += 1) {\n    buf[offset + i] = rnds[i]\n  }\n\n  return buf\n}\n\n/*\n * Overload: no buffer => return a UUID string.\n */\nfunction v4(options?: UuidV4Options, buf?: undefined, offset?: number): string\n/*\n * Overload: caller provides a buffer slice to fill with UUID bytes.\n */\nfunction v4<TBuf extends Uint8Array = Uint8Array>(options: UuidV4Options | undefined, buf: TBuf, offset?: number): TBuf\nfunction v4<TBuf extends Uint8Array = Uint8Array>(options?: UuidV4Options, buf?: TBuf, offset?: number): string | TBuf {\n  if (!buf && !options) {\n    return randomUUID()\n  }\n\n  return _v4(options, buf, offset)\n}\n\nfunction _v4<TBuf extends Uint8Array = Uint8Array>(\n  options?: UuidV4Options,\n  buf?: TBuf,\n  offset?: number,\n): string | TBuf {\n  const bytes = v4Bytes(options?.random ?? rng(), buf, offset)\n  return buf ?? formatUuid(bytes)\n}\n\nfunction isValid(id: unknown): id is string {\n  return typeof id === 'string' && UUID_V4_REGEX.test(id)\n}\n\n/**\n * Generate a UUID v4 string or write the bytes into a buffer.\n *\n * UUID v4 is a purely random UUID with 122 bits of entropy. It's the most\n * widely compatible UUID format, supported by virtually all databases and systems.\n * Use when you need maximum compatibility and don't require time-ordering.\n *\n * @example\n * ```ts\n * import { uuidv4 } from 'uniku/uuid/v4'\n *\n * const id = uuidv4()\n * // => \"550e8400-e29b-41d4-a716-446655440000\"\n *\n * // Validate\n * uuidv4.isValid(id) // true\n *\n * // Convert to/from bytes (16 bytes)\n * const bytes = uuidv4.toBytes(id)\n * const restored = uuidv4.fromBytes(bytes)\n * ```\n */\nexport const uuidv4: UuidV4 = Object.assign(v4, {\n  toBytes: parseUuid,\n  fromBytes: formatUuid,\n  isValid,\n  NIL: '00000000-0000-0000-0000-000000000000',\n  MAX: 'ffffffff-ffff-ffff-ffff-ffffffffffff',\n})\n\nexport { BufferError, InvalidInputError, ParseError, UniqueIdError } from '../errors'\n"],"mappings":"iMAIA,MAAM,EAA2B,WAAW,OAAO,WAAW,KAAK,WAAW,OAAO,CAuB/E,EAAgB,yEAEtB,SAAS,EAAQ,EAAkB,EAAkB,EAAS,EAAe,CAC3E,GAAI,EAAK,OAAS,GAChB,MAAM,IAAI,EAAkB,8BAA+B,oCAAoC,CAQjG,GAHA,EAAK,GAAM,EAAK,GAAK,GAAQ,GAC7B,EAAK,GAAM,EAAK,GAAK,GAAQ,IAEzB,CAAC,EAEH,OAAO,EAGT,GAAI,EAAS,GAAK,EAAS,GAAK,EAAI,OAClC,MAAM,IAAI,EACR,4BACA,mBAAmB,EAAO,GAAG,EAAS,GAAG,0BAC1C,CAIH,IAAK,IAAI,EAAI,EAAG,EAAI,GAAI,GAAK,EAC3B,EAAI,EAAS,GAAK,EAAK,GAGzB,OAAO,EAWT,SAAS,EAAyC,EAAyB,EAAY,EAAgC,CAKrH,MAJI,CAAC,GAAO,CAAC,EACJ,GAAY,CAGd,EAAI,EAAS,EAAK,EAAO,CAGlC,SAAS,EACP,EACA,EACA,EACe,CACf,IAAM,EAAQ,EAAQ,GAAS,QAAU,GAAK,CAAE,EAAK,EAAO,CAC5D,OAAO,GAAO,EAAW,EAAM,CAGjC,SAAS,EAAQ,EAA2B,CAC1C,OAAO,OAAO,GAAO,UAAY,EAAc,KAAK,EAAG,CAyBzD,MAAaA,EAAiB,OAAO,OAAO,EAAI,CAC9C,QAAS,EACT,UAAW,EACX,UACA,IAAK,uCACL,IAAK,uCACN,CAAC"}