@@ -290,7 +290,7 @@ const defaultMethods = {
290
290
lazy : true
291
291
} ,
292
292
'??' : defineCoalesce ( ) ,
293
- try : defineCoalesce ( downgrade ) ,
293
+ try : defineCoalesce ( downgrade , true ) ,
294
294
and : {
295
295
[ Sync ] : ( data , buildState ) => isSyncDeep ( data , buildState . engine , buildState ) ,
296
296
method : ( arr , _1 , _2 , engine ) => {
@@ -715,7 +715,7 @@ const defaultMethods = {
715
715
/**
716
716
* Defines separate coalesce methods
717
717
*/
718
- function defineCoalesce ( func ) {
718
+ function defineCoalesce ( func , panic ) {
719
719
let downgrade
720
720
if ( func ) downgrade = func
721
721
else downgrade = ( a ) => a
@@ -734,6 +734,7 @@ function defineCoalesce (func) {
734
734
}
735
735
736
736
if ( item === undefined ) return null
737
+ if ( panic ) throw item
737
738
return item
738
739
} ,
739
740
asyncMethod : async ( arr , _1 , _2 , engine ) => {
@@ -748,6 +749,7 @@ function defineCoalesce (func) {
748
749
}
749
750
750
751
if ( item === undefined ) return null
752
+ if ( panic ) throw item
751
753
return item
752
754
} ,
753
755
deterministic : ( data , buildState ) => isDeterministic ( data , buildState . engine , buildState ) ,
@@ -756,8 +758,10 @@ function defineCoalesce (func) {
756
758
const funcCall = func ? 'downgrade' : ''
757
759
if ( Array . isArray ( data ) && data . length ) {
758
760
return `(${ data . map ( ( i , x ) => {
759
- if ( Array . isArray ( i ) || ! i || typeof i !== 'object' || x === data . length - 1 ) return buildString ( i , buildState )
760
- return `${ funcCall } (` + buildString ( i , buildState ) + ')'
761
+ const built = buildString ( i , buildState )
762
+ if ( panic && x === data . length - 1 ) return `(typeof ((prev = ${ built } ) || 0).error !== 'undefined' || Number.isNaN(prev) ? (() => { throw prev.error })() : prev)`
763
+ if ( Array . isArray ( i ) || ! i || typeof i !== 'object' || x === data . length - 1 ) return built
764
+ return `${ funcCall } (` + built + ')'
761
765
} ) . join ( ' ?? ' ) } )`
762
766
}
763
767
return `(${ buildString ( data , buildState ) } ).reduce((a,b) => ${ funcCall } (a) ?? b, null)`
0 commit comments