parky
Re-mapping object keys to new names
Usage
Install with npm install --save parky
.
var parky = ;var user = username: 'bob' email: 'bob@gmail.com';var result = ; console;// { user_name: 'bob', email: 'bob@gmail.com' }
You can optionally pass a third parameter, a boolean, specifying if you want it to throw an error if a mapping is invalid. Otherwise, the default behavior is to ignore invalid key mappings.
;// Throws an error
Constructor Usage
var user = username: 'dragonx' email: 'd@x.com' ;var parky = keyMap: username: 'user_name' reuseObject: true throwOnInvalid: true; var result = parky;// { user_name: '..', email: '..' } var alternate = parky;// { user_name: '..', user_email: '..' }
Methods
map
parky
The second argument, map
, is optional if you have a keyMap
specified in the constructor.
The keyMap
and this map
are merged if both exist, with map
taking precedence.
reverseMap
parky
The second argument, reverseMap
, is optional if you have a keyMap
specified in the constructor.
The reverseMap should be defined in reverse, so key = current property name, value = desired property name,
while the keyMap
is defined normally, and is reversed for you. These two are merged if they are both defined.
Example
var parky = keyMap: lastModified: 'last_modified' ;var postRaw = title: 'Hello' last_modified: 'today'; var normalized = parky; // value of `normalized`//// {// title: 'Hello',// lastModified: 'today'// } var raw = parky; // value of `raw`//// {// title: 'Hello',// last_modified: 'today'// }
Test
npm test