Skip to content

Commit

Permalink
Check for invalid input (#59)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mottie authored and sindresorhus committed May 2, 2018
1 parent 6ffaeed commit 9ef944c
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
7 changes: 6 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,18 @@ module.exports = (obj, opts) => {
promiseModule: Promise
}, opts);

const objType = typeof obj;
if (objType === 'string' || objType === 'undefined' || obj === null) {
throw new TypeError(`Expected \`input\` to be a \`Function\` or \`Object\`, got \`${obj === null ? 'null' : objType}\``);
}

const filter = key => {
const match = pattern => typeof pattern === 'string' ? key === pattern : pattern.test(key);
return opts.include ? opts.include.some(match) : !opts.exclude.some(match);
};

let ret;
if (typeof obj === 'function') {
if (objType === 'function') {
ret = function () {
if (opts.excludeMain) {
return obj.apply(this, arguments);
Expand Down
11 changes: 11 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,17 @@ test('main', async t => {
t.is(await m(fixture)(), 'unicorn');
});

test('throw error on invalid input', t => {
let error = t.throws(() => m());
t.is(error.message, 'Expected `input` to be a `Function` or `Object`, got `undefined`');

error = t.throws(() => m(''));
t.is(error.message, 'Expected `input` to be a `Function` or `Object`, got `string`');

error = t.throws(() => m(null));
t.is(error.message, 'Expected `input` to be a `Function` or `Object`, got `null`');
});

test('error', async t => {
t.is(await m(fixture1)().catch(err => err), 'error');
});
Expand Down

0 comments on commit 9ef944c

Please sign in to comment.