Skip to content

Commit 2e5e0a4

Browse files
committed
feat: add support for regexp flags in matches (rql)
1 parent 07e8b35 commit 2e5e0a4

File tree

2 files changed

+24
-20
lines changed

2 files changed

+24
-20
lines changed

src/mongo/rql.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import * as _ from 'lodash';
22

33
const rqlParser = require('rql/parser').parseQuery;
44

5-
export default function(query: any, opts: any, data: string) {
5+
export default function (query: any, opts: any, data: string) {
66
return _rqlToMongo(query, opts, rqlParser(data));
77
}
88

@@ -19,15 +19,15 @@ function _rqlToMongo(query: any, opts: any, data: any) {
1919
query = _rqlToMongo(query, opts, data.args[0]);
2020
} else if (_.find(data.args, (i: any) => i.name === 'or' || i.name === 'and')) {
2121
query.$and = [];
22-
_.each(data.args, function(i) {
22+
_.each(data.args, function (i) {
2323
let _p = _rqlToMongo({}, opts, i);
2424
if (_p) query.$and.push(_p);
2525
});
2626
if (query.$and.length === 1) {
2727
query = query.$and[0];
2828
}
2929
} else {
30-
_.each(data.args, function(i) {
30+
_.each(data.args, function (i) {
3131
query = _rqlToMongo(query, opts, i);
3232
});
3333
}
@@ -37,7 +37,7 @@ function _rqlToMongo(query: any, opts: any, data: any) {
3737
query = _rqlToMongo(query, opts, data.args[0]);
3838
} else {
3939
query.$or = [];
40-
_.each(data.args, function(i) {
40+
_.each(data.args, function (i) {
4141
let _p = _rqlToMongo({}, opts, i);
4242
if (_p) query.$or.push(_p);
4343
});
@@ -65,7 +65,7 @@ function _rqlToMongo(query: any, opts: any, data: any) {
6565
query[data.args[0]] = { $ne: data.args[1] };
6666
break;
6767
case 'matches':
68-
query[data.args[0]] = new RegExp(data.args[1]);
68+
query[data.args[0]] = new RegExp(data.args[1], data.args[2]);
6969
break;
7070

7171
case 'sort':

test/ts/rql.test.ts

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,21 @@ import rql from '../../dist/mongo/rql';
55
chai.should();
66
chai.use(spies);
77

8-
describe('rql', function() {
9-
it('should parse "in"', function() {
8+
describe('rql', function () {
9+
it('should parse "in"', function () {
1010
let query = rql({}, {}, 'in(x,a,b,c)');
1111
query.x.should.deep.equal({ $in: ['a', 'b', 'c'] });
1212
});
1313

14-
it('should parse "contains"', function() {
14+
it('should parse "contains"', function () {
1515
let query = rql({}, {}, 'contains(x,aaa)');
1616
query.x.should.equal('aaa');
1717

1818
query = rql({}, {}, 'contains(x,aaa,bbb,ccc)');
1919
query.x.should.deep.equal(['aaa', 'bbb', 'ccc']);
2020
});
2121

22-
it('should parse "and"', function() {
22+
it('should parse "and"', function () {
2323
let query1 = rql({}, {}, 'and(eq(x,b))');
2424
let query2 = rql({}, {}, 'eq(x,b)');
2525
query1.should.deep.equal(query2);
@@ -32,7 +32,7 @@ describe('rql', function() {
3232
query5.should.deep.equal({ x: 'b' });
3333
});
3434

35-
it('should parse "or"', function() {
35+
it('should parse "or"', function () {
3636
let query1 = rql({}, {}, 'or(eq(x,b))');
3737
let query2 = rql({}, {}, 'eq(x,b)');
3838
query1.should.deep.equal(query2);
@@ -45,54 +45,58 @@ describe('rql', function() {
4545
query5.should.deep.equal({ x: 'b' });
4646
});
4747

48-
it('should parse "eq"', function() {
48+
it('should parse "eq"', function () {
4949
let query = rql({}, {}, 'eq(x,a)');
5050
query.should.deep.equal({ x: 'a' });
5151
});
5252

53-
it('should parse "lt"', function() {
53+
it('should parse "lt"', function () {
5454
let query = rql({}, {}, 'lt(x,a)');
5555
query.should.deep.equal({ x: { $lt: 'a' } });
5656
});
5757

58-
it('should parse "le"', function() {
58+
it('should parse "le"', function () {
5959
let query = rql({}, {}, 'le(x,a)');
6060
query.should.deep.equal({ x: { $lte: 'a' } });
6161
});
6262

63-
it('should parse "gt"', function() {
63+
it('should parse "gt"', function () {
6464
let query = rql({}, {}, 'gt(x,a)');
6565
query.should.deep.equal({ x: { $gt: 'a' } });
6666
});
6767

68-
it('should parse "ge"', function() {
68+
it('should parse "ge"', function () {
6969
let query = rql({}, {}, 'ge(x,a)');
7070
query.should.deep.equal({ x: { $gte: 'a' } });
7171
});
7272

73-
it('should parse "ne"', function() {
73+
it('should parse "ne"', function () {
7474
let query = rql({}, {}, 'ne(x,a)');
7575
query.should.deep.equal({ x: { $ne: 'a' } });
7676
});
7777

78-
it('should parse "matches"', function() {
78+
it('should parse "matches"', function () {
7979
let query = rql({}, {}, 'matches(x,a)');
8080
query.should.deep.equal({ x: /a/ });
81+
query = rql({}, {}, 'matches(x,a,i)');
82+
query.should.deep.equal({ x: /a/i });
83+
query = rql({}, {}, 'matches(x,a,ig)');
84+
query.should.deep.equal({ x: /a/gi });
8185
});
8286

83-
it('should parse "sort"', function() {
87+
it('should parse "sort"', function () {
8488
let opts: any = {};
8589
rql({}, opts, 'sort(a,b)');
8690
opts.sort.should.deep.equal(['a', 'b']);
8791
});
8892

89-
it('should parse "select"', function() {
93+
it('should parse "select"', function () {
9094
let opts: any = {};
9195
rql({}, opts, 'select(a,b)');
9296
opts.fields.should.deep.equal(['a', 'b']);
9397
});
9498

95-
it('should parse "limit"', function() {
99+
it('should parse "limit"', function () {
96100
let opts: any = {};
97101
rql({}, opts, 'limit(a,b)');
98102
opts.skip.should.equal('a');

0 commit comments

Comments
 (0)