From a72e824e4efc64ee7f9f49ba301c678ecfb96155 Mon Sep 17 00:00:00 2001 From: Chris Duncan Date: Thu, 3 Jul 2025 08:39:17 -0700 Subject: [PATCH] Add function to convert stringified bytes back to parsed Uint8Array. --- src/lib/convert.ts | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/lib/convert.ts b/src/lib/convert.ts index 2b8016f..8511e8e 100644 --- a/src/lib/convert.ts +++ b/src/lib/convert.ts @@ -334,4 +334,20 @@ export const utf8 = { } } +export const obj = { + /** + * Convert a numerically-indexed object of 8-bit values to a Uint8Array of bytes. + * + * @param {object} obj - Object to convert + * @returns {Uint8Array} Byte array representation of the input object + */ + toBytes (obj: { [key: number]: number }): Uint8Array { + const values = Object.keys(obj) + .map(key => +key) + .sort((a, b) => a - b) + .map(i => obj[i]) + return new Uint8Array(values) + } +} + export default { base32, bin, bytes, dec, hex, utf8 } -- 2.47.3