I would need to rewrite all properties for an object which could contains hyphenated word to camelCase using ramdajs.
Example, property name animation-timing-function
should become animationTimingFunction
and so on for every keys.
Could you please provide an example:
Here is data I should convert:
var data = {
"bounce": [
{
"animation-timing-function": "cubic-bezier(0.215, 0.610, 0.355, 1.000)",
"transform": "translate3d(0,0,0)",
"offset": 0
},
{
"animation-timing-function": "cubic-bezier(0.215, 0.610, 0.355, 1.000)",
"transform": "translate3d(0,0,0)",
"offset": 0.2
},
{
"animation-timing-function": "cubic-bezier(0.755, 0.050, 0.855, 0.060)",
"transform": "translate3d(0, -30px, 0)",
"offset": 0.4
},
{
"animation-timing-function": "cubic-bezier(0.755, 0.050, 0.855, 0.060)",
"transform": "translate3d(0, -30px, 0)",
"offset": 0.43
},
{
"animation-timing-function": "cubic-bezier(0.215, 0.610, 0.355, 1.000)",
"transform": "translate3d(0,0,0)",
"offset": 0.53
},
{
"animation-timing-function": "cubic-bezier(0.755, 0.050, 0.855, 0.060)",
"transform": "translate3d(0, -15px, 0)",
"offset": 0.7
},
{
"animation-timing-function": "cubic-bezier(0.215, 0.610, 0.355, 1.000)",
"transform": "translate3d(0,0,0)",
"offset": 0.8
},
{
"transform": "translate3d(0,-4px,0)",
"offset": 0.9
},
{
"animation-timing-function": "cubic-bezier(0.215, 0.610, 0.355, 1.000)",
"transform": "translate3d(0,0,0)",
"offset": 1
}
]
};
I have tried modify this receipt but with no success:
const renameKeys = R.curry((keysMap, obj) => {
return R.reduce((acc, key) => {
acc[keysMap[key] || key] = obj[key];
return acc;
}, {}, R.keys(obj));
});
It's not clear to me what you tried. With that function and a small bit of infrastructure to apply it to the right objects, it seems to work for me:
But that seems to be the only key having hyphens in the name, so perhaps I'm simply confused. Are there supposed to be others?
You can see this in action on the Ramda REPL.
Update
Based on follow-up comments, it looks like the entry in Ramda's Cookbook right before the one you used is more appropriate:
Although Ramda does not include a
camelCase
function, you can find them all over the Web, or create your own quite easily. One version:With these, it's even easier:
Again, this version is also available in the Ramda REPL.