From: Chris Duncan Date: Thu, 3 Jul 2025 15:39:17 +0000 (-0700) Subject: Add function to convert stringified bytes back to parsed Uint8Array. X-Git-Tag: v0.10.5~136^2~27 X-Git-Url: https://git.codecow.com/?a=commitdiff_plain;h=a72e824e4efc64ee7f9f49ba301c678ecfb96155;p=libnemo.git Add function to convert stringified bytes back to parsed Uint8Array. --- 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 }