Skip to content

Commit

Permalink
Improve regexp to extract casters (fix #126)
Browse files Browse the repository at this point in the history
  • Loading branch information
loris committed Oct 26, 2021
1 parent bebf5b6 commit bc4f0cf
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ const builtInCasters = {
const parseValue = (value, key, options) => {
// Match type casting operators like string(true)
const casters = { ...builtInCasters, ...options.casters };
const casting = value.match(/^(\w+)\((.*)\)$/);
const castersList = Object.keys(casters).join('|');
const castersRegexp = new RegExp(`^(${castersList})\\(([^)]*)\\)$`);
const casting = value.match(castersRegexp);
if (casting && casters[casting[1]]) {
return casters[casting[1]](casting[2]);
}
Expand Down
12 changes: 12 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,12 +185,24 @@ test('filter: $in operator (multiple keys)', (t) => {
t.deepEqual(res.filter, { key: { $in: ['a', 'b'] } });
});

test('filter: $in operator (multiple keys), with casters', (t) => {
const res = aqp('key=string(1)&key=string(2)');
t.truthy(res);
t.deepEqual(res.filter, { key: { $in: ['1', '2'] } });
});

test('filter: $in operator (comma-separated)', (t) => {
const res = aqp('key=a,b');
t.truthy(res);
t.deepEqual(res.filter, { key: { $in: ['a', 'b'] } });
});

test('filter: $in operator (comma-separated), with casters', (t) => {
const res = aqp('key=string(1),string(2)');
t.truthy(res);
t.deepEqual(res.filter, { key: { $in: ['1', '2'] } });
});

test('filter: $in operator (comma-separated regexes)', (t) => {
const res = aqp('key=/a/,/b/');
t.truthy(res);
Expand Down

0 comments on commit bc4f0cf

Please sign in to comment.