Skip to content

Commit

Permalink
style: pull out isMissinFrom check
Browse files Browse the repository at this point in the history
  • Loading branch information
JaKXz committed Feb 10, 2017
1 parent 942da4f commit 3f56f6b
Showing 1 changed file with 8 additions and 12 deletions.
20 changes: 8 additions & 12 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,20 +31,12 @@ module.exports = function argsert (typeConfig, ...args) {
const typesAtIndex = types[index];
const errorMessage = invalidArgMessage.bind(this, positionName(index), typesAtIndex, observedType);

if ('required' in typesAtIndex) {
const required = typesAtIndex.required;

if (required.indexOf(observedType) < 0 && required.indexOf('*') < 0) {
throw new TypeError(errorMessage('required'));
}
if (('required' in typesAtIndex) && isMissingFrom(typesAtIndex.required, observedType)) {
throw new TypeError(errorMessage('required'));
}

if ('optional' in typesAtIndex) {
const optional = typesAtIndex.optional;

if (arg !== undefined && optional.indexOf('*') < 0 && optional.indexOf(observedType) < 0) {
throw new TypeError(errorMessage('optional'));
}
if (('optional' in typesAtIndex) && arg !== undefined && isMissingFrom(typesAtIndex.optional, observedType)) {
throw new TypeError(errorMessage('optional'));
}
});

Expand Down Expand Up @@ -73,6 +65,10 @@ function getTypes (typeConfig) {
}, {});
}

function isMissingFrom (types, observed) {
return types.indexOf(observed) < 0 && types.indexOf('*') < 0;
}

function compactArgs (args) {
const lastArg = args[args.length - 1];
if (lastArg === undefined || lastArg === '') {
Expand Down

0 comments on commit 3f56f6b

Please sign in to comment.