curry-remap-keys
Utility for remapping key names of an object shallowly and depply nested too, that supports currying for partial application.
It embraces functional programming not mutating its entry, but returning a new object that maintains the original prototype chain and properties enumerables or not, respecting referential transparency.
Install
$ npm install curry-remap-keys --save
Usage (Basic)
const remapKeys = // As ES6 Module const user = user_id: '9e947a10-af08-11e8-9b04-d3ce91a97e8d' order_id: 'aa4025d0-af08-11e8-9215-1106c9538c60' productName: 'The best notebook of the whole world' qty: '1' const remapping = user_id: 'userId' order_id: 'orderId' const remappedUser = console// {// userId: '9e947a10-af08-11e8-9b04-d3ce91a97e8d',// orderId: 'aa4025d0-af08-11e8-9215-1106c9538c60',// productName: 'The best notebook of the whole world',// qty: '1'// }
Usage (Nested keys)
This will follow or create the necessary key names that fulfills the supplied path.
const remapKeys = // As ES6 Module const user = user_id: '9e947a10-af08-11e8-9b04-d3ce91a97e8d' order_id: 'aa4025d0-af08-11e8-9215-1106c9538c60' productName: 'The best notebook of the whole world' qty: '1' billingInfo: locationInfo: billing_address: '1368 Meadowbrook Mall Road' city: 'Los Angeles, CA' zip: '90017' customerInfo: interested_in: 'notebooks' 'smartphones' 'smart tv' isOneTimeBuyer: false const remapping = user_id: 'userId' billing_address: 'billingAddress' 'billingInfo' 'locationInfo' // Natural point notation for path to nested key also supported like... // billing_address: ['billingAddress', 'billingInfo.locationInfo'], interested_in: 'interestedIn' 'customerInfo' const remappedUser = console// {// userId: '9e947a10-af08-11e8-9b04-d3ce91a97e8d',// order_id: 'aa4025d0-af08-11e8-9215-1106c9538c60',// productName: 'The best notebook of the whole world',// qty: '1',// billingInfo: {// locationInfo: {// billingAddress: '1368 Meadowbrook Mall Road',// city: 'Los Angeles, CA',// zip: '90017'// }// },// customerInfo: {// interestedIn: ['notebooks', 'smartphones', 'smart tv'],// isOneTimeBuyer: false// }// }
Currying
const remapKeys = // As ES6 Module // Same user and remapping object as above // Partially apply remapKeys for reusingconst userRemapper = console// {// userId: '9e947a10-af08-11e8-9b04-d3ce91a97e8d',// orderId: 'aa4025d0-af08-11e8-9215-1106c9538c60',// productName: 'The best notebook of the whole world',// qty: '1'// }
See also
License
Copyright © 2018, José Antonio Reyes. Released under the MIT License.