@@ -159,17 +159,81 @@ const defaultMethods = {
159
159
'!=' : ( [ a , b ] ) => a != b ,
160
160
'!==' : ( [ a , b ] ) => a !== b ,
161
161
xor : ( [ a , b ] ) => a ^ b ,
162
- or : ( arr , _1 , _2 , engine ) => {
163
- for ( let i = 0 ; i < arr . length ; i ++ ) {
164
- if ( engine . truthy ( arr [ i ] ) ) return arr [ i ]
165
- }
166
- return arr [ arr . length - 1 ]
162
+ // Why "executeInLoop"? Because if it needs to execute to get an array, I do not want to execute the arguments,
163
+ // Both for performance and safety reasons.
164
+ or : {
165
+ method : ( arr , _1 , _2 , engine ) => {
166
+ // See "executeInLoop" above
167
+ const executeInLoop = Array . isArray ( arr )
168
+ if ( ! executeInLoop ) arr = engine . run ( arr , _1 , { above : _2 } )
169
+
170
+ let item
171
+ for ( let i = 0 ; i < arr . length ; i ++ ) {
172
+ item = executeInLoop ? engine . run ( arr [ i ] , _1 , { above : _2 } ) : arr [ i ]
173
+ if ( engine . truthy ( item ) ) return item
174
+ }
175
+
176
+ return item
177
+ } ,
178
+ asyncMethod : async ( arr , _1 , _2 , engine ) => {
179
+ // See "executeInLoop" above
180
+ const executeInLoop = Array . isArray ( arr )
181
+ if ( ! executeInLoop ) arr = await engine . run ( arr , _1 , { above : _2 } )
182
+
183
+ let item
184
+ for ( let i = 0 ; i < arr . length ; i ++ ) {
185
+ item = executeInLoop ? await engine . run ( arr [ i ] , _1 , { above : _2 } ) : arr [ i ]
186
+ if ( engine . truthy ( item ) ) return item
187
+ }
188
+
189
+ return item
190
+ } ,
191
+ deterministic : ( data , buildState ) => isDeterministic ( data , buildState . engine , buildState ) ,
192
+ compile : ( data , buildState ) => {
193
+ if ( ! buildState . engine . truthy . IDENTITY ) return false
194
+ if ( Array . isArray ( data ) ) {
195
+ return `(${ data . map ( ( i ) => buildString ( i , buildState ) ) . join ( ' || ' ) } )`
196
+ } else {
197
+ return `(${ buildString ( data , buildState ) } ).reduce((a,b) => a||b, false)`
198
+ }
199
+ } ,
200
+ traverse : false
167
201
} ,
168
- and : ( arr , _1 , _2 , engine ) => {
169
- for ( let i = 0 ; i < arr . length ; i ++ ) {
170
- if ( ! engine . truthy ( arr [ i ] ) ) return arr [ i ]
202
+ and : {
203
+ method : ( arr , _1 , _2 , engine ) => {
204
+ // See "executeInLoop" above
205
+ const executeInLoop = Array . isArray ( arr )
206
+ if ( ! executeInLoop ) arr = engine . run ( arr , _1 , { above : _2 } )
207
+
208
+ let item
209
+ for ( let i = 0 ; i < arr . length ; i ++ ) {
210
+ item = executeInLoop ? engine . run ( arr [ i ] , _1 , { above : _2 } ) : arr [ i ]
211
+ if ( ! engine . truthy ( item ) ) return item
212
+ }
213
+ return item
214
+ } ,
215
+ asyncMethod : async ( arr , _1 , _2 , engine ) => {
216
+ // See "executeInLoop" above
217
+ const executeInLoop = Array . isArray ( arr )
218
+ if ( ! executeInLoop ) arr = await engine . run ( arr , _1 , { above : _2 } )
219
+
220
+ let item
221
+ for ( let i = 0 ; i < arr . length ; i ++ ) {
222
+ item = executeInLoop ? await engine . run ( arr [ i ] , _1 , { above : _2 } ) : arr [ i ]
223
+ if ( ! engine . truthy ( item ) ) return item
224
+ }
225
+ return item
226
+ } ,
227
+ traverse : false ,
228
+ deterministic : ( data , buildState ) => isDeterministic ( data , buildState . engine , buildState ) ,
229
+ compile : ( data , buildState ) => {
230
+ if ( ! buildState . engine . truthy . IDENTITY ) return false
231
+ if ( Array . isArray ( data ) ) {
232
+ return `(${ data . map ( ( i ) => buildString ( i , buildState ) ) . join ( ' && ' ) } )`
233
+ } else {
234
+ return `(${ buildString ( data , buildState ) } ).reduce((a,b) => a&&b, true)`
235
+ }
171
236
}
172
- return arr [ arr . length - 1 ]
173
237
} ,
174
238
substr : ( [ string , from , end ] ) => {
175
239
if ( end < 0 ) {
@@ -677,32 +741,12 @@ defaultMethods['%'].compile = function (data, buildState) {
677
741
}
678
742
}
679
743
680
- // @ts -ignore Allow custom attribute
681
- defaultMethods . or . compile = function ( data , buildState ) {
682
- if ( ! buildState . engine . truthy . IDENTITY ) return false
683
- if ( Array . isArray ( data ) ) {
684
- return `(${ data . map ( ( i ) => buildString ( i , buildState ) ) . join ( ' || ' ) } )`
685
- } else {
686
- return `(${ buildString ( data , buildState ) } ).reduce((a,b) => a||b, false)`
687
- }
688
- }
689
-
690
744
// @ts -ignore Allow custom attribute
691
745
defaultMethods . in . compile = function ( data , buildState ) {
692
746
if ( ! Array . isArray ( data ) ) return false
693
747
return buildState . compile `(${ data [ 1 ] } || []).includes(${ data [ 0 ] } )`
694
748
}
695
749
696
- // @ts -ignore Allow custom attribute
697
- defaultMethods . and . compile = function ( data , buildState ) {
698
- if ( ! buildState . engine . truthy . IDENTITY ) return false
699
- if ( Array . isArray ( data ) ) {
700
- return `(${ data . map ( ( i ) => buildString ( i , buildState ) ) . join ( ' && ' ) } )`
701
- } else {
702
- return `(${ buildString ( data , buildState ) } ).reduce((a,b) => a&&b, true)`
703
- }
704
- }
705
-
706
750
// @ts -ignore Allow custom attribute
707
751
defaultMethods [ '-' ] . compile = function ( data , buildState ) {
708
752
if ( Array . isArray ( data ) ) {
0 commit comments