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

Commit 1ae01a0

Browse files
Alan Shawdaviddias
authored andcommitted
fix: do not throw when non buffer/string passed to isEncoded
License: MIT Signed-off-by: Alan Shaw <alan.shaw@protocol.ai>
1 parent 4d0fc17 commit 1ae01a0

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

src/index.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,11 @@ function isEncoded (bufOrString) {
9999
bufOrString = bufOrString.toString()
100100
}
101101

102+
// Ensure bufOrString is a string
103+
if (Object.prototype.toString.call(bufOrString) !== '[object String]') {
104+
return false
105+
}
106+
102107
const code = bufOrString.substring(0, 1)
103108
try {
104109
const base = getBase(code)

test/multibase.spec.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,3 +230,24 @@ describe('multibase.codes', () => {
230230
expect(Object.isFrozen(multibase.codes)).to.be.true()
231231
})
232232
})
233+
234+
describe('multibase.isEncoded', () => {
235+
it('should not throw for non string/buffer input', () => {
236+
const invalidInputs = [
237+
null,
238+
undefined,
239+
false,
240+
0,
241+
{},
242+
[],
243+
/[a-z]/,
244+
() => {},
245+
Symbol('test')
246+
]
247+
248+
invalidInputs.forEach(input => {
249+
expect(() => multibase.isEncoded(input)).to.not.throw()
250+
expect(multibase.isEncoded(input)).to.be.false()
251+
})
252+
})
253+
})

0 commit comments

Comments
 (0)