(uuid: string)
| 37 | * @returns A ULID string |
| 38 | */ |
| 39 | export function uuidToULID(uuid: string): ULID { |
| 40 | const isValid = UUID_REGEX.test(uuid); |
| 41 | if (!isValid) { |
| 42 | throw new ULIDError(ULIDErrorCode.UUIDInvalid, `Invalid UUID: ${uuid}`); |
| 43 | } |
| 44 | const bytes = uuid.replace(/-/g, "").match(/.{1,2}/g); |
| 45 | if (!bytes) { |
| 46 | throw new ULIDError(ULIDErrorCode.Unexpected, `Failed parsing UUID bytes: ${uuid}`); |
| 47 | } |
| 48 | const uint8Array = new Uint8Array(bytes.map(byte => parseInt(byte, 16))); |
| 49 | return crockfordEncode(uint8Array); |
| 50 | } |
no test coverage detected