]> git.codecow.com Git - libnemo.git/commitdiff
Refactor conversion classes to flat frozen objects.
authorChris Duncan <chris@codecow.com>
Tue, 30 Jun 2026 19:21:39 +0000 (12:21 -0700)
committerChris Duncan <chris@codecow.com>
Tue, 30 Jun 2026 19:21:39 +0000 (12:21 -0700)
src/lib/convert/base32.ts
src/lib/convert/bin.ts
src/lib/convert/bytes.ts
src/lib/convert/dec.ts
src/lib/convert/hex.ts
src/lib/convert/obj.ts
src/lib/convert/utf8.ts

index 93cb99cdc8d7f522a83de6128c2033583cdaca1a..4e554c1ccc9489ea64430aeb8586486130272dad 100644 (file)
@@ -3,14 +3,14 @@
 
 import { ALPHABET } from '../constants'
 
-export class base32 {
+export const base32 = Object.freeze({
        /**
-       * Convert a base32 string to a Uint8Array of bytes.
-       *
-       * @param {string} base32 - String to convert
-       * @returns {Uint8Array} Byte array representation of the input string
-       */
-       static toBytes (base32: string): Uint8Array<ArrayBuffer> {
+        * Convert a base32 string to a Uint8Array of bytes.
+        *
+        * @param {string} base32 - String to convert
+        * @returns {Uint8Array} Byte array representation of the input string
+        */
+       toBytes (base32: string): Uint8Array<ArrayBuffer> {
                const leftover = (base32.length * 5) % 8
                const offset = leftover === 0
                        ? 0
@@ -39,15 +39,15 @@ export class base32 {
                        output = output.slice(1)
                }
                return output
-       }
+       },
 
        /**
-       * Convert a base32 string to an ArrayBuffer.
-       *
-       * @param {string} base32 - String to convert
-       * @returns {Uint8Array} Byte array representation of the input string
-       */
-       static toBuffer (base32: string): ArrayBuffer {
+        * Convert a base32 string to an ArrayBuffer.
+        *
+        * @param {string} base32 - String to convert
+        * @returns {Uint8Array} Byte array representation of the input string
+        */
+       toBuffer (base32: string): ArrayBuffer {
                return this.toBytes(base32).buffer
-       }
-}
+       },
+})
index 548d7e328f78e6d345b2dcf4c6e2859e2e390784..5ca0876701839f15fa655305ef90c0975010f21c 100644 (file)
@@ -1,14 +1,14 @@
 //! SPDX-FileCopyrightText: 2025 Chris Duncan <chris@codecow.com>
 //! SPDX-License-Identifier: GPL-3.0-or-later
 
