(ulid: ULID)
| 10 | * @returns A UUID string |
| 11 | */ |
| 12 | export function ulidToUUID(ulid: ULID): UUID { |
| 13 | const isValid = ULID_REGEX.test(ulid); |
| 14 | if (!isValid) { |
| 15 | throw new ULIDError(ULIDErrorCode.ULIDInvalid, `Invalid ULID: ${ulid}`); |
| 16 | } |
| 17 | const uint8Array = crockfordDecode(ulid); |
| 18 | let uuid = Array.from(uint8Array) |
| 19 | .map(byte => byte.toString(16).padStart(2, "0")) |
| 20 | .join(""); |
| 21 | uuid = |
| 22 | uuid.substring(0, 8) + |
| 23 | "-" + |
| 24 | uuid.substring(8, 12) + |
| 25 | "-" + |
| 26 | uuid.substring(12, 16) + |
| 27 | "-" + |
| 28 | uuid.substring(16, 20) + |
| 29 | "-" + |
| 30 | uuid.substring(20); |
| 31 | return uuid.toUpperCase() as UUID; |
| 32 | } |
| 33 | |
| 34 | /** |
| 35 | * Convert a UUID to a ULID |
no test coverage detected