diff --git a/src/basex.mjs b/src/basex.mjs index d09e9e8c1b9fc54da1a7b3a12b8cf4b32d75c09f..58132ee92f18e0935d196c451f1b9e5d6e3f39e4 100644 --- a/src/basex.mjs +++ b/src/basex.mjs @@ -2,8 +2,8 @@ const B58_ALPHABET = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'; export const b58 = basex(B58_ALPHABET); export const b16 = basex('0123456789abcdef'); - -export default function basex(ALPHABET) { +export default basex; +export function basex(ALPHABET) { const ALPHABET_MAP = {}; const BASE = ALPHABET.length; const LEADER = ALPHABET.charAt(0); diff --git a/src/basex.test.mjs b/src/basex.test.mjs index 9971b3e9b0c072f5db4f82b41ea0686d6e369502..2cf198c16b3cb57a76f60b644587266fdfc408aa 100644 --- a/src/basex.test.mjs +++ b/src/basex.test.mjs @@ -7,3 +7,24 @@ const pubKey = 'AoxVA41dGL2s4ogMNdbCw3FFYjFo5FPK36LuiW1tjGbG'; test('b58 should decode/encode well', t => { t.is(app.b58.encode(app.b58.decode(pubKey)), pubKey); }); +test('basex dont allow ambigous alphabet (each character must be unique)', t => { + t.throws(() => app.basex('zz')); +}); +test('empty input empty output', t => { + t.is(app.b58.encode([]), ''); +}); +test('encode 0000 filled source', t => { + t.is(app.b16.encode([0, 0, 0, 0, 15]), '0000f'); +}); +test('decode 0000 filled source', t => { + t.deepEqual(app.b16.decode('0000f'), new Uint8Array([0, 0, 0, 0, 15])); +}); +test('decode out of base chr throw error', t => { + t.throws(() => app.b58.decode(pubKey + 'ยง')); +}); +test('no string decode throw', t => { + t.throws(() => app.b58.decode([])); +}); +test('decode empty string => empty array', t => { + t.deepEqual(app.b16.decode(''), new Uint8Array(0)); +});