Skip to content

Commit

Permalink
Merge pull request #180 from johnbacon/patch-1
Browse files Browse the repository at this point in the history
Improved solution to issue #179 (invalid options throwing errors in joinRegExp)
  • Loading branch information
mattrobenolt committed Jan 12, 2014
2 parents 61d5b88 + 74b868d commit f3baeee
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 9 deletions.
19 changes: 10 additions & 9 deletions src/raven.js
Original file line number Diff line number Diff line change
Expand Up @@ -642,16 +642,17 @@ function isSetup() {
function joinRegExp(patterns) {
// Combine an array of regular expressions and strings into one large regexp
// Be mad.
var sources = [], i = patterns.length;
while (i--) {
if (!isUndefined(patterns[i]) && patterns[i]) {
sources[i] = isString(patterns[i]) ?
// If it's a string, we need to escape it
// Taken from: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions
patterns[i].replace(/([.*+?^=!:${}()|\[\]\/\\])/g, "\\$1") :
// If it's a regexp already, we want to extract the source
patterns[i].source;
var sources = [], len = patterns.length;
for (var i = 0; i < len; i++) {
if (isString(patterns[i])) {
// If it's a string, we need to escape it
// Taken from: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions
sources.push(patterns[i].replace(/([.*+?^=!:${}()|\[\]\/\\])/g, "\\$1"));
} else if (patterns[i] && patterns[i].source) {
// If it's a regexp already, we want to extract the source
sources.push(patterns[i].source);
}
// Intentionally skip other cases
}
return new RegExp(sources.join('|'), 'i');
}
Expand Down
6 changes: 6 additions & 0 deletions test/raven.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -879,6 +879,12 @@ describe('globals', function() {
'a', 'b', null, undefined
]).source, 'a|b');
});

it('should skip entries that are not strings or regular expressions in the passed array of patterns', function() {
assert.equal(joinRegExp([
'a', 'b', null, 'a.b', undefined, true, /d/, 123, {}, /[0-9]/, []
]).source, 'a|b|a\\.b|d|[0-9]');
});
});
});

Expand Down

0 comments on commit f3baeee

Please sign in to comment.