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
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
- }
-}
+ },
+})
//! 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)
bin = bin.substring(8)
}
return new Uint8Array(bytes)
- }
-}
+ },
+})
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
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)
} 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)
+ },
+})
//! 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')
}
} 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`)
}
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`)
}
} catch (err) {
throw new RangeError('Invalid decimal integer')
}
- }
-}
+ },
+})
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 })
}
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))
- }
-}
+ },
+})
//! 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)
- }
-}
+ },
+})
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))
- }
-}
+ },
+})