Skip to content

Commit bc4f0cf

Browse files
committed
Improve regexp to extract casters (fix #126)
1 parent bebf5b6 commit bc4f0cf

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

src/index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ const builtInCasters = {
1212
const parseValue = (value, key, options) => {
1313
// Match type casting operators like string(true)
1414
const casters = { ...builtInCasters, ...options.casters };
15-
const casting = value.match(/^(\w+)\((.*)\)$/);
15+
const castersList = Object.keys(casters).join('|');
16+
const castersRegexp = new RegExp(`^(${castersList})\\(([^)]*)\\)$`);
17+
const casting = value.match(castersRegexp);
1618
if (casting && casters[casting[1]]) {
1719
return casters[casting[1]](casting[2]);
1820
}

test/index.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,12 +185,24 @@ test('filter: $in operator (multiple keys)', (t) => {
185185
t.deepEqual(res.filter, { key: { $in: ['a', 'b'] } });
186186
});
187187

188+
test('filter: $in operator (multiple keys), with casters', (t) => {
189+
const res = aqp('key=string(1)&key=string(2)');
190+
t.truthy(res);
191+
t.deepEqual(res.filter, { key: { $in: ['1', '2'] } });
192+
});
193+
188194
test('filter: $in operator (comma-separated)', (t) => {
189195
const res = aqp('key=a,b');
190196
t.truthy(res);
191197
t.deepEqual(res.filter, { key: { $in: ['a', 'b'] } });
192198
});
193199

200+
test('filter: $in operator (comma-separated), with casters', (t) => {
201+
const res = aqp('key=string(1),string(2)');
202+
t.truthy(res);
203+
t.deepEqual(res.filter, { key: { $in: ['1', '2'] } });
204+
});
205+
194206
test('filter: $in operator (comma-separated regexes)', (t) => {
195207
const res = aqp('key=/a/,/b/');
196208
t.truthy(res);

0 commit comments

Comments
 (0)