UNPKG

@thi.ng/base-n

Version:

Arbitrary base-n conversions w/ presets for base8/16/32/36/58/62/64/83/85, support for bigints and encoding/decoding of byte arrays

46 lines (45 loc) 875 B
import { BaseNDecoder } from "./decode.js"; import { BaseNEncoder } from "./encode.js"; const defBase = (chars) => new BaseN(chars); class BaseN { constructor(base) { this.base = base; this.N = base.length; this.enc = new BaseNEncoder(base); this.dec = new BaseNDecoder(base); } N; enc; dec; clear() { this.enc.clear(); } encode(x, size) { return this.enc.encode(x, size); } encodeBigInt(x, size) { return this.enc.encodeBigInt(x, size); } encodeBytes(buf, size) { return this.enc.encodeBytes(buf, size); } decode(x) { return this.dec.decode(x); } decodeBigInt(x) { return this.dec.decodeBigInt(x); } decodeBytes(x, buf) { return this.dec.decodeBytes(x, buf); } validate(x) { return this.dec.validate(x); } size(x) { return this.enc.size(x); } } export { BaseN, defBase };