Skip to content

Commit

Permalink
Throw TypeError if argument is not a buffer
Browse files Browse the repository at this point in the history
  • Loading branch information
sonicdoe committed Jul 22, 2018
1 parent 8545f46 commit a4d9f10
Showing 2 changed files with 10 additions and 0 deletions.
4 changes: 4 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -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)
}
6 changes: 6 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
@@ -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)
})

0 comments on commit a4d9f10

Please # to comment.