From a4d9f10b6bf1cd468d1a5b9a283cdf437f8bb7b3 Mon Sep 17 00:00:00 2001 From: Jakob Krigovsky Date: Sun, 22 Jul 2018 16:46:20 +0200 Subject: [PATCH] Throw TypeError if argument is not a buffer --- index.js | 4 ++++ test.js | 6 ++++++ 2 files changed, 10 insertions(+) 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) +})