'use strict' const { encodeText } = require('./util') /** @typedef {import('./types').CodecFactory} CodecFactory */ /** @typedef {import("./types").BaseName} BaseName */ /** @typedef {import("./types").BaseCode} BaseCode */ /** * Class to encode/decode in the supported Bases * */ class Base { /** * @param {BaseName} name * @param {BaseCode} code * @param {CodecFactory} factory * @param {string} alphabet */ constructor (name, code, factory, alphabet) { this.name = name this.code = code this.codeBuf = encodeText(this.code) this.alphabet = alphabet this.codec = factory(alphabet) } /** * @param {Uint8Array} buf * @returns {string} */ encode (buf) { return this.codec.encode(buf) } /** * @param {string} string * @returns {Uint8Array} */ decode (string) { for (const char of string) { if (this.alphabet && this.alphabet.indexOf(char) < 0) { throw new Error(`invalid character '${char}' in '${string}'`) } } return this.codec.decode(string) } } module.exports = Base