Skip to content

Commit 69b9f2f

Browse files
authored
Merge pull request #3 from Fizcko/develop
Add like operator. Resolve #2
2 parents e2fb285 + 8072085 commit 69b9f2f

File tree

4 files changed

+17
-4
lines changed

4 files changed

+17
-4
lines changed

Readme.md

+7
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ It's a query language that introduces basic and logical operators. This is perfe
3131
- AND : ;
3232
- OR : ,
3333

34+
#### Additionals operators
35+
- Like : =~ (to match regex values)
36+
3437
###### NOTE
3538
Parenthesized expression can be used to define the precedence.
3639

@@ -72,6 +75,10 @@ try{
7275
rsqlMongoDB('childs=out=(1,2,3)');
7376
//=> { "childs": { $nin: [1,2,3] } }
7477

78+
// Like operator
79+
rsqlMongoDB('lastName=~do*');
80+
//=> { "lastName": { $regex: "do*" } }
81+
7582
// Groups
7683
rsqlMongoDB('(firstName=="john";lastName=="doe"),(firstName==janne;lastName==doe)');
7784
//=> { $or: [ { $and: [ { "firstName" : "john" } , { "lastName" : "doe" } ] } , { $and: [ { "firstName" : "janne" } , { "lastName" : "doe" } ] } ] }

package.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "rsql-mongodb",
3-
"version": "1.1.6",
3+
"version": "1.2.0",
44
"description": "Converting RSQL queries to MongoDB queries",
55
"main": "rsql-mongodb.js",
66
"typings": "rsql-mongodb.ts",
@@ -36,8 +36,8 @@
3636
],
3737
"devDependencies": {
3838
"chai": "4.2.0",
39-
"coveralls": "3.0.4",
40-
"mocha": "6.1.4",
39+
"coveralls": "3.0.5",
40+
"mocha": "6.2.0",
4141
"nyc": "14.1.1"
4242
}
4343
}

rsql-mongodb.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ module.exports = function (input) {
193193
}
194194

195195
// Split the query
196-
var rsqlOperators = /(.*)(==|!=|=gt=|=ge=|=lt=|=le=|=in=|=out=)(.*)/g;
196+
var rsqlOperators = /(.*)(==|!=|=gt=|=ge=|=lt=|=le=|=in=|=out=|=~)(.*)/g;
197197
var rsqlQuery = rsqlOperators.exec(outputTab[i]);
198198

199199
try {
@@ -247,6 +247,9 @@ module.exports = function (input) {
247247
}
248248
mongoOperatorQuery[exp1] = { $nin: typedValues };
249249
break;
250+
case "=~":
251+
mongoOperatorQuery[exp1] = { $regex: typedExp2 };
252+
break;
250253
default:
251254
throw "Operator not supported."
252255
}

test.js

+3
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ describe('rsql-mongodb', function () {
5252
expect(rsqlMongoDB('childs=out=("1","2","3")')).to.deep.include({ "childs": { $nin: ["1","2","3"] } });
5353
expect(rsqlMongoDB('childs=out=(1, 2, 3 )')).to.deep.include({ "childs": { $nin: [1,2,3] } });
5454
});
55+
it("Test operator Like ('=~')", function () {
56+
expect(rsqlMongoDB('lastName=~do*')).to.deep.include({ "lastName": { $regex: "do*" } });
57+
});
5558
it("Test logical operator AND (';')", function () {
5659
expect(rsqlMongoDB('firstName=="john";lastName=="doe"')).to.deep.include({ $and: [ { "firstName" : "john" } , { "lastName" : "doe" } ] });
5760
});

0 commit comments

Comments
 (0)