Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: allow undefined for optional arguments #6

Merged
merged 2 commits into from Feb 9, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 4 additions & 2 deletions index.js
Expand Up @@ -42,7 +42,7 @@ module.exports = function argsert (typeConfig, ...args) {
if ('optional' in typesAtIndex) {
const optional = typesAtIndex.optional;

if (optional.indexOf('*') < 0 && optional.indexOf(observedType) < 0) {
if (arg !== undefined && optional.indexOf('*') < 0 && optional.indexOf(observedType) < 0) {
throw new TypeError(errorMessage('optional'));
}
}
Expand Down Expand Up @@ -91,7 +91,9 @@ function positionName (index) {
}

function expectedTypes (types, kind) {
return types[kind].join(' or ');
return types[kind]
.concat(kind === 'optional' && types[kind].indexOf('undefined') < 0 ? 'undefined' : [])
.join(' or ');
}

function isOptional (arg) {
Expand Down
45 changes: 34 additions & 11 deletions test.js
Expand Up @@ -10,22 +10,22 @@ test('does not throw exception if optional argument is not provided', async t =>
test('throws exception if wrong type is provided for optional argument', t => {
t.throws(
() => argsert('[object|number]', 'hello'),
/Invalid first argument. Expected object or number but received string./
/Invalid first argument. Expected object or number or undefined but received string./
);
t.throws(
argsertPromise('[object|number]', 'hello'),
/Invalid first argument. Expected object or number but received string./
/Invalid first argument. Expected object or number or undefined but received string./
);
});

test('does not throw exception if optional argument is valid', t => {
t.true(argsert('[object]', {foo: 'bar'}));
});

test('throws exception if required argument is not provided', t => {
test('throws exception if required arguments are not provided', t => {
t.throws(
() => argsert('<object>'),
/Not enough arguments provided. Expected 1 but received 0./
() => argsert('<object> <*>'),
/Not enough arguments provided. Expected 2 but received 0./
);
});

Expand Down Expand Up @@ -85,6 +85,10 @@ test('allows wildcard to be used in optional configuration', async t => {
t.true(await argsertPromise('[string] [*]', 'foo', {}));
});

test('allows undefined for optional arguments', t => {
t.true(argsert('[string] <*>', undefined, {}));
});

test('throws when the optional config has broken syntax', t => {
t.throws(
() => argsert('<*> [foo||bar]', 'baz', 'boo'),
Expand Down Expand Up @@ -119,6 +123,18 @@ test('allows rejected promise in the optional config', t => {
t.true(argsert('[promise]', Promise.reject(new Error('no timeout')).catch(() => {})));
});

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));
});

test('the arguments object can be passed in, spread', t => {
function foo () {
return argsert('[string|number] <object>', ...arguments);
Expand All @@ -127,14 +143,21 @@ test('the arguments object can be passed in, spread', t => {
t.true(foo('far', {}));
});

test('allows buffer in the required config', t => {
const buffer = new Buffer('robin');
test('Function.prototype.apply with the arguments object', t => {
function foo () {
return argsert.apply(this, arguments);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bcoe so this is probably the way that yargs/yargs#773 would make use of this lib :)

}

t.true(argsert('<buffer>', buffer));
t.true(foo('[string|number] <object>', 'bar', {}));
});

test('allows buffer in the optional config', t => {
const buffer = new Buffer('batgirl');
test('Function.prototype.call with the arguments object spread', t => {
function foo () {
return argsert.call(this, ...arguments);
}

t.true(argsert('[buffer]', buffer));
t.throws(
() => foo('[number] <object>', 'bar', {}),
/Invalid first argument. Expected number or undefined but received string./
);
});