@@ -212,6 +212,7 @@ const defaultMethods = {
212
212
// Why "executeInLoop"? Because if it needs to execute to get an array, I do not want to execute the arguments,
213
213
// Both for performance and safety reasons.
214
214
'??' : {
215
+ [ Sync ] : ( data , buildState ) => isSyncDeep ( data , buildState . engine , buildState ) ,
215
216
method : ( arr , _1 , _2 , engine ) => {
216
217
// See "executeInLoop" above
217
218
const executeInLoop = Array . isArray ( arr )
@@ -249,6 +250,7 @@ const defaultMethods = {
249
250
traverse : false
250
251
} ,
251
252
or : {
253
+ [ Sync ] : ( data , buildState ) => isSyncDeep ( data , buildState . engine , buildState ) ,
252
254
method : ( arr , _1 , _2 , engine ) => {
253
255
// See "executeInLoop" above
254
256
const executeInLoop = Array . isArray ( arr )
@@ -284,6 +286,7 @@ const defaultMethods = {
284
286
traverse : false
285
287
} ,
286
288
and : {
289
+ [ Sync ] : ( data , buildState ) => isSyncDeep ( data , buildState . engine , buildState ) ,
287
290
method : ( arr , _1 , _2 , engine ) => {
288
291
// See "executeInLoop" above
289
292
const executeInLoop = Array . isArray ( arr )
@@ -419,6 +422,7 @@ const defaultMethods = {
419
422
some : createArrayIterativeMethod ( 'some' , true ) ,
420
423
all : createArrayIterativeMethod ( 'every' , true ) ,
421
424
none : {
425
+ [ Sync ] : ( data , buildState ) => isSyncDeep ( data , buildState . engine , buildState ) ,
422
426
traverse : false ,
423
427
// todo: add async build & build
424
428
method : ( val , context , above , engine ) => {
@@ -439,7 +443,7 @@ const defaultMethods = {
439
443
} ,
440
444
merge : ( arrays ) => ( Array . isArray ( arrays ) ? [ ] . concat ( ...arrays ) : [ arrays ] ) ,
441
445
every : createArrayIterativeMethod ( 'every' ) ,
442
- filter : createArrayIterativeMethod ( 'filter' ) ,
446
+ filter : createArrayIterativeMethod ( 'filter' , true ) ,
443
447
reduce : {
444
448
deterministic : ( data , buildState ) => {
445
449
return (
@@ -548,11 +552,27 @@ const defaultMethods = {
548
552
} ,
549
553
'!' : ( value , _1 , _2 , engine ) => Array . isArray ( value ) ? ! engine . truthy ( value [ 0 ] ) : ! engine . truthy ( value ) ,
550
554
'!!' : ( value , _1 , _2 , engine ) => Boolean ( Array . isArray ( value ) ? engine . truthy ( value [ 0 ] ) : engine . truthy ( value ) ) ,
551
- cat : ( arr ) => {
552
- if ( typeof arr === 'string' ) return arr
553
- let res = ''
554
- for ( let i = 0 ; i < arr . length ; i ++ ) res += arr [ i ]
555
- return res
555
+ cat : {
556
+ [ OriginalImpl ] : true ,
557
+ [ Sync ] : true ,
558
+ method : ( arr ) => {
559
+ if ( typeof arr === 'string' ) return arr
560
+ if ( ! Array . isArray ( arr ) ) return arr . toString ( )
561
+ let res = ''
562
+ for ( let i = 0 ; i < arr . length ; i ++ ) res += arr [ i ] . toString ( )
563
+ return res
564
+ } ,
565
+ deterministic : true ,
566
+ traverse : true ,
567
+ optimizeUnary : true ,
568
+ compile : ( data , buildState ) => {
569
+ if ( typeof data === 'string' ) return JSON . stringify ( data )
570
+ if ( typeof data === 'number' ) return '"' + JSON . stringify ( data ) + '"'
571
+ if ( ! Array . isArray ( data ) ) return false
572
+ let res = buildState . compile `''`
573
+ for ( let i = 0 ; i < data . length ; i ++ ) res = buildState . compile `${ res } + ${ data [ i ] } `
574
+ return buildState . compile `(${ res } )`
575
+ }
556
576
} ,
557
577
keys : ( [ obj ] ) => typeof obj === 'object' ? Object . keys ( obj ) : [ ] ,
558
578
pipe : {
@@ -673,8 +693,8 @@ function createArrayIterativeMethod (name, useTruthy = false) {
673
693
( await engine . run ( selector , context , {
674
694
above
675
695
} ) ) || [ ]
676
- return asyncIterators [ name ] ( selector , ( i , index ) => {
677
- const result = engine . run ( mapper , i , {
696
+ return asyncIterators [ name ] ( selector , async ( i , index ) => {
697
+ const result = await engine . run ( mapper , i , {
678
698
above : [ { iterator : selector , index } , context , above ]
679
699
} )
680
700
return useTruthy ? engine . truthy ( result ) : result
@@ -694,15 +714,16 @@ function createArrayIterativeMethod (name, useTruthy = false) {
694
714
695
715
const method = build ( mapper , mapState )
696
716
const aboveArray = method . aboveDetected ? buildState . compile `[{ iterator: z, index: x }, context, above]` : buildState . compile `null`
717
+ const useTruthyMethod = useTruthy ? buildState . compile `engine.truthy` : buildState . compile ``
697
718
698
719
if ( async ) {
699
720
if ( ! isSync ( method ) ) {
700
721
buildState . asyncDetected = true
701
- return buildState . compile `await asyncIterators[${ name } ](${ selector } || [], async (i, x, z) => ${ method } (i, x, ${ aboveArray } ))`
722
+ return buildState . compile `await asyncIterators[${ name } ](${ selector } || [], async (i, x, z) => ${ useTruthyMethod } ( ${ method } (i, x, ${ aboveArray } ) ))`
702
723
}
703
724
}
704
725
705
- return buildState . compile `(${ selector } || [])[${ name } ]((i, x, z) => ${ method } (i, x, ${ aboveArray } ))`
726
+ return buildState . compile `(${ selector } || [])[${ name } ]((i, x, z) => ${ useTruthyMethod } ( ${ method } (i, x, ${ aboveArray } ) ))`
706
727
} ,
707
728
traverse : false
708
729
}
0 commit comments