Just a tool to translate objects keys
The typical use-case is to query a forgeign API don't use English as work language...
$ yarn add @nutshelllab/object-translator
import Translator from '@nutshelllab/object-translator'
const translator = new Translator(dictionnary, aliases)
dictionnary sample
{
"etablissement": {
"nom": "name",
"utilisateurs": "users"
},
"utilisateur": {
"nom": "lastName",
"prenom": "firstName",
"civilite": "gender",
"datedenaissance": "birthDate",
"etablissements": "companies"
}
}
aliases sample
{
"etablissement": ["etablissement", "etablissements"],
"utilisateur": ["utilisateur", "utilisateurs"]
}
You can now use it everywhere you want ! Here is some samples.
translator.run('utilisateur', 'nom')
exptected result :
lastName
const object = {
nom: 'Doe',
prenom: 'John',
civilite: 'M',
datedenaissance: '25-02-1994',
unknow: 123
}
translator.run('utilisateur', object)
exptected result :
{
lastName: 'Doe',
firstName: 'John',
gender: 'M',
birthDate: '25-02-1994',
unknow: 123
}
const array = ['nom', 'prenom', 'civilite', 'unknow']
translator.run('utilisateur', array)
exptected result :
['lastName', 'firstName', 'gender', 'unknow']
const object = {
nom: 'Doe',
etablissements: {
nom: 'Nutshell'
}
}
exptected result :
{
lastName: 'Doe',
companies: {
name: 'Nutshell'
}
}
MIT © Nutshell