Last active
March 7, 2022 21:19
-
-
Save crshmk/b87db5c3dc79211ea4adec51310aa4e1 to your computer and use it in GitHub Desktop.
apply a transform to each key in an object
This file contains hidden or 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 updateKeys from './updateKeys' | |
import { map, toLower } from 'ramda' | |
const x = { | |
ONE: 1, | |
TWO: 2 | |
} | |
const toLowerKeys = updateKeys(toLower) | |
toLowerKeys(x) | |
// { one: 1, two: 2 } | |
const mapToLowerKeys = map(toLowerKeys) | |
mapToLowerKeys([x, x]) | |
// [ { one: 1, two: 2 }, { one: 1, two: 2 } ] |
This file contains hidden or 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 { | |
fromPairs, | |
lensIndex, | |
map, | |
over, | |
pipe, | |
toLower, | |
toPairs | |
} from 'ramda' | |
const headLens = lensIndex(0) | |
const updateKeys = keyTransform => | |
pipe( | |
toPairs, | |
map(over(headLens, keyTransform)), | |
fromPairs | |
) | |
export default updateKeys |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
try it out