@@ -377,6 +377,39 @@ test.each([
377377 expect ( result ) . toBe ( JSON . stringify ( array . sort ( compareFn ) ) ) ;
378378} ) ;
379379
380+ test . each ( [
381+ { array : [ 1 , [ 2 , 3 ] , 4 ] , expected : [ 1 , 2 , 3 , 4 ] } ,
382+ { array : [ 1 , [ 2 , 3 ] , 4 ] , depth : 0 , expected : [ 1 , [ 2 , 3 ] , 4 ] } ,
383+ { array : [ 1 , [ [ 2 ] , [ 3 ] ] , 4 ] , expected : [ 1 , [ 2 ] , [ 3 ] , 4 ] } ,
384+ { array : [ 1 , [ [ [ 2 ] , [ 3 ] ] ] , 4 ] , depth : Infinity , expected : [ 1 , 2 , 3 , 4 ] } ,
385+ ] ) ( "array.flat (%p)" , ( { array, depth, expected } ) => {
386+ // TODO: Remove once `Infinity` would be implemented
387+ const luaDepth = depth === Infinity ? "1 / 0" : depth ;
388+ const result = util . transpileAndExecute ( `
389+ return JSONStringify(${ JSON . stringify ( array ) } .flat(${ luaDepth } ))
390+ ` ) ;
391+
392+ expect ( JSON . parse ( result ) ) . toEqual ( expected ) ;
393+ } ) ;
394+
395+ test . each ( [
396+ { array : [ 1 , [ 2 , 3 ] , [ 4 ] ] , map : < T > ( value : T ) => value } ,
397+ { array : [ 1 , 2 , 3 ] , map : ( v : number ) => v * 2 } ,
398+ { array : [ 1 , 2 , 3 ] , map : ( v : number ) => [ v , v * 2 ] } ,
399+ { array : [ 1 , 2 , 3 ] , map : ( v : number ) => [ v , [ v ] ] } ,
400+ { array : [ 1 , 2 , 3 ] , map : ( v : number , i : number ) => [ v * 2 * i ] } ,
401+ ] ) ( "array.flatMap (%p)" , ( { array, map } ) => {
402+ const result = util . transpileAndExecute ( `
403+ const array = ${ JSON . stringify ( array ) } ;
404+ const result = array.flatMap(${ map . toString ( ) } );
405+ return JSONStringify(result);
406+ ` ) ;
407+
408+ // TODO(node 12): array.flatMap(map)
409+ const expected = [ ] . concat ( ...( array as any [ ] ) . map ( map ) ) ;
410+ expect ( JSON . parse ( result ) ) . toEqual ( expected ) ;
411+ } ) ;
412+
380413test . each ( [
381414 { condition : "true" , lhs : "4" , rhs : "5" , expected : 4 } ,
382415 { condition : "false" , lhs : "4" , rhs : "5" , expected : 5 } ,
0 commit comments