npm i @eramux/rekey
First we will need to create an object which will be passed to the rekey funcitons
// Define a sample object
let data = {
key1: "some string",
settings: {
name: "John",
active: true
},
users: [
{
name: "Marie"
},
{
name: "Steven",
type: "Human"
}
],
items: [
{
name: "abs"
},
{
name: "pvc",
supported: false
}
]
}
import { renameKey } from "@eramux/rekey"
// Rename the settings -> name key to 'username'
renameKey(data, "settings.name", "username")
// -> {
// ...
// settings: {
// username: "John"
// active: true
// }
// ...
// }
// It will automatically rename keys in object arrays
renameKey(data, "items.name", "material")
// -> {
// ...
// items: [
// {
// material: "abs"
// }
// ...
// ]
// ...
// }
// It only changes keys that exist
renameKey(data, "users.type", "species")
// -> {
// ...
// users: [
// {
// name: "Marie"
// },
// {
// name: "Steven",
// species: "Human"
// }
// ]
// ...
// }
// Delete a key nested deeply inside of the object or arrays
deleteKey(data, "users.name")
// -> {
// ...
// users: [
// {},
// {
// species: "Human"
// }
// ]
// ...
// }
Since rekey does not copy the object, you won't have any unexpected memory spikes. It works by only modifying the reference recursively.
Any contributions are welcome! Please make sure to first file an issue so we can discuss the problem at hand and you don't create a PR that doesn't get pulled.