8000 joinRegExp should accept both strings and regexp as input · webworkadmin/raven-js@067521d · GitHub
[go: up one dir, main page]

Skip to content

Commit 067521d

Browse files
committed
joinRegExp should accept both strings and regexp as input
1 parent b676c58 commit 067521d

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

src/raven.js

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,10 @@ function isFunction(what) {
347347
return typeof what === 'function';
348348
}
349349

350+
function isString(what) {
351+
return typeof what === 'string';
352+
}
353+
350354
function each(obj, callback) {
351355
var i, j;
352356

@@ -630,10 +634,19 @@ function isSetup() {
630634
}
631635

632636
function joinRegExp(patterns) {
633-
// Combine an array of regular expressions into one large regexp
637+
// Combine an array of regular expressions and strings into one large regexp
634638
var sources = [], i = patterns.length;
635639
// lol, map
636-
while (i--) sources[i] = patterns[i].source;
640+
while (i--) {
641+
if (isString(patterns[i])) {
642+
// If it's a string, we need to escape it
643+
// Taken from: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions
644+
sources[i] = patterns[i].replace(/([.*+?^=!:${}()|\[\]\/\\])/g, "\\$1");
645+
} else {
646+
// If it's a regexp already, we want to extract the source
647+
sources[i] = patterns[i].source;
648+
}
649+
}
637650
return new RegExp(sources.join('|'), 'i');
638651
}
639652

test/raven.test.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,15 @@ describe('globals', function() {
9393
});
9494
});
9595

96+
describe('isString', function() {
97+
it('should do as advertised', function() {
98+
assert.isTrue(isString(''));
99+
assert.isFalse(isString({}));
100+
assert.isFalse(isString(undefined));
101+
assert.isFalse(isString(function(){}))
102+
});
103+
});
104+
96105
describe('isSetup', function() {
97106
it('should return false with no JSON support', function() {
98107
globalServer = 'http://localhost/';
@@ -773,6 +782,14 @@ describe('globals', function() {
773782
]);
774783
});
775784
});
785+
786+
describe('joinRegExp', function() {
787+
it('should work as advertised', function() {
788+
assert.equal(joinRegExp([
789+
'a', 'b', 'a.b', /d/, /[0-9]/
790+
]).source, 'a|b|a\\.b|d|[0-9]');
791+
});
792+
});
776793
});
777794

778795
describe('Raven (public API)', function() {

0 commit comments

Comments
 (0)
0