Created
September 29, 2017 14:02
-
-
Save danmartens/37d0e1769ee354d1781b5257f9d6d1ef to your computer and use it in GitHub Desktop.
Deep transforms keys in Objects and Arrays
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
// @flow | |
import { isObject, isArray, reduce, map } from 'lodash'; | |
function deepTransformKeys<T>(transformKey: string => string, object: T): T { | |
if (isObject(object)) { | |
return reduce( | |
object, | |
(reduction, value, key) => { | |
reduction[transformKey(key)] = deepTransformKeys(transformKey, value); | |
return reduction; | |
}, | |
{} | |
); | |
} else if (isArray(object)) { | |
return map(object, value => deepTransformKeys(transformKey, value)); | |
} else { | |
return object; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment