Skip to content
This repository has been archived by the owner on Aug 24, 2021. It is now read-only.

feat: more encoding, errors, spec tests #55

Merged
merged 11 commits into from Jun 9, 2020
2 changes: 1 addition & 1 deletion src/base.js
Expand Up @@ -16,7 +16,7 @@ class Base {
decode (string) {
for (const char of string) {
if (this.alphabet && this.alphabet.indexOf(char) < 0) {
throw new Error(`invalid ${this.name} character '${char}' in '${string}'`)
throw new Error(`invalid character '${char}' in '${string}'`)
}
}
return this.engine.decode(string)
Expand Down
6 changes: 6 additions & 0 deletions src/index.js
Expand Up @@ -55,6 +55,12 @@ function decode (data) {
if (Buffer.isBuffer(data)) {
data = data.toString()
}
const prefix = data[0]

// Make all encodings case-insensitive expect the ones that include upper and lower chars in the alphabet
if (!['z', 'Z', 'm', 'M', 'u', 'U', '\x00'].includes(prefix)) {
hugomrdias marked this conversation as resolved.
Show resolved Hide resolved
data = data.toLocaleLowerCase()
hugomrdias marked this conversation as resolved.
Show resolved Hide resolved
}
const enc = encoding(data[0])
return Buffer.from(enc.decode(data.substring(1)))
}
Expand Down
2 changes: 1 addition & 1 deletion test/spec-test1.spec.js
Expand Up @@ -67,7 +67,7 @@ describe('spec test1', () => {
const nonEncodedBuf = Buffer.from(base.code + '^!@$%!#$%@#y')
expect(() => {
multibase.decode(nonEncodedBuf)
}).to.throw(Error, `invalid ${name} character '^' in '^!@$%!#$%@#y'`)
}).to.throw(Error, 'invalid character \'^\' in \'^!@$%!#$%@#y\'')
})
})
}
Expand Down
3 changes: 1 addition & 2 deletions test/spec-test6.spec.js
Expand Up @@ -19,8 +19,7 @@ const encoded = [
['base36upper', 'KfUVrSIVVnFRbJWAJo']
]

// we dont need to actually test these because we check the chars against the alphabet
describe.skip('spec test6', () => {
describe('spec test6', () => {
for (const e of encoded) {
hugomrdias marked this conversation as resolved.
Show resolved Hide resolved
const name = e[0]
const output = e[1]
Expand Down