Created
December 9, 2019 13:49
-
-
Save aSapien/600050817cdf98b8833ef545716b5677 to your computer and use it in GitHub Desktop.
transform a deep object tree
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import _ from 'lodash' | |
// accumulator, value, key, object) | |
export const transformDeep = (root: object, iteratee: (accumulator: object, nodeOrLeaf: any, key: string, parent: object) => void): object => { | |
const deepIteratee = (deepAccumulator: object, deepNodeOrLeaf: any, deepKey: string, deepParent: object): void => { | |
iteratee(deepAccumulator, deepNodeOrLeaf, deepKey, deepParent) //run potential mutation | |
if (_.isPlainObject(deepNodeOrLeaf) || _.isArray(deepNodeOrLeaf)) { // run deep (maybe shouldRunDeep with eq check?) | |
(deepAccumulator as any)[deepKey] = transformDeep(deepNodeOrLeaf, iteratee) | |
} | |
} | |
return _.transform(root, deepIteratee) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment