Skip to content
Permalink
Browse files
Check for invalid input (#59)
  • Loading branch information
Mottie authored and sindresorhus committed May 2, 2018
1 parent 6ffaeed commit 9ef944cf168cb589b611c48f5ef06bd5ea736a3c
Showing 2 changed files with 17 additions and 1 deletion.
@@ -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);
11 test.js
@@ -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');
});

0 comments on commit 9ef944c

Please sign in to comment.