]> git.codecow.com Git - libnemo.git/commitdiff
Fix decimal padding args.
authorChris Duncan <chris@codecow.com>
Tue, 30 Jun 2026 22:10:47 +0000 (15:10 -0700)
committerChris Duncan <chris@codecow.com>
Tue, 30 Jun 2026 22:10:47 +0000 (15:10 -0700)
src/lib/convert/dec.ts

index 575a929eb69e1848dc7ef1ec59425eca9a124848..5f8f4fe59f9ce7bc5704edd4225c6a109089e24a 100644 (file)
@@ -3,17 +3,18 @@
 
 export const dec = Object.freeze({
        /**
-        * Convert a decimal integer to a Uint8Array of bytes. Fractional part is truncated.
+        * Convert a decimal integer to a Uint8Array of bytes. Fractional part is
+        * truncated.
         *
         * @param {bigint|number|string} decimal - Integer to convert
-        * @param {number} [padding=0] - Minimum length of the resulting array padded as necessary with starting 0x00 bytes
+        * @param {number} [padding=1] - Minimum length of the resulting array padded as necessary with starting 0x00 bytes
         * @returns {Uint8Array} Byte array representation of the input decimal
         */
-       toBytes (decimal: bigint | number | string, padding: number = 0): Uint8Array<ArrayBuffer> {
+       toBytes (decimal: bigint | number | string, padding: number = 1): Uint8Array<ArrayBuffer> {
                if (decimal == null) {
                        throw new TypeError(`Failed to convert '${decimal}' from decimal to bytes`)
                }
-               if (typeof padding !== 'number') {
+               if (typeof padding !== 'number' || padding < 1 || padding > 0xffffffff) {
                        throw new TypeError('Invalid padding')
                }
                let integer = BigInt(decimal)
@@ -32,14 +33,14 @@ export const dec = Object.freeze({
         * Convert a decimal integer to a hexadecimal string.
         *
         * @param {(bigint|number|string)} decimal - Integer to convert
-        * @param {number} [padding=0] - Minimum length of the resulting string padded as necessary with starting zeroes
+        * @param {number} [padding=1] - Minimum length of the resulting string padded as necessary with starting zeroes
         * @returns {string} Hexadecimal string representation of the input decimal
         */
-       toHex (decimal: bigint | number | string, padding: number = 0): string {
+       toHex (decimal: bigint | number | string, padding: number = 1): string {
                if (decimal == null) {
                        throw new TypeError(`Failed to convert '${decimal}' from decimal to hex`)
                }
-               if (typeof padding !== 'number') {
+               if (typeof padding !== 'number' || padding < 1 || padding > 0x1fffffffe) {
                        throw new TypeError('Invalid padding')
                }
                try {