From d76f42006031a7aba091c3fc29defae1799b4031 Mon Sep 17 00:00:00 2001 From: Chris Duncan Date: Tue, 30 Jun 2026 15:03:46 -0700 Subject: [PATCH] Simplify hex-to-byte converter. --- src/lib/convert/hex.ts | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/src/lib/convert/hex.ts b/src/lib/convert/hex.ts index 92844ad..fb4360e 100644 --- a/src/lib/convert/hex.ts +++ b/src/lib/convert/hex.ts @@ -30,14 +30,10 @@ export const hex = Object.freeze({ throw new TypeError('Invalid padding when converting hex to bytes', { cause: padding }) } if (hex.length & 1) hex = `0${hex}` - const hexArray = hex.match(/.{2}/g) - if (hexArray == null) { - throw new RangeError('Error converting hex string to bytes', { cause: hexArray }) - } - const diff = padding - hexArray.length + const byteLength = hex.length >> 1 + const diff = padding - byteLength const offset = diff & ~(diff >> 31) - const length = hexArray.length + offset - const bytes = new Uint8Array(length) + const bytes = new Uint8Array(byteLength + offset) bytes.set(hex.match(/.{2}/g)?.map(b => parseInt(b, 16)) ?? [], offset) return bytes }, -- 2.52.0