]> git.codecow.com Git - libnemo.git/commitdiff
Add function to convert stringified bytes back to parsed Uint8Array.
authorChris Duncan <chris@zoso.dev>
Thu, 3 Jul 2025 15:39:17 +0000 (08:39 -0700)
committerChris Duncan <chris@zoso.dev>
Thu, 3 Jul 2025 15:39:17 +0000 (08:39 -0700)
src/lib/convert.ts

index 2b8016ff7757473e9cacdab8a791a9796c07890b..8511e8eeb98cff87699e69193330251802c2cbcf 100644 (file)
@@ -334,4 +334,20 @@ export const utf8 = {
        }\r
 }\r
 \r
+export const obj = {\r
+       /**\r
+       * Convert a numerically-indexed object of 8-bit values to a Uint8Array of bytes.\r
+       *\r
+       * @param {object} obj - Object to convert\r
+       * @returns {Uint8Array} Byte array representation of the input object\r
+       */\r
+       toBytes (obj: { [key: number]: number }): Uint8Array {\r
+               const values = Object.keys(obj)\r
+                       .map(key => +key)\r
+                       .sort((a, b) => a - b)\r
+                       .map(i => obj[i])\r
+               return new Uint8Array(values)\r
+       }\r
+}\r
+\r
 export default { base32, bin, bytes, dec, hex, utf8 }\r