Last active
July 14, 2019 11:40
-
-
Save sanusart/a68a3b990044e7cb393c3f100664935d to your computer and use it in GitHub Desktop.
Rename object key (deep object too) #lodashfp #fp #lodash #rename
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
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); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://runkit.com/sanusart/5942dda2c72ffe0012804a76