@@ -77,9 +77,10 @@ module.exports = function (input) {
77
77
78
78
lastLogical = logicalsTab [ logicalsTab . length - 1 ] ;
79
79
}
80
-
80
+
81
81
// Push the character into 'logicalsTab'
82
82
logicalsTab . push ( character ) ;
83
+
83
84
}
84
85
85
86
}
@@ -93,7 +94,13 @@ module.exports = function (input) {
93
94
}
94
95
// Else push the character into the 'logicalsTab'
95
96
else {
97
+
98
+ // Push all operator presents in 'logicalsTab' into 'outputTab'
99
+ while ( logicalsTab . length > 0 ) {
100
+ outputTab . push ( logicalsTab . pop ( ) ) ;
101
+ }
96
102
logicalsTab . push ( character ) ;
103
+ outputTab . push ( character ) ;
97
104
}
98
105
99
106
}
@@ -120,6 +127,7 @@ module.exports = function (input) {
120
127
121
128
// Remove the open parenthesis from 'logicalsTab'
122
129
logicalsTab . pop ( ) ;
130
+ outputTab . push ( character ) ;
123
131
}
124
132
}
125
133
// If the character is not an operator push it into the 'outputString' buffer
@@ -148,7 +156,9 @@ module.exports = function (input) {
148
156
// Define variables
149
157
var mongoStack = [ ] ;
150
158
var mongoQuery = [ ] ;
151
-
159
+ var tmpPrecedence = [ ] ;
160
+ var lastLogical = "" ;
161
+ var lastLogicalBeforePrecedence = ""
152
162
153
163
for ( var i = 0 ; i < outputTab . length ; i ++ ) {
154
164
@@ -160,7 +170,7 @@ module.exports = function (input) {
160
170
switch ( outputTab [ i ] ) {
161
171
case ";" :
162
172
case "," :
163
- if ( i == ( outputTab . length - 1 ) || ( mongoStack . length == 1 && mongoQuery . length == 1 ) ) {
173
+ if ( i == ( outputTab . length - 1 ) || ( mongoQuery . length == 1 ) ) {
164
174
while ( mongoQuery . length > 0 ) {
165
175
tmpArray . push ( mongoQuery . shift ( ) )
166
176
}
@@ -169,10 +179,12 @@ module.exports = function (input) {
169
179
tmpArray . push ( mongoStack . shift ( ) )
170
180
}
171
181
if ( outputTab [ i ] == ";" ) {
172
- newValue [ '$and' ] = tmpArray ;
182
+ lastLogical = '$and' ;
183
+ newValue [ lastLogical ] = tmpArray ;
173
184
}
174
185
else {
175
- newValue [ '$or' ] = tmpArray ;
186
+ lastLogical = '$or' ;
187
+ newValue [ lastLogical ] = tmpArray ;
176
188
}
177
189
break ;
178
190
default :
@@ -182,6 +194,19 @@ module.exports = function (input) {
182
194
mongoQuery . push ( newValue ) ;
183
195
184
196
}
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
+ }
185
210
else {
186
211
187
212
// Verify if the is no injections
0 commit comments