import { ALPHABET } from '../constants'
export const base32 = Object.freeze({
- /**
- * 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
- },
-
/**
* Convert a base32 string to a Uint8Array of bytes.
*
+++ /dev/null
-//! SPDX-FileCopyrightText: 2025 Chris Duncan <chris@codecow.com>
-//! SPDX-License-Identifier: GPL-3.0-or-later
-
-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
- */
- toBytes (bin: string, padding: number = 1): Uint8Array<ArrayBuffer> {
- if (typeof bin !== 'string' || !/^[01]+$/i.test(bin)) {
- throw new TypeError('Invalid string when converting bin to bytes', { cause: bin })
- }
- if (typeof padding !== 'number' || padding < 1 || padding > 0xffffffff) {
- throw new TypeError('Invalid padding when converting bin to bytes', { cause: padding })
- }
- const pad = bin.length & 7
- if (pad) bin = bin.padStart(bin.length + 8 - pad, '0')
- const byteLength = bin.length >> 3
- const diff = padding - byteLength
- const offset = diff & ~(diff >> 31)
- const bytes = new Uint8Array(byteLength + offset)
- bytes.set(bin.match(/.{8}/g)?.map(b => parseInt(b, 2)) ?? [], offset)
- return bytes
- },
-})
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
- */
- 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.
//! SPDX-License-Identifier: GPL-3.0-or-later
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
- */
- toBin (decimal: bigint | number | string, padding: number = 0): string {
- if (typeof padding !== 'number') {
- throw new TypeError('Invalid padding')
- }
- try {
- return BigInt(decimal)
- .toString(2)
- .padStart(padding, '0')
- } catch (err) {
- throw new RangeError('Invalid decimal integer')
- }
- },
-
/**
* Convert a decimal integer to a Uint8Array of bytes. Fractional part is truncated.
*
//! SPDX-License-Identifier: GPL-3.0-or-later\r
\r
export { base32 } from './base32'\r
-export { bin } from './bin'\r
export { bytes } from './bytes'\r
export { dec } from './dec'\r
export { hex } from './hex'\r
export { utf8 } from './utf8'\r
+\r