MCPcopy Index your code
hub / github.com/josdejong/mathjs / deepMap

Function deepMap

src/utils/array.js:867–904  ·  view source on GitHub ↗
(array, callback, skipIndex = false)

Source from the content-addressed store, hash-verified

865 * @returns {Array} A new array with each element being the result of the callback function.
866 */
867export function deepMap (array, callback, skipIndex = false) {
868 if (array.length === 0) {
869 return []
870 }
871
872 if (skipIndex) {
873 return recursiveMap(array)
874 }
875 const index = []
876
877 return recursiveMapWithIndex(array, 0)
878
879 function recursiveMapWithIndex (value, depth) {
880 if (Array.isArray(value)) {
881 const N = value.length
882 const result = Array(N)
883 for (let i = 0; i < N; i++) {
884 index[depth] = i
885 result[i] = recursiveMapWithIndex(value[i], depth + 1)
886 }
887 return result
888 } else {
889 return callback(value, index.slice(0, depth), array)
890 }
891 }
892 function recursiveMap (value) {
893 if (Array.isArray(value)) {
894 const N = value.length
895 const result = Array(N)
896 for (let i = 0; i < N; i++) {
897 result[i] = recursiveMap(value[i])
898 }
899 return result
900 } else {
901 return callback(value)
902 }
903 }
904}
905
906/**
907 * Recursively iterates over each element in a multi-dimensional array and applies a callback function.

Callers 15

deepMap.test.jsFile · 0.90
den.jsFile · 0.90
num.jsFile · 0.90
isPositive.jsFile · 0.90
isNaN.jsFile · 0.90
isZero.jsFile · 0.90
isPrime.jsFile · 0.90
isInteger.jsFile · 0.90
isNegative.jsFile · 0.90
isNumeric.jsFile · 0.90
not.jsFile · 0.90
factorial.jsFile · 0.90

Calls 2

recursiveMapFunction · 0.85
recursiveMapWithIndexFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…