npm install renk
const { rename, renameonly } from 'renk'
const user = {
firstName: "Bob",
lastName: "Foo", age: 16,
gender: "male",
food: [{
name: "rice"
},{
name: "bean"
}, {
name: "steak"
}]
}
Renomeando as keys do objeto user
const renamedUser = rename(user, {
food: "favoriteFoods",
firstName: "name"
})
/*
{
name: "Bob",
lastName: "Foo",
age: 16,
gender: "male",
favoriteFoods: [{
name: "rice"
},{
name: "bean"
}, {
name: "steak"
}]
}
*/
Nesse caso, vamos transformar o objeto user
e excluir as keys que não desejamos
const renamedUser = rename(user, {
food: "favoriteFoods",
firstName: "name"
}, ['gender', 'age', 'lastName'])
/*
{ name: "Bob",
favoriteFoods: [{
name: "rice"
},{
name: "bean"
}, {
name: "steak"
}]
}
*/
}
Retornar apenas as keys renomeadas:
renameOnly(user, {
firstName: ['fullName', person => `${person.firstName} ${person.lastName}`]
})
/*
{
fulName: "Bob Foo"
}
*/
Alterando as keys e transformando os valores passando um array com [key, function]
e removendo a propriedade lastName
do objeto user
rename(user, {
firstName: ['fullName', person => `${person.firstName} ${person.lastName}`]
}, ["lastName"])
/*
{age: 16, gender: "male", food: Array(3), fullName: "Bob Foo"}
*/
rename
e renameOnly
podem receber um array como primeiro argumento podendo atualizar toda a estrutura
const foods = [
{n: "rice", price: 16.50},
{n: "onion", price: 5.00}
]
rename(foods, {n: "name"})
/*
[
{name: "rice", price: 16.50},
{name: "onion", price: 5.00}
]
*/
const foods = [
{n: "rice", price: 16.50},
{n: "onion", price: 5.00}
]
renameOnly(foods, {n: "name"})
/*
[
{name: "rice"},
{name: "onion"}
]
*/