@@ -29,7 +29,6 @@ var jsonata = (function() {
2929 var isArrayOfNumbers = utils . isArrayOfNumbers ;
3030 var createSequence = utils . createSequence ;
3131 var isSequence = utils . isSequence ;
32- var toSequence = utils . toSequence ;
3332 var isFunction = utils . isFunction ;
3433 var isLambda = utils . isLambda ;
3534 var isIterable = utils . isIterable ;
@@ -113,11 +112,11 @@ var jsonata = (function() {
113112 break ;
114113 }
115114
116- if ( environment . lookup ( '__jsonata_async' ) &&
115+ if ( environment . async &&
117116 ( typeof result === 'undefined' || result === null || typeof result . then !== 'function' ) ) {
118117 result = Promise . resolve ( result ) ;
119118 }
120- if ( environment . lookup ( '__jsonata_async' ) && typeof result . then === 'function' && expr . nextFunction && typeof result [ expr . nextFunction ] === 'function' ) {
119+ if ( environment . async && typeof result . then === 'function' && expr . nextFunction && typeof result [ expr . nextFunction ] === 'function' ) {
121120 // although this is a 'thenable', it is chaining a different function
122121 // so don't yield since yielding will trigger the .then()
123122 } else {
@@ -142,7 +141,12 @@ var jsonata = (function() {
142141 if ( expr . keepArray ) {
143142 result . keepSingleton = true ;
144143 }
145- result = result . value ( ) ;
144+ if ( result . length === 0 ) {
145+ result = undefined ;
146+ } else if ( result . length === 1 ) {
147+ result = result . keepSingleton ? result : result [ 0 ] ;
148+ }
149+
146150 }
147151
148152 return result ;
@@ -849,7 +853,8 @@ var jsonata = (function() {
849853 for ( var item = lhs , index = 0 ; item <= rhs ; item ++ , index ++ ) {
850854 result [ index ] = item ;
851855 }
852- return toSequence ( result ) ;
856+ result . sequence = true ;
857+ return result ;
853858 }
854859
855860 /**
@@ -1606,7 +1611,8 @@ var jsonata = (function() {
16061611 }
16071612 return value ;
16081613 } ,
1609- timestamp : enclosingEnvironment ? enclosingEnvironment . timestamp : null
1614+ timestamp : enclosingEnvironment ? enclosingEnvironment . timestamp : null ,
1615+ async : enclosingEnvironment ? enclosingEnvironment . async : false
16101616 } ;
16111617 }
16121618
@@ -1854,7 +1860,7 @@ var jsonata = (function() {
18541860 var result , it ;
18551861 // if a callback function is supplied, then drive the generator in a promise chain
18561862 if ( typeof callback === 'function' ) {
1857- exec_env . bind ( '__jsonata_async' , true ) ;
1863+ exec_env . async = true ;
18581864 var catchHandler = function ( err ) {
18591865 populateMessage ( err ) ; // possible side-effects on `err`
18601866 callback ( err , null ) ;
0 commit comments