Skip to content

Commit

Permalink
feat: add support for Buffers (#5)
Browse files Browse the repository at this point in the history
Also test for spread arguments
  • Loading branch information
JaKXz committed Feb 8, 2017
1 parent 8146fd4 commit 2548f57
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
3 changes: 2 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ module.exports = function argsert (typeConfig, ...args) {
: arg === null ? 'null'
: arg instanceof Error ? 'error'
: isPromise(arg) ? 'promise'
: typeof arg;
: Buffer.isBuffer(arg) ? 'buffer'
: typeof arg;

const typesAtIndex = types[index];
const errorMessage = invalidArgMessage.bind(this, positionName(index), typesAtIndex, observedType);
Expand Down
20 changes: 20 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,3 +118,23 @@ test('allows promise in the required config', t => {
test('allows rejected promise in the optional config', t => {
t.true(argsert('[promise]', Promise.reject(new Error('no timeout')).catch(() => {})));
});

test('the arguments object can be passed in, spread', t => {
function foo () {
return argsert('[string|number] <object>', ...arguments);
}

t.true(foo('far', {}));
});

test('allows buffer in the required config', t => {
const buffer = new Buffer('robin');

t.true(argsert('<buffer>', buffer));
});

test('allows buffer in the optional config', t => {
const buffer = new Buffer('batgirl');

t.true(argsert('[buffer]', buffer));
});

0 comments on commit 2548f57

Please sign in to comment.