Skip to content

Instantly share code, notes, and snippets.

@danmartens
Created September 29, 2017 14:02
Show Gist options
  • Save danmartens/37d0e1769ee354d1781b5257f9d6d1ef to your computer and use it in GitHub Desktop.
Save danmartens/37d0e1769ee354d1781b5257f9d6d1ef to your computer and use it in GitHub Desktop.
Deep transforms keys in Objects and Arrays
// @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