From 3e6f07b4b5438361edc73f3984a843ca098520bf Mon Sep 17 00:00:00 2001 From: Chris Duncan Date: Wed, 16 Jul 2025 07:18:00 -0700 Subject: [PATCH] Use static codecs for convert functions. Formatting. --- src/lib/convert.ts | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/src/lib/convert.ts b/src/lib/convert.ts index ab35a73..091d6ac 100644 --- a/src/lib/convert.ts +++ b/src/lib/convert.ts @@ -5,7 +5,7 @@ import { ALPHABET } from './constants' export class base32 { /** - * Converts a base32 string to a Uint8Array of bytes. + * Convert a base32 string to a Uint8Array of bytes. * * @param {string} base32 - String to convert * @returns {Uint8Array} Byte array representation of the input string @@ -37,7 +37,7 @@ export class base32 { } /** - * Converts a base32 string to a hexadecimal string. + * Convert a base32 string to a hexadecimal string. * * @param {string} base32 - String to convert * @returns {string} Hexadecimal representation of the input base32 @@ -76,8 +76,10 @@ export class bin { } export class bytes { + static #decoder: TextDecoder = new TextDecoder() + /** - * Writes zeroes to memory to erase bytes and then transfers the buffer to + * 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 @@ -112,8 +114,9 @@ export class bytes { } return byteArray } + /** - * Converts a Uint8Aarray of bytes to a base32 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 @@ -151,7 +154,7 @@ export class bytes { } /** - * Sums an array of bytes to a decimal integer. If the result is larger than + * 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 @@ -172,7 +175,7 @@ export class bytes { } /** - * Converts a Uint8Array of bytes to a hexadecimal 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 @@ -184,13 +187,13 @@ export class bytes { } /** - * Converts a Uint8Array of bytes to a UTF-8 text string. + * 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 new TextDecoder().decode(bytes) + return this.#decoder.decode(bytes) } } @@ -331,6 +334,8 @@ export class obj { } export class utf8 { + static #encoder: TextEncoder = new TextEncoder() + /** * Convert a UTF-8 text string to a Uint8Array of bytes. * @@ -338,7 +343,7 @@ export class utf8 { * @returns {Uint8Array} Byte array representation of the input string */ static toBytes (utf8: string): Uint8Array { - return new TextEncoder().encode(utf8) as Uint8Array + return this.#encoder.encode(utf8) as Uint8Array } /** @@ -353,6 +358,7 @@ export class utf8 { } export default ` + const ALPHABET = '${ALPHABET}' const base32 = ${base32} const bin = ${bin} const bytes = ${bytes} -- 2.47.3