Skip to content

Commit 10c8123

Browse files
committed
Fix issue #6 with AND/OR operator and groups
1 parent 8072fea commit 10c8123

File tree

3 files changed

+33
-6
lines changed

3 files changed

+33
-6
lines changed

package.json

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

rsql-mongodb.js

+30-5
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,10 @@ module.exports = function (input) {
7777

7878
lastLogical = logicalsTab[logicalsTab.length - 1];
7979
}
80-
80+
8181
// Push the character into 'logicalsTab'
8282
logicalsTab.push(character);
83+
8384
}
8485

8586
}
@@ -93,7 +94,13 @@ module.exports = function (input) {
9394
}
9495
// Else push the character into the 'logicalsTab'
9596
else{
97+
98+
// Push all operator presents in 'logicalsTab' into 'outputTab'
99+
while(logicalsTab.length > 0) {
100+
outputTab.push(logicalsTab.pop());
101+
}
96102
logicalsTab.push(character);
103+
outputTab.push(character);
97104
}
98105

99106
}
@@ -120,6 +127,7 @@ module.exports = function (input) {
120127

121128
// Remove the open parenthesis from 'logicalsTab'
122129
logicalsTab.pop();
130+
outputTab.push(character);
123131
}
124132
}
125133
// If the character is not an operator push it into the 'outputString' buffer
@@ -148,7 +156,9 @@ module.exports = function (input) {
148156
// Define variables
149157
var mongoStack = [];
150158
var mongoQuery = [];
151-
159+
var tmpPrecedence = [];
160+
var lastLogical = "";
161+
var lastLogicalBeforePrecedence = ""
152162

153163
for(var i = 0; i < outputTab.length; i++) {
154164

@@ -160,7 +170,7 @@ module.exports = function (input) {
160170
switch(outputTab[i]){
161171
case ";":
162172
case ",":
163-
if(i == (outputTab.length -1) || (mongoStack.length == 1 && mongoQuery.length == 1)){
173+
if(i == (outputTab.length -1) || (mongoQuery.length == 1)){
164174
while(mongoQuery.length > 0) {
165175
tmpArray.push(mongoQuery.shift())
166176
}
@@ -169,10 +179,12 @@ module.exports = function (input) {
169179
tmpArray.push(mongoStack.shift())
170180
}
171181
if(outputTab[i] == ";"){
172-
newValue['$and'] = tmpArray;
182+
lastLogical = '$and';
183+
newValue[lastLogical] = tmpArray;
173184
}
174185
else{
175-
newValue['$or'] = tmpArray;
186+
lastLogical = '$or';
187+
newValue[lastLogical] = tmpArray;
176188
}
177189
break;
178190
default:
@@ -182,6 +194,19 @@ module.exports = function (input) {
182194
mongoQuery.push(newValue);
183195

184196
}
197+
else if( outputTab[i] == '('){
198+
tmpPrecedence = mongoQuery.shift();
199+
lastLogicalBeforePrecedence = lastLogical;
200+
}
201+
else if( outputTab[i] == ')'){
202+
if(tmpPrecedence){
203+
tmpPrecedence[lastLogicalBeforePrecedence].push(mongoQuery.shift());
204+
mongoQuery.push(tmpPrecedence);
205+
206+
}else{
207+
208+
}
209+
}
185210
else{
186211

187212
// Verify if the is no injections

test.js

+2
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ describe('rsql-mongodb', function () {
7676
it("Test other cases", function () {
7777
expect(rsqlMongoDB('firstName==john,firstName==janne,firstName==jim')).to.deep.include({$or:[{"firstName":"john"},{"firstName":"janne"},{"firstName":"jim"}]});
7878
expect(rsqlMongoDB('firstName==john,firstName==janne,firstName==jim;lastName==doe')).to.deep.include({$and:[{$or:[{"firstName":"john"},{"firstName":"janne"},{"firstName":"jim"}]},{"lastName":"doe"}]});
79+
expect(rsqlMongoDB('a==1;(b==2,c==3)')).to.deep.include({"$and":[{"a":1},{"$or":[{"b":2},{"c":3}]}]});
80+
expect(rsqlMongoDB('(b==2,c==3);a==1')).to.deep.include({"$and":[{"$or":[{"b":2},{"c":3}]},{"a":1}]});
7981
});
8082
it("Test errors", function () {
8183
expect(function () { rsqlMongoDB('azerty') }).to.throw('Wrong RSQL query. No operator found.');

0 commit comments

Comments
 (0)