convert-object-keys
Recursively convert an object's keys into anything you want.
Use Case
convert-object-keys
is a tiny, versatile library that can recursively convert your object keys with any transformer method you want. My original use case was this: I was building a JavaScript project that needed to interact with both the [Firebase][firebase] and the [Twilio][twilio] APIs. The issue is that my lettercase was different everywhere. Since I was using JavaScript, my variables were all camel cased (thisIsACamelCasedVariable
). Firebase, however, sent all their JSON keys in snake case (this_is_a_snake_cased_variable
), while Twilio sent all of their JSON keys in upper camel case (ThisIsAnUpperCamelCasedVariable
).
To make matters worse, my ESlint config enforced camel casing and I didn't want to change it! Instead, I wrote a couple of tools to solve the issue: convert-object-keys
and transform-string-case
.
Usage
To use convert-object-keys
, you need to pass it an object and a transformer method.
const objectToConvert = "FOO": "lorem ipsum" "BAR": "lorem ipsum" "BAZ": "lorem ipsum" const transformer = { return key} /* Result:{ "foo": "lorem ipsum", "bar": "lorem ipsum", "baz": "lorem ipsum",}*/
By default, the convertObjectKeys
method will convert all keys in the object recursively, including traversing into arrays. You can control this behavior by passing a boolean in as the third parameter.
const objectToConvert = "FOO": "BAR": "lorem ipsum" const transformer = { return key} /* Result:{ "foo": { "bar": "lorem ipsum", },}*/ /* Result:{ "foo": { "BAR": "lorem ipsum", },}*/
Contributing
If you want to contribute, make sure to check out our contributing guide!
Contributors
Trezy 💻 📖 🤔 🚇 🚧 🔧 ⚠️ |