Skip to content

Commit 4a507e3

Browse files
committed
refactor deepMap to simplify zero-skipping logic and utilize iterableMap
1 parent a0e7ca5 commit 4a507e3

File tree

1 file changed

+5
-22
lines changed

1 file changed

+5
-22
lines changed

src/utils/collection.js

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { isCollection, isMatrix } from './is.js'
22
import { IndexError } from '../error/IndexError.js'
33
import { arraySize, findFirst as arrayFindFirst } from './array.js'
44
import { _switch } from './switch.js'
5-
import { forEach as iterableForEach } from './iterable.js'
5+
import { forEach as iterableForEach, map as iterableMap } from './iterable.js'
66

77
/**
88
* Test whether an array contains collections
@@ -56,28 +56,11 @@ export function deepForEach (array, callback) {
5656
* @return {Array | Matrix} res
5757
*/
5858
export function deepMap (array, callback, skipZeros) {
59-
if (skipZeros) {
60-
const callbackSkip = (x) => x === 0 ? 0 : callback(x)
61-
if (isMatrix(array)) {
62-
return array.create(recurse(array.valueOf(), callbackSkip), array.datatype())
63-
} else {
64-
return recurse(array, callbackSkip)
65-
}
59+
const callbackSkip = skipZeros ? x => x === 0 ? 0 : callback(x) : x => callback(x)
60+
if (isMatrix(array)) {
61+
return array.map(callbackSkip)
6662
} else {
67-
if (isMatrix(array)) {
68-
return array.create(recurse(array.valueOf(), callback), array.datatype())
69-
} else {
70-
return recurse(array, callback)
71-
}
72-
}
73-
function recurse (array) {
74-
if (Array.isArray(array)) {
75-
return array.map(function (x) {
76-
return recurse(x)
77-
})
78-
} else {
79-
return callback(array)
80-
}
63+
return iterableMap(array, callbackSkip, false, false, array)
8164
}
8265
}
8366

0 commit comments

Comments
 (0)