-export class bin {
+export const bin = Object.freeze({
        /**
-       * Convert a binary string to a Uint8Array of bytes.
-       *
-       * @param {string} bin - String to convert
-       * @returns {Uint8Array} Byte array representation of the input string
-       */
-       static toBytes (bin: string): Uint8Array<ArrayBuffer> {
+        * Convert a binary string to a Uint8Array of bytes.
+        *
+        * @param {string} bin - String to convert
+        * @returns {Uint8Array} Byte array representation of the input string
+        */
+       toBytes (bin: string): Uint8Array<ArrayBuffer> {
                const bytes: number[] = []
                while (bin.length > 0) {
                        const bits = bin.substring(0, 8)
@@ -16,5 +16,5 @@ export class bin {
                        bin = bin.substring(8)
                }
                return new Uint8Array(bytes)
-       }
-}
+       },
+})
index fe63fc1b7a6be43e9562e191827fb92ceaef16a6..9fd87e038030d0b53c22e40375f3696ef755ebb8 100644 (file)
@@ -3,30 +3,30 @@
 
 import { ALPHABET } from '../constants'
 
-export class bytes {
-       static decoder: TextDecoder = new TextDecoder()
+const decoder: TextDecoder = new TextDecoder()
 
+export const bytes = Object.freeze({
        /**
-       * Write zeroes to memory to erase bytes and then transfers the buffer to
-       * render it inaccessible to any process.
-       *
-       * @param bytes - Buffer or bytes to erase
-       */
-       static erase (bytes?: ArrayBuffer | Uint8Array<ArrayBuffer> | null): void {
+        * Write zeroes to memory to erase bytes and then transfers the buffer to
+        * render it inaccessible to any process.
+        *
+        * @param bytes - Buffer or bytes to erase
+        */
+       erase (bytes?: ArrayBuffer | Uint8Array<ArrayBuffer> | null): void {
                if (bytes == null) return
                if (bytes instanceof ArrayBuffer && bytes.byteLength === 0) return
                if (bytes instanceof Uint8Array && bytes.buffer.byteLength === 0) return
                bytes = bytes instanceof ArrayBuffer ? new Uint8Array(bytes) : bytes
                bytes.fill(0)
-       }
+       },
 
        /**
-       * Convert a Uint8Aarray of bytes to a base32 string.
-       *
-       * @param {Uint8Array} bytes - Byte array to convert
-       * @returns {string} Base32 string representation of the input bytes
-       */
-       static toBase32 (bytes: ArrayBuffer | Uint8Array): string {
+        * Convert a Uint8Aarray of bytes to a base32 string.
+        *
+        * @param {Uint8Array} bytes - Byte array to convert
+        * @returns {string} Base32 string representation of the input bytes
+        */
+       toBase32 (bytes: ArrayBuffer | Uint8Array): string {
                if (bytes instanceof ArrayBuffer) bytes = new Uint8Array(bytes)
                const leftover = (bytes.length * 8) % 5
                const offset = leftover === 0
@@ -47,26 +47,26 @@ export class bytes {
                        output += ALPHABET[(value << (5 - (bits + offset))) & 31]
                }
                return output
-       }
+       },
 
        /**
-       * Convert a Uint8Array of bytes to a binary string.
-       *
-       * @param {Uint8Array} bytes - Byte array to convert
-       * @returns {string} Binary string representation of the input value
-       */
-       static toBin (bytes: Uint8Array<ArrayBuffer>): string {
+        * Convert a Uint8Array of bytes to a binary string.
+        *
+        * @param {Uint8Array} bytes - Byte array to convert
+        * @returns {string} Binary string representation of the input value
+        */
+       toBin (bytes: Uint8Array<ArrayBuffer>): string {
                return [...bytes].map(b => b.toString(2).padStart(8, '0')).join('')
-       }
+       },
 
        /**
-       * Sum an array of bytes to a decimal integer. If the result is larger than
-       * Number.MAX_SAFE_INTEGER, it will be returned as a bigint.
-       *
-       * @param {Uint8Array} bytes - Byte array to convert
-       * @returns {bigint|number} Decimal sum of the literal byte values
-       */
-       static toDec (bytes: Uint8Array): bigint | number {
+        * Sum an array of bytes to a decimal integer. If the result is larger than
+        * Number.MAX_SAFE_INTEGER, it will be returned as a bigint.
+        *
+        * @param {Uint8Array} bytes - Byte array to convert
+        * @returns {bigint|number} Decimal sum of the literal byte values
+        */
+       toDec (bytes: Uint8Array): bigint | number {
                let decimal = 0n
                for (let i = bytes.byteLength; i > 0; i--) {
                        decimal += BigInt(bytes[i - 1]) << (BigInt(bytes.byteLength - i) * 8n)
@@ -76,30 +76,30 @@ export class bytes {
                } else {
                        return Number(decimal)
                }
-       }
+       },
 
        /**
-       * Convert a Uint8Array of bytes to a hexadecimal string.
-       *
-       * @param {Uint8Array} bytes - Byte array to convert
-       * @returns {string} Hexadecimal string representation of the input bytes
-       */
-       static toHex (bytes: ArrayBuffer | Uint8Array): string {
+        * Convert a Uint8Array of bytes to a hexadecimal string.
+        *
+        * @param {Uint8Array} bytes - Byte array to convert
+        * @returns {string} Hexadecimal string representation of the input bytes
+        */
+       toHex (bytes: ArrayBuffer | Uint8Array): string {
                if (bytes instanceof ArrayBuffer) bytes = new Uint8Array(bytes)
                if (bytes.buffer instanceof ArrayBuffer && bytes.buffer.byteLength === 0) return ''
                return [...bytes]
                        .map(byte => byte.toString(16).padStart(2, '0'))
                        .join('')
                        .toUpperCase()
-       }
+       },
 
        /**
-       * Convert a Uint8Array of bytes to a UTF-8 text string.
-       *
-       * @param {Uint8Array} bytes - Byte array to convert
-       * @returns {string} UTF-8 encoded text string
-       */
-       static toUtf8 (bytes: Uint8Array<ArrayBuffer>): string {
-               return this.decoder.decode(bytes)
-       }
-}
+        * Convert a Uint8Array of bytes to a UTF-8 text string.
+        *
+        * @param {Uint8Array} bytes - Byte array to convert
+        * @returns {string} UTF-8 encoded text string
+        */
+       toUtf8 (bytes: Uint8Array<ArrayBuffer>): string {
+               return decoder.decode(bytes)
+       },
+})
index 15016cf63b601e9bb0f6a81919ad587aeefea84b..b7e5bf3f2aebc3a82bb3fc61bb57273d410528ce 100644 (file)
@@ -1,15 +1,15 @@
 //! SPDX-FileCopyrightText: 2025 Chris Duncan <chris@codecow.com>
 //! SPDX-License-Identifier: GPL-3.0-or-later
 
-export class dec {
+export const dec = Object.freeze({
        /**
-       * Convert a decimal integer to a binary 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
-       * @returns {string} Binary string representation of the input decimal
-       */
-       static toBin (decimal: bigint | number | string, padding: number = 0): string {
+        * Convert a decimal integer to a binary 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
+        * @returns {string} Binary string representation of the input decimal
+        */
+       toBin (decimal: bigint | number | string, padding: number = 0): string {
                if (typeof padding !== 'number') {
                        throw new TypeError('Invalid padding')
                }
@@ -20,16 +20,16 @@ export class dec {
                } catch (err) {
                        throw new RangeError('Invalid decimal integer')
                }
-       }
+       },
 
        /**
-       * 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
-       * @returns {Uint8Array} Byte array representation of the input decimal
-       */
-       static toBytes (decimal: bigint | number | string, padding: number = 0): Uint8Array<ArrayBuffer> {
+        * 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
+        * @returns {Uint8Array} Byte array representation of the input decimal
+        */
+       toBytes (decimal: bigint | number | string, padding: number = 0): Uint8Array<ArrayBuffer> {
                if (decimal == null) {
                        throw new TypeError(`Failed to convert '${decimal}' from decimal to bytes`)
                }
@@ -46,16 +46,16 @@ export class dec {
                const result = new Uint8Array(Math.max(padding, bytes.length))
                result.set(bytes)
                return (result.reverse())
-       }
+       },
 
        /**
-       * 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
-       * @returns {string} Hexadecimal string representation of the input decimal
-       */
-       static toHex (decimal: bigint | number | string, padding: number = 0): string {
+        * 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
+        * @returns {string} Hexadecimal string representation of the input decimal
+        */
+       toHex (decimal: bigint | number | string, padding: number = 0): string {
                if (decimal == null) {
                        throw new TypeError(`Failed to convert '${decimal}' from decimal to hex`)
                }
@@ -70,5 +70,5 @@ export class dec {
                } catch (err) {
                        throw new RangeError('Invalid decimal integer')
                }
-       }
-}
+       },
+})
index adb072dbf1dd9f7dda2c6afed62c74ad1cc5dd3d..43be081383daffa5376a92f233b722615f8680c1 100644 (file)
@@ -3,15 +3,15 @@
 
 import { dec } from './dec'
 
-export class hex {
+export const hex = Object.freeze({
        /**
-       * Convert a hexadecimal string to an array of decimal byte values.
-       *
-       * @param {string} hex - Hexadecimal number string to convert
-       * @param {number}[padding=0] - Minimum length of the resulting array padded as necessary with starting 0 values
-       * @returns {number[]} Decimal array representation of the input value
-       */
-       static toArray (hex: string, padding: number = 0): number[] {
+        * Convert a hexadecimal string to an array of decimal byte values.
+        *
+        * @param {string} hex - Hexadecimal number string to convert
+        * @param {number}[padding=0] - Minimum length of the resulting array padded as necessary with starting 0 values
+        * @returns {number[]} Decimal array representation of the input value
+        */
+       toArray (hex: string, padding: number = 0): number[] {
                if (typeof hex !== 'string' || !/^[A-Fa-f0-9]+$/i.test(hex)) {
                        throw new TypeError('Invalid string when converting hex to array', { cause: hex })
                }
@@ -26,37 +26,37 @@ export class hex {
                const diff = padding - hexArray.length
                const pad = new Array(diff > 0 ? diff : 0).fill(0)
                return pad.concat(hexArray.map(v => parseInt(v, 16)))
-       }
+       },
 
        /**
-       * Convert a hexadecimal string to a binary string.
-       *
-       * @param {string} hex - Hexadecimal number string to convert
-       * @returns {string} Binary string representation of the input value
-       */
-       static toBin (hex: string): string {
+        * Convert a hexadecimal string to a binary string.
+        *
+        * @param {string} hex - Hexadecimal number string to convert
+        * @returns {string} Binary string representation of the input value
+        */
+       toBin (hex: string): string {
                return [...hex].map(c => dec.toBin(parseInt(c, 16), 4)).join('')
-       }
+       },
 
        /**
-       * Convert a hexadecimal string to an ArrayBuffer.
-       *
-       * @param {string} hex - Hexadecimal number string to convert
-       * @param {number} [padding=0] - Minimum length of the resulting array padded as necessary with starting 0x00 bytes
-       * @returns {ArrayBuffer} Buffer representation of the input value
-       */
-       static toBuffer (hex: string, padding: number = 0): ArrayBuffer {
+        * Convert a hexadecimal string to an ArrayBuffer.
+        *
+        * @param {string} hex - Hexadecimal number string to convert
+        * @param {number} [padding=0] - Minimum length of the resulting array padded as necessary with starting 0x00 bytes
+        * @returns {ArrayBuffer} Buffer representation of the input value
+        */
+       toBuffer (hex: string, padding: number = 0): ArrayBuffer {
                return this.toBytes(hex, padding).buffer
-       }
+       },
 
        /**
-       * Convert a hexadecimal string to a Uint8Array of bytes.
-       *
-       * @param {string} hex - Hexadecimal number string to convert
-       * @param {number} [padding=0] - Minimum length of the resulting array padded as necessary with starting 0x00 bytes
-       * @returns {Uint8Array} Byte array representation of the input value
-       */
-       static toBytes (hex: string, padding: number = 0): Uint8Array<ArrayBuffer> {
+        * Convert a hexadecimal string to a Uint8Array of bytes.
+        *
+        * @param {string} hex - Hexadecimal number string to convert
+        * @param {number} [padding=0] - Minimum length of the resulting array padded as necessary with starting 0x00 bytes
+        * @returns {Uint8Array} Byte array representation of the input value
+        */
+       toBytes (hex: string, padding: number = 0): Uint8Array<ArrayBuffer> {
                return new Uint8Array(this.toArray(hex, padding))
-       }
-}
+       },
+})
index 55cb99418b4e61398e1cb5387380f49c26282f55..d701a338b11cf614729fb710e2b5b812be6a4405 100644 (file)
@@ -1,18 +1,18 @@
 //! SPDX-FileCopyrightText: 2025 Chris Duncan <chris@codecow.com>
 //! SPDX-License-Identifier: GPL-3.0-or-later
 
-export class obj {
+export const obj = Object.freeze({
        /**
-       * 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
-       */
-       static toBytes (obj: { [key: number]: number }): Uint8Array<ArrayBuffer> {
+        * Convert a numerically-indexed object of 8-bit values to a Uint8Array of bytes.
+        *
+        * @param {Record<number, number>} obj - Object to convert
+        * @returns {Uint8Array} Byte array representation of the input object
+        */
+       toBytes (obj: Record<number, number>): Uint8Array<ArrayBuffer> {
                const values = Object.keys(obj)
                        .map(key => +key)
                        .sort((a, b) => a - b)
                        .map(i => obj[i])
                return new Uint8Array(values)
-       }
-}
+       },
+})
index 6c40c2bdd6ff12723138cc606370aad2c58501ef..1d0037fabd3539be1edfe2897c83b62c6c614dfb 100644 (file)
@@ -3,36 +3,36 @@
 
 import { bytes } from './bytes'
 
-export class utf8 {
-       static encoder: TextEncoder = new TextEncoder()
+const encoder: TextEncoder = new TextEncoder()
 
+export const utf8 = Object.freeze({
        /**
-       * Convert a UTF-8 text string to an ArrayBuffer.
-       *
-       * @param {string} utf8 - String to convert
-       * @returns {ArrayBuffer} Buffer representation of the input string
-       */
-       static toBuffer (utf8: string): ArrayBuffer {
+        * Convert a UTF-8 text string to an ArrayBuffer.
+        *
+        * @param {string} utf8 - String to convert
+        * @returns {ArrayBuffer} Buffer representation of the input string
+        */
+       toBuffer (utf8: string): ArrayBuffer {
                return this.toBytes(utf8).buffer
-       }
+       },
 
        /**
-       * Convert a UTF-8 text string to a Uint8Array of bytes.
-       *
-       * @param {string} utf8 - String to convert
-       * @returns {Uint8Array} Byte array representation of the input string
-       */
-       static toBytes (utf8: string): Uint8Array<ArrayBuffer> {
-               return this.encoder.encode(utf8) as Uint8Array<ArrayBuffer>
-       }
+        * Convert a UTF-8 text string to a Uint8Array of bytes.
+        *
+        * @param {string} utf8 - String to convert
+        * @returns {Uint8Array} Byte array representation of the input string
+        */
+       toBytes (utf8: string): Uint8Array<ArrayBuffer> {
+               return encoder.encode(utf8)
+       },
 
        /**
-       * Convert a string to a hexadecimal representation
-       *
-       * @param {string} utf8 - String to convert
-       * @returns {string} Hexadecimal representation of the input string
-       */
-       static toHex (utf8: string): string {
+        * Convert a string to a hexadecimal representation
+        *
+        * @param {string} utf8 - String to convert
+        * @returns {string} Hexadecimal representation of the input string
+        */
+       toHex (utf8: string): string {
                return bytes.toHex(this.toBytes(utf8))
-       }
-}
+       },
+})