Skip to content
/ Renk Public

💫 Renomeie/transforme chaves e valores de um objeto. Com suporte ao TypeScript

License

Notifications You must be signed in to change notification settings

Freitas29/Renk

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

53 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Codacy Badge

🌠 Instalação

npm install renk

🚩 Uso

  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"}
  ]
*/

About

💫 Renomeie/transforme chaves e valores de um objeto. Com suporte ao TypeScript

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published