Skip to content

Commit

Permalink
Fix matchStringOrRegexp TypeError on undefined (#998)
Browse files Browse the repository at this point in the history
  • Loading branch information
serahhh authored and paulmelnikow committed Oct 20, 2017
1 parent 4772df5 commit 62e7566
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/common.js
Expand Up @@ -320,7 +320,7 @@ function percentEncode(str) {
}

function matchStringOrRegexp(target, pattern) {
var str = !_.isUndefined(target) && target.toString ? target.toString() : target;
var str = (!_.isUndefined(target) && target.toString && target.toString()) || '';

return pattern instanceof RegExp ? str.match(pattern) : str === String(pattern);
}
Expand Down
3 changes: 3 additions & 0 deletions tests/test_common.js
Expand Up @@ -152,6 +152,9 @@ tap.test('matchStringOrRegexp', {only: true}, function (t) {

t.true(common.matchStringOrRegexp(123, 123), 'true if pattern is number and target matches');

t.false(common.matchStringOrRegexp(undefined, 'to not match'), 'handle undefined target when pattern is string');
t.false(common.matchStringOrRegexp(undefined, /not/), 'handle undefined target when pattern is regex');

t.ok(common.matchStringOrRegexp('to match', /match/), 'match if pattern is regex and target matches');
t.false(common.matchStringOrRegexp('to match', /not/), 'false if pattern is regex and target doesn\'t match');
t.end();
Expand Down

0 comments on commit 62e7566

Please sign in to comment.