https://github.com/imjaroiswebdev/curry-remap-keys
Utility for remapping key names of an object shallowly and depply nested too, that supports currying for partial application.
https://github.com/imjaroiswebdev/curry-remap-keys
es6 npm-package utility
Last synced: 11 months ago
JSON representation
Utility for remapping key names of an object shallowly and depply nested too, that supports currying for partial application.
- Host: GitHub
- URL: https://github.com/imjaroiswebdev/curry-remap-keys
- Owner: imjaroiswebdev
- License: mit
- Created: 2018-09-02T19:48:00.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2019-01-04T09:37:54.000Z (over 7 years ago)
- Last Synced: 2025-07-11T02:50:48.362Z (11 months ago)
- Topics: es6, npm-package, utility
- Language: JavaScript
- Homepage:
- Size: 120 KB
- Stars: 4
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# curry-remap-keys [](https://standardjs.com) [](https://www.npmjs.com/package/curry-remap-keys) [](https://npmjs.org/package/curry-remap-keys) [](https://npmjs.org/package/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
```shell
$ npm install curry-remap-keys --save
```
## Usage (Basic)
```javascript
const { remapKeys } = require('curry-remap-keys')
// As ES6 Module
import { remapKeys } from 'curry-remap-keys'
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 = remapKeys(remapping, user)
console.log(remappedUser)
// {
// 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.
```javascript
const { remapKeys } = require('curry-remap-keys')
// As ES6 Module
import { remapKeys } from 'curry-remap-keys'
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 = remapKeys(remapping, user)
console.log(remappedUser)
// {
// 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
```javascript
const { remapKeys } = require('curry-remap-keys')
// As ES6 Module
import { remapKeys } from 'curry-remap-keys'
// Same user and remapping object as above
// Partially apply remapKeys for reusing
const userRemapper = remapKeys(remapping)
console.log(userRemapper(user))
// {
// userId: '9e947a10-af08-11e8-9b04-d3ce91a97e8d',
// orderId: 'aa4025d0-af08-11e8-9215-1106c9538c60',
// productName: 'The best notebook of the whole world',
// qty: '1'
// }
```
## See also
* [API documentation](https://github.com/imjaroiswebdev/curry-remap-keys/blob/master/docs/API.md)
## License
Copyright © 2018, [José Antonio Reyes](https://imjaroiswebdev.tech).
Released under the [MIT License](LICENSE).