Skip to content

Commit

Permalink
Fix escaped characters with operators =in= and =out=
Browse files Browse the repository at this point in the history
  • Loading branch information
Fizcko committed May 21, 2024
1 parent d88ea80 commit 74f4b03
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 8 deletions.
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": "2.2.0",
"version": "2.2.1",
"description": "Converting RSQL queries to MongoDB queries",
"main": "rsql-mongodb.js",
"typings": "rsql-mongodb.ts",
Expand Down
22 changes: 16 additions & 6 deletions rsql-mongodb.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,14 @@ module.exports = function (input) {

// if the parenthesis is value of a special operator then push it into 'outputString' buffer
if(specialOperator){
specialOperator = false;
outputString += character;
if(outputString[outputString.length - 1] == "\\"){
outputString = outputString.substring(0, outputString.length - 1);
outputString += character;
}
else{
specialOperator = false;
outputString += character;
}
}
// Manage escape character
else if(outputString[outputString.length - 1] == "\\"){
Expand Down Expand Up @@ -293,8 +299,10 @@ module.exports = function (input) {
mongoOperatorQuery[exp1] = { $lte: typedExp2 };
break;
case "=in=":
typedExp2 = typedExp2.replace("(","");
typedExp2 = typedExp2.replace(")","");
if(typedExp2[0] == "(")
typedExp2 = typedExp2.slice(1);
if(typedExp2[typedExp2.length -1] == ")")
typedExp2 = typedExp2.slice(0, typedExp2.length -1);
var typedValues = new Array();
for ( var token of typedExp2.split(",") ) {
var value = setType(token.trim());
Expand All @@ -305,8 +313,10 @@ module.exports = function (input) {
mongoOperatorQuery[exp1] = { $in: typedValues };
break;
case "=out=":
typedExp2 = typedExp2.replace("(","");
typedExp2 = typedExp2.replace(")","");
if(typedExp2[0] == "(")
typedExp2 = typedExp2.slice(1);
if(typedExp2[typedExp2.length -1] == ")")
typedExp2 = typedExp2.slice(0, typedExp2.length -1);
var typedValues = new Array();
for ( var token of typedExp2.split(",") ) {
var value = setType(token.trim());
Expand Down
4 changes: 3 additions & 1 deletion test.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,14 @@ describe('rsql-mongodb', function () {
expect(rsqlMongoDB('childs=in=(1,2,3)')).to.deep.include({ "childs": { $in: [1,2,3] } });
expect(rsqlMongoDB('childs=in=("1","2","3")')).to.deep.include({ "childs": { $in: ["1","2","3"] } });
expect(rsqlMongoDB('childs=in=(1, 2, 3 )')).to.deep.include({ "childs": { $in: [1,2,3] } });
expect(rsqlMongoDB('_id=in=(650a7389a7ab39ddcfbc6832,650a7389a7ab39ddcfbc6833)')).to.deep.include({ "_id": { $in: [new ObjectId('650a7389a7ab39ddcfbc6832'),new ObjectId('650a7389a7ab39ddcfbc6833')] } });
expect(rsqlMongoDB('_id=in=(650a7389a7ab39ddcfbc6832,650a7389a7ab39ddcfbc6833)')).to.deep.include({ "_id": { $in: [new ObjectId('650a7389a7ab39ddcfbc6832'),new ObjectId('650a7389a7ab39ddcfbc6833')] }});
expect(rsqlMongoDB('invoice.userid=in=("XXFF11 \\(180kW\\)")')).to.deep.include({ "invoice.userid": { $in: ['XXFF11 (180kW)'] } });
});
it("Test operator Out ('=out=')", function () {
expect(rsqlMongoDB('childs=out=(1,2,3)')).to.deep.include({ "childs": { $nin: [1,2,3] } });
expect(rsqlMongoDB('childs=out=("1","2","3")')).to.deep.include({ "childs": { $nin: ["1","2","3"] } });
expect(rsqlMongoDB('childs=out=(1, 2, 3 )')).to.deep.include({ "childs": { $nin: [1,2,3] } });
expect(rsqlMongoDB('invoice.userid=out=("XXFF11 \\(180kW\\)")')).to.deep.include({ "invoice.userid": { $nin: ['XXFF11 (180kW)'] } });
});
it("Test operator Like ('=regex=')", function () {
expect(rsqlMongoDB('lastName=regex=do*')).to.deep.include({ "lastName": { $regex: "do*", $options: "" } });
Expand Down

0 comments on commit 74f4b03

Please sign in to comment.