Skip to content

Commit

Permalink
Merge pull request #5 from zishone/regex-options
Browse files Browse the repository at this point in the history
Add regex options
  • Loading branch information
Fizcko authored May 25, 2020
2 parents 08ae29d + 514c7b7 commit 8072fea
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 5 deletions.
8 changes: 7 additions & 1 deletion Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,13 @@ try{

// Like operator
rsqlMongoDB('lastName=~do*');
//=> { "lastName": { $regex: "do*" } }
//=> { "lastName": { $regex: "do*", $options: "" } }

// Like operator with options
rsqlMongoDB('lastName=~do*=si');
//=> { "lastName": { $regex: "do*", $options: "si" } }
rsqlMongoDB('lastName=~"do=*"=si');
//=> { "lastName": { $regex: "do=*", $options: "si" } }

// Exists operator
rsqlMongoDB('childs=exists=true');
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "rsql-mongodb",
"version": "1.3.0",
"version": "1.4.0",
"description": "Converting RSQL queries to MongoDB queries",
"main": "rsql-mongodb.js",
"typings": "rsql-mongodb.ts",
Expand Down
3 changes: 2 additions & 1 deletion rsql-mongodb.js
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,8 @@ module.exports = function (input) {
mongoOperatorQuery[exp1] = { $nin: typedValues };
break;
case "=~":
mongoOperatorQuery[exp1] = { $regex: typedExp2 };
var expArr = exp2.split(/(=)(?=(?:[^"]|"[^"]*")*$)/g);
mongoOperatorQuery[exp1] = { $regex: setType(expArr[0]), $options: expArr[2] || "" };
break;
case "=exists=":
mongoOperatorQuery[exp1] = { $exists: typedExp2 };
Expand Down
6 changes: 4 additions & 2 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,15 @@ describe('rsql-mongodb', function () {
expect(rsqlMongoDB('childs=out=(1, 2, 3 )')).to.deep.include({ "childs": { $nin: [1,2,3] } });
});
it("Test operator Like ('=~')", function () {
expect(rsqlMongoDB('lastName=~do*')).to.deep.include({ "lastName": { $regex: "do*" } });
expect(rsqlMongoDB('lastName=~do*')).to.deep.include({ "lastName": { $regex: "do*", $options: "" } });
expect(rsqlMongoDB('lastName=~do*=i')).to.deep.include({ "lastName": { $regex: "do*", $options: "i" } });
expect(rsqlMongoDB('lastName=~do*=mxs')).to.deep.include({ "lastName": { $regex: "do*", $options: "mxs" } });
expect(rsqlMongoDB('lastName=~"do=*"=mxs')).to.deep.include({ "lastName": { $regex: "do=*", $options: "mxs" } });
});
it("Test operator Exists ('=exists=')", function () {
expect(rsqlMongoDB('childs=exists=true')).to.deep.include({ "childs": { $exists: true } });
expect(rsqlMongoDB('childs=exists=false')).to.deep.include({ "childs": { $exists: false } });
expect(rsqlMongoDB('childs=exists=true')).to.be.a('object');

});
it("Test logical operator AND (';')", function () {
expect(rsqlMongoDB('firstName=="john";lastName=="doe"')).to.deep.include({ $and: [ { "firstName" : "john" } , { "lastName" : "doe" } ] });
Expand Down

0 comments on commit 8072fea

Please sign in to comment.