normalize-obj
Rename keys, and/or change structure of an object.
Install
npm install normalize-obj --save
How to use
var normalize = ; var object = name: 'Darlan' age: 25 address: num: '117'; var normalized = ;// => {fullname: 'Darlan', age: 25, address: {num: '117'}};
Methods can be chained, example:
;// => {fullname: 'Darlan', old: 25, address: {num: '117'}};
Accept nesting keys, with dot syntax, to change key name
;// => {name: 'Darlan', age: 25, address: {number: '117'}};
or change structure too
; // => {name: 'Darlan', age: 25, number: '117'};
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' ; ;/* => { number: 1107, // keep address, because have others properties address: { street: 'Paulista', country: 'br' }, phone: { mobile: '0000-0000' } }; */ // now, if dont have properties; /* => { number: 1107, address: { street: 'Paulista', country: 'br' }, mobile: '0000-0000' // delete phone, because don't have others properties }; */
And offer method to copy field
;// => {name: 'Darlan', age: 25, address: {num: '117'}, number: '117'};
Tests
npm test