From f5ce0563ae6ae5a5afc34b0a46031349676ec299 Mon Sep 17 00:00:00 2001 From: Ardis Lu Date: Tue, 21 Oct 2025 23:45:32 -0700 Subject: [PATCH] Improve function documentation --- calculate.js | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/calculate.js b/calculate.js index f75ceca..ec92175 100644 --- a/calculate.js +++ b/calculate.js @@ -1,16 +1,35 @@ import { RLP, keccak256 } from './vendor.min.js'; -// Uint8Array to hex string +/** + * Converts a `Uint8Array` into a hexadecimal string. + * @param {Uint8Array} array The byte array to convert. + * @returns {string} A hexadecimal string representation of the byte array, + * always prefixed with `'0x'`, with each byte padded to two digits. + */ function hex(array) { return `0x${[...array].map(v => v.toString(16).padStart(2, '0')).join('')}`; } -// Hex string to Uint8Array +/** + * Converts a hexadecimal string into a `Uint8Array`. + * @param {string} hex A hexadecimal string representing a byte array. + * May optionally include a `'0x'` prefix. Each byte must be represented + * by exactly two hex digits. + * @returns {Uint8Array} A new byte array decoded from the hexadecimal string. + * @throws {Error} If the string has an odd length or contains invalid hexadecimal + * characters. + */ function arr(hex) { return Uint8Array.from(hex.replace('0x', '').match(/.{2}/g), v => parseInt(v, 16)); } -// Number to Uint8Array (big-endian) +/** + * Returns a 4-byte big-endian representation of a given number. + * @param {number|string} n A number or numeric string to encode. + * Values outside the 32-bit unsigned integer range will be truncated. + * @returns {Uint8Array} A 4-byte `Uint8Array` containing the big-endian + * `uint32` representation of `n`. + */ function arrBE(n) { const v = new DataView(new ArrayBuffer(4)); v.setUint32(0, Number(n), false); // Forces big-endian regardless of platform -- 2.47.3