Skip to content

Commit 284bbf4

Browse files
committed
Guard against a possible string being pulled from the query string and coerce to a Regular Expression
1 parent a5a32d1 commit 284bbf4

File tree

3 files changed

+21
-2
lines changed

3 files changed

+21
-2
lines changed

lib/util/coerce-to-reg-exp.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
var regexp;
2+
3+
module.exports = function coerceToRegExp (stringOrRegExp) {
4+
var result = stringOrRegExp;
5+
6+
if (typeof result === 'string') {
7+
eval('regexp = ' + stringOrRegExp);
8+
result = regexp;
9+
}
10+
11+
return result;
12+
};

lib/util/title-case.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
var R = require('ramda');
2+
var coerceToRegExp = require('./coerce-to-reg-exp');
23
var uppercaseFirst = require('./upper-case-first');
34

45
var DEFAULT = /[:\w-]/;
56

67
module.exports = R.curry(function titleCase (delim, text) {
7-
var words = text.split(delim || DEFAULT);
8+
var words = text.split(coerceToRegExp(delim) || DEFAULT);
89
return words.
910
map(uppercaseFirst).
1011
join('');

test/unit/utility/title-case.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,18 @@ describe('svg-react-loader/lib/util/title-case', () => {
1919
delim: /[:-]/g,
2020
text: 'foo-bar-baz',
2121
result: 'FooBarBaz'
22+
},
23+
{
24+
delim: '/[:-]/g',
25+
text: 'foo-bar-baz',
26+
result: 'FooBarBaz'
2227
}
2328
];
2429

2530
expectations.
2631
forEach((spec) => {
27-
const description = `titleCase(/${spec.delim.source}/, '${spec.text}') should equal '${spec.result}'`;
32+
const type = spec.delim.constructor.name;
33+
const description = `titleCase(${spec.delim} as ${type}, '${spec.text}') should equal '${spec.result}'`;
2834

2935
it(description, () => {
3036
titleCase(spec.delim, spec.text).

0 commit comments

Comments
 (0)