]> git.codecow.com Git - libnemo.git/commitdiff
Use static codecs for convert functions. Formatting.
authorChris Duncan <chris@zoso.dev>
Wed, 16 Jul 2025 14:18:00 +0000 (07:18 -0700)
committerChris Duncan <chris@zoso.dev>
Wed, 16 Jul 2025 14:18:00 +0000 (07:18 -0700)
src/lib/convert.ts

index ab35a732262537658fab2a327d5d3690011bc289..091d6ac6ac79500bf91ac2edabb289baeb1aff7a 100644 (file)
@@ -5,7 +5,7 @@ import { ALPHABET } from './constants'
 \r
 export class base32 {\r
        /**\r
-       * Converts a base32 string to a Uint8Array of bytes.\r
+       * Convert a base32 string to a Uint8Array of bytes.\r
        *\r
        * @param {string} base32 - String to convert\r
        * @returns {Uint8Array} Byte array representation of the input string\r
@@ -37,7 +37,7 @@ export class base32 {
        }\r
 \r
        /**\r
-       * Converts a base32 string to a hexadecimal string.\r
+       * Convert a base32 string to a hexadecimal string.\r
        *\r
        * @param {string} base32 - String to convert\r
        * @returns {string} Hexadecimal representation of the input base32\r
@@ -76,8 +76,10 @@ export class bin {
 }\r
 \r
 export class bytes {\r
+       static #decoder: TextDecoder = new TextDecoder()\r
+\r
        /**\r
-       * Writes zeroes to memory to erase bytes and then transfers the buffer to\r
+       * Write zeroes to memory to erase bytes and then transfers the buffer to\r
        * render it inaccessible to any process.\r
        *\r
        * @param bytes - Buffer or bytes to erase\r
@@ -112,8 +114,9 @@ export class bytes {
                }\r
                return byteArray\r
        }\r
+\r
        /**\r
-       * Converts a Uint8Aarray of bytes to a base32 string.\r
+       * Convert a Uint8Aarray of bytes to a base32 string.\r
        *\r
        * @param {Uint8Array} bytes - Byte array to convert\r
        * @returns {string} Base32 string representation of the input bytes\r
@@ -151,7 +154,7 @@ export class bytes {
        }\r
 \r
        /**\r
-       * Sums an array of bytes to a decimal integer. If the result is larger than\r
+       * Sum an array of bytes to a decimal integer. If the result is larger than\r
        * Number.MAX_SAFE_INTEGER, it will be returned as a bigint.\r
        *\r
        * @param {Uint8Array} bytes - Byte array to convert\r
@@ -172,7 +175,7 @@ export class bytes {
        }\r
 \r
        /**\r
-       * Converts a Uint8Array of bytes to a hexadecimal string.\r
+       * Convert a Uint8Array of bytes to a hexadecimal string.\r
        *\r
        * @param {Uint8Array} bytes - Byte array to convert\r
        * @returns {string} Hexadecimal string representation of the input bytes\r
@@ -184,13 +187,13 @@ export class bytes {
        }\r
 \r
        /**\r
-       * Converts a Uint8Array of bytes to a UTF-8 text string.\r
+       * Convert a Uint8Array of bytes to a UTF-8 text string.\r
        *\r
        * @param {Uint8Array} bytes - Byte array to convert\r
        * @returns {string} UTF-8 encoded text string\r
        */\r
        static toUtf8 (bytes: Uint8Array<ArrayBuffer>): string {\r
-               return new TextDecoder().decode(bytes)\r
+               return this.#decoder.decode(bytes)\r
        }\r
 }\r
 \r
@@ -331,6 +334,8 @@ export class obj {
 }\r
 \r
 export class utf8 {\r
+       static #encoder: TextEncoder = new TextEncoder()\r
+\r
        /**\r
        * Convert a UTF-8 text string to a Uint8Array of bytes.\r
        *\r
@@ -338,7 +343,7 @@ export class utf8 {
        * @returns {Uint8Array} Byte array representation of the input string\r
        */\r
        static toBytes (utf8: string): Uint8Array<ArrayBuffer> {\r
-               return new TextEncoder().encode(utf8) as Uint8Array<ArrayBuffer>\r
+               return this.#encoder.encode(utf8) as Uint8Array<ArrayBuffer>\r
        }\r
 \r
        /**\r
@@ -353,6 +358,7 @@ export class utf8 {
 }\r
 \r
 export default `\r
+       const ALPHABET = '${ALPHABET}'\r
        const base32 = ${base32}\r
        const bin = ${bin}\r
        const bytes = ${bytes}\r