Skip to content

Commit 74f4b03

Browse files
committed
Fix escaped characters with operators =in= and =out=
1 parent d88ea80 commit 74f4b03

File tree

3 files changed

+20
-8
lines changed

3 files changed

+20
-8
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": "2.2.0",
3+
"version": "2.2.1",
44
"description": "Converting RSQL queries to MongoDB queries",
55
"main": "rsql-mongodb.js",
66
"typings": "rsql-mongodb.ts",

rsql-mongodb.js

+16-6
Original file line numberDiff line numberDiff line change
@@ -138,8 +138,14 @@ module.exports = function (input) {
138138

139139
// if the parenthesis is value of a special operator then push it into 'outputString' buffer
140140
if(specialOperator){
141-
specialOperator = false;
142-
outputString += character;
141+
if(outputString[outputString.length - 1] == "\\"){
142+
outputString = outputString.substring(0, outputString.length - 1);
143+
outputString += character;
144+
}
145+
else{
146+
specialOperator = false;
147+
outputString += character;
148+
}
143149
}
144150
// Manage escape character
145151
else if(outputString[outputString.length - 1] == "\\"){
@@ -293,8 +299,10 @@ module.exports = function (input) {
293299
mongoOperatorQuery[exp1] = { $lte: typedExp2 };
294300
break;
295301
case "=in=":
296-
typedExp2 = typedExp2.replace("(","");
297-
typedExp2 = typedExp2.replace(")","");
302+
if(typedExp2[0] == "(")
303+
typedExp2 = typedExp2.slice(1);
304+
if(typedExp2[typedExp2.length -1] == ")")
305+
typedExp2 = typedExp2.slice(0, typedExp2.length -1);
298306
var typedValues = new Array();
299307
for ( var token of typedExp2.split(",") ) {
300308
var value = setType(token.trim());
@@ -305,8 +313,10 @@ module.exports = function (input) {
305313
mongoOperatorQuery[exp1] = { $in: typedValues };
306314
break;
307315
case "=out=":
308-
typedExp2 = typedExp2.replace("(","");
309-
typedExp2 = typedExp2.replace(")","");
316+
if(typedExp2[0] == "(")
317+
typedExp2 = typedExp2.slice(1);
318+
if(typedExp2[typedExp2.length -1] == ")")
319+
typedExp2 = typedExp2.slice(0, typedExp2.length -1);
310320
var typedValues = new Array();
311321
for ( var token of typedExp2.split(",") ) {
312322
var value = setType(token.trim());

test.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,14 @@ describe('rsql-mongodb', function () {
5757
expect(rsqlMongoDB('childs=in=(1,2,3)')).to.deep.include({ "childs": { $in: [1,2,3] } });
5858
expect(rsqlMongoDB('childs=in=("1","2","3")')).to.deep.include({ "childs": { $in: ["1","2","3"] } });
5959
expect(rsqlMongoDB('childs=in=(1, 2, 3 )')).to.deep.include({ "childs": { $in: [1,2,3] } });
60-
expect(rsqlMongoDB('_id=in=(650a7389a7ab39ddcfbc6832,650a7389a7ab39ddcfbc6833)')).to.deep.include({ "_id": { $in: [new ObjectId('650a7389a7ab39ddcfbc6832'),new ObjectId('650a7389a7ab39ddcfbc6833')] } });
60+
expect(rsqlMongoDB('_id=in=(650a7389a7ab39ddcfbc6832,650a7389a7ab39ddcfbc6833)')).to.deep.include({ "_id": { $in: [new ObjectId('650a7389a7ab39ddcfbc6832'),new ObjectId('650a7389a7ab39ddcfbc6833')] }});
61+
expect(rsqlMongoDB('invoice.userid=in=("XXFF11 \\(180kW\\)")')).to.deep.include({ "invoice.userid": { $in: ['XXFF11 (180kW)'] } });
6162
});
6263
it("Test operator Out ('=out=')", function () {
6364
expect(rsqlMongoDB('childs=out=(1,2,3)')).to.deep.include({ "childs": { $nin: [1,2,3] } });
6465
expect(rsqlMongoDB('childs=out=("1","2","3")')).to.deep.include({ "childs": { $nin: ["1","2","3"] } });
6566
expect(rsqlMongoDB('childs=out=(1, 2, 3 )')).to.deep.include({ "childs": { $nin: [1,2,3] } });
67+
expect(rsqlMongoDB('invoice.userid=out=("XXFF11 \\(180kW\\)")')).to.deep.include({ "invoice.userid": { $nin: ['XXFF11 (180kW)'] } });
6668
});
6769
it("Test operator Like ('=regex=')", function () {
6870
expect(rsqlMongoDB('lastName=regex=do*')).to.deep.include({ "lastName": { $regex: "do*", $options: "" } });

0 commit comments

Comments
 (0)