@@ -4,6 +4,7 @@ import { format } from './string.js'
4
4
import { DimensionError } from '../error/DimensionError.js'
5
5
import { IndexError } from '../error/IndexError.js'
6
6
import { deepStrictEqual } from './object.js'
7
+ import { findNumberOfArguments } from './optimizeCallback.js'
7
8
8
9
/**
9
10
* Calculate the size of a multi dimensional array.
@@ -835,13 +836,52 @@ export function get (array, index) {
835
836
* @returns {* } The new array with each element being the result of the callback function.
836
837
*/
837
838
export function deepMap ( value , array , callback ) {
838
- return recurse ( value , [ ] , array , callback )
839
- function recurse ( value , index , array , callback ) {
839
+ const numberOfArguments = findNumberOfArguments ( callback , array )
840
+ switch ( numberOfArguments ) {
841
+ case 1 :
842
+ return recurse1 ( value )
843
+ case 2 :
844
+ return recurse2 ( value , [ ] )
845
+ case 3 :
846
+ return recurse3 ( value , [ ] )
847
+ default :
848
+ return recurse3 ( value , [ ] )
849
+ }
850
+
851
+ function recurse1 ( value ) {
852
+ if ( Array . isArray ( value ) ) {
853
+ return value . map ( function ( child ) {
854
+ // we create a copy of the index array and append the new index value
855
+ const results = recurse1 ( child )
856
+ return results
857
+ } )
858
+ } else {
859
+ // invoke the callback function with the right number of arguments
860
+ return callback ( value )
861
+ }
862
+ }
863
+
864
+ function recurse2 ( value , index ) {
840
865
if ( Array . isArray ( value ) ) {
841
866
return value . map ( function ( child , i ) {
842
867
// we create a copy of the index array and append the new index value
843
868
index . push ( i )
844
- const results = recurse ( child , index , array , callback )
869
+ const results = recurse2 ( child , index )
870
+ index . pop ( )
871
+ return results
872
+ } )
873
+ } else {
874
+ // invoke the callback function with the right number of arguments
875
+ return callback ( value , [ ...index ] )
876
+ }
877
+ }
878
+
879
+ function recurse3 ( value , index ) {
880
+ if ( Array . isArray ( value ) ) {
881
+ return value . map ( function ( child , i ) {
882
+ // we create a copy of the index array and append the new index value
883
+ index . push ( i )
884
+ const results = recurse3 ( child , index )
845
885
index . pop ( )
846
886
return results
847
887
} )
@@ -862,13 +902,51 @@ export function deepMap (value, array, callback) {
862
902
* @returns {* } The new array with each element being the result of the callback function.
863
903
*/
864
904
export function deepForEach ( value , array , callback ) {
865
- recurse ( value , [ ] , array , callback )
866
- function recurse ( value , index , array , callback ) {
905
+ const numberOfArguments = findNumberOfArguments ( callback , array )
906
+ switch ( numberOfArguments ) {
907
+ case 1 :
908
+ recurse1 ( value )
909
+ break
910
+ case 2 :
911
+ recurse2 ( value , [ ] )
912
+ break
913
+ case 3 :
914
+ recurse3 ( value , [ ] )
915
+ break
916
+ default :
917
+ recurse3 ( value , [ ] )
918
+ break
919
+ }
920
+
921
+ function recurse1 ( value ) {
922
+ if ( Array . isArray ( value ) ) {
923
+ return value . forEach ( function ( child ) {
924
+ recurse1 ( child )
925
+ } )
926
+ } else {
927
+ // invoke the callback function with the right number of arguments
928
+ callback ( value )
929
+ }
930
+ }
931
+
932
+ function recurse2 ( value , index ) {
933
+ if ( Array . isArray ( value ) ) {
934
+ return value . forEach ( function ( child , i ) {
935
+ index . push ( i )
936
+ recurse2 ( child , index )
937
+ index . pop ( )
938
+ } )
939
+ } else {
940
+ // invoke the callback function with the right number of arguments
941
+ callback ( value , [ ...index ] )
942
+ }
943
+ }
944
+
945
+ function recurse3 ( value , index ) {
867
946
if ( Array . isArray ( value ) ) {
868
947
return value . forEach ( function ( child , i ) {
869
- // we create a copy of the index array and append the new index value
870
948
index . push ( i )
871
- recurse ( child , index , array , callback )
949
+ recurse3 ( child , index )
872
950
index . pop ( )
873
951
} )
874
952
} else {
0 commit comments