From: Chris Duncan Date: Tue, 30 Jun 2026 19:21:39 +0000 (-0700) Subject: Refactor conversion classes to flat frozen objects. X-Git-Url: https://git.codecow.com/?a=commitdiff_plain;h=00585e1e1d164e1056b18579e24082ed860772ea;p=libnemo.git Refactor conversion classes to flat frozen objects. --- diff --git a/src/lib/convert/base32.ts b/src/lib/convert/base32.ts index 93cb99c..4e554c1 100644 --- a/src/lib/convert/base32.ts +++ b/src/lib/convert/base32.ts @@ -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 { + * 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 { 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 - } -} + }, +}) diff --git a/src/lib/convert/bin.ts b/src/lib/convert/bin.ts index 548d7e3..5ca0876 100644 --- a/src/lib/convert/bin.ts +++ b/src/lib/convert/bin.ts @@ -1,14 +1,14 @@ //! SPDX-FileCopyrightText: 2025 Chris Duncan //! 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 { + * 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 { 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) - } -} + }, +}) diff --git a/src/lib/convert/bytes.ts b/src/lib/convert/bytes.ts index fe63fc1..9fd87e0 100644 --- a/src/lib/convert/bytes.ts +++ b/src/lib/convert/bytes.ts @@ -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 | 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 | 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): 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): 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): 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): string { + return decoder.decode(bytes) + }, +}) diff --git a/src/lib/convert/dec.ts b/src/lib/convert/dec.ts index 15016cf..b7e5bf3 100644 --- a/src/lib/convert/dec.ts +++ b/src/lib/convert/dec.ts @@ -1,15 +1,15 @@ //! SPDX-FileCopyrightText: 2025 Chris Duncan //! 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 { + * 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 { 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') } - } -} + }, +}) diff --git a/src/lib/convert/hex.ts b/src/lib/convert/hex.ts index adb072d..43be081 100644 --- a/src/lib/convert/hex.ts +++ b/src/lib/convert/hex.ts @@ -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 { + * 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 { return new Uint8Array(this.toArray(hex, padding)) - } -} + }, +}) diff --git a/src/lib/convert/obj.ts b/src/lib/convert/obj.ts index 55cb994..d701a33 100644 --- a/src/lib/convert/obj.ts +++ b/src/lib/convert/obj.ts @@ -1,18 +1,18 @@ //! SPDX-FileCopyrightText: 2025 Chris Duncan //! 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 { + * Convert a numerically-indexed object of 8-bit values to a Uint8Array of bytes. + * + * @param {Record} obj - Object to convert + * @returns {Uint8Array} Byte array representation of the input object + */ + toBytes (obj: Record): Uint8Array { const values = Object.keys(obj) .map(key => +key) .sort((a, b) => a - b) .map(i => obj[i]) return new Uint8Array(values) - } -} + }, +}) diff --git a/src/lib/convert/utf8.ts b/src/lib/convert/utf8.ts index 6c40c2b..1d0037f 100644 --- a/src/lib/convert/utf8.ts +++ b/src/lib/convert/utf8.ts @@ -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 { - return this.encoder.encode(utf8) as Uint8Array - } + * 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 { + 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)) - } -} + }, +})