Skip to content

Commit 8072fea

Browse files
authored
Merge pull request #5 from zishone/regex-options
Add regex options
2 parents 08ae29d + 514c7b7 commit 8072fea

File tree

4 files changed

+14
-5
lines changed

4 files changed

+14
-5
lines changed

Readme.md

+7-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,13 @@ try{
7878

7979
// Like operator
8080
rsqlMongoDB('lastName=~do*');
81-
//=> { "lastName": { $regex: "do*" } }
81+
//=> { "lastName": { $regex: "do*", $options: "" } }
82+
83+
// Like operator with options
84+
rsqlMongoDB('lastName=~do*=si');
85+
//=> { "lastName": { $regex: "do*", $options: "si" } }
86+
rsqlMongoDB('lastName=~"do=*"=si');
87+
//=> { "lastName": { $regex: "do=*", $options: "si" } }
8288

8389
// Exists operator
8490
rsqlMongoDB('childs=exists=true');

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "rsql-mongodb",
3-
"version": "1.3.0",
3+
"version": "1.4.0",
44
"description": "Converting RSQL queries to MongoDB queries",
55
"main": "rsql-mongodb.js",
66
"typings": "rsql-mongodb.ts",

rsql-mongodb.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,8 @@ module.exports = function (input) {
248248
mongoOperatorQuery[exp1] = { $nin: typedValues };
249249
break;
250250
case "=~":
251-
mongoOperatorQuery[exp1] = { $regex: typedExp2 };
251+
var expArr = exp2.split(/(=)(?=(?:[^"]|"[^"]*")*$)/g);
252+
mongoOperatorQuery[exp1] = { $regex: setType(expArr[0]), $options: expArr[2] || "" };
252253
break;
253254
case "=exists=":
254255
mongoOperatorQuery[exp1] = { $exists: typedExp2 };

test.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,15 @@ describe('rsql-mongodb', function () {
5353
expect(rsqlMongoDB('childs=out=(1, 2, 3 )')).to.deep.include({ "childs": { $nin: [1,2,3] } });
5454
});
5555
it("Test operator Like ('=~')", function () {
56-
expect(rsqlMongoDB('lastName=~do*')).to.deep.include({ "lastName": { $regex: "do*" } });
56+
expect(rsqlMongoDB('lastName=~do*')).to.deep.include({ "lastName": { $regex: "do*", $options: "" } });
57+
expect(rsqlMongoDB('lastName=~do*=i')).to.deep.include({ "lastName": { $regex: "do*", $options: "i" } });
58+
expect(rsqlMongoDB('lastName=~do*=mxs')).to.deep.include({ "lastName": { $regex: "do*", $options: "mxs" } });
59+
expect(rsqlMongoDB('lastName=~"do=*"=mxs')).to.deep.include({ "lastName": { $regex: "do=*", $options: "mxs" } });
5760
});
5861
it("Test operator Exists ('=exists=')", function () {
5962
expect(rsqlMongoDB('childs=exists=true')).to.deep.include({ "childs": { $exists: true } });
6063
expect(rsqlMongoDB('childs=exists=false')).to.deep.include({ "childs": { $exists: false } });
6164
expect(rsqlMongoDB('childs=exists=true')).to.be.a('object');
62-
6365
});
6466
it("Test logical operator AND (';')", function () {
6567
expect(rsqlMongoDB('firstName=="john";lastName=="doe"')).to.deep.include({ $and: [ { "firstName" : "john" } , { "lastName" : "doe" } ] });

0 commit comments

Comments
 (0)