normalize-obj
Rename keys, and/or change structure of an object.
Install
npm install normalize-obj --save
How to use
var normalize = require('normalize-obj');
var object = {name: 'Darlan', age: 25, address: {num: '117'}};
var normalized = normalize(object).change('name', 'fullname');
Methods can be chained, example:
normalize(object)
.change('name', 'fullname')
.change('age', 'old');
Accept nesting keys, with dot syntax, to change key name
normalize(object).change('address.num', 'address.number');
or change structure too
normalize(object).change('address.num', 'number');
Important
On change structure, like below, if old structure don't have others properties, there are deleted. I.e:
var object = {
address: {
street: 'Paulista',
country: 'br',
num: '1107'
},
phone: {
mobile: '0000-0000'
}
};
normalize(object).change('address.num', 'number');
normalize(object).change('phone.mobile', 'mobile');
And offer method to copy field
normalize(object).copy('address.num', 'number');
Tests
npm test