diff --git a/index.js b/index.js index b5507ce..d1e177b 100644 --- a/index.js +++ b/index.js @@ -3,5 +3,9 @@ const bindings = require('bindings')('ced.node') module.exports = buf => { + if (!Buffer.isBuffer(buf)) { + throw new TypeError('Expected a buffer') + } + return bindings.detectEncoding(buf) } diff --git a/test.js b/test.js index e0b0c4b..b84ae19 100644 --- a/test.js +++ b/test.js @@ -10,3 +10,9 @@ test('detects ASCII', t => { const buf = Buffer.from('tést', 'ascii') t.is(ced(buf), 'ASCII') }) + +test('throws a TypeError if the argument is not a buffer', t => { + t.throws(() => { + ced('tést') + }, TypeError) +})