Skip to content

Instantly share code, notes, and snippets.

@sanusart
Last active July 14, 2019 11:40
Show Gist options
  • Save sanusart/a68a3b990044e7cb393c3f100664935d to your computer and use it in GitHub Desktop.
Save sanusart/a68a3b990044e7cb393c3f100664935d to your computer and use it in GitHub Desktop.
Rename object key (deep object too) #lodashfp #fp #lodash #rename
const { flow, omit, head, set, at } = require("lodash/fp")
const object = {
id: 'CA1',
longName: 'The name',
status: 'open',
another1: 'value1',
another2: 'value2'
};
const objectDeep = {
details: {
id: 'CA1',
longName: 'The name deep',
status: 'open',
another1: 'value1',
another2: 'value2'
}
};
const renameKey = (from, to, object) => flow([
set(to, head(at(from, object))),
omit(from)
])(object);
export const renameKeyCurried = curry((from, to, object) =>
flow([set(to, head(at(from, object))), omit(from)])(object)
);
// renameKey('longName', 'name', object);
renameKey('details.longName', 'details.name', objectDeep);
@sanusart
Copy link
Author

sanusart commented Jul 3, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment