2.2.1 • Published 5 years ago

curry-remap-keys v2.2.1

Weekly downloads
201
License
MIT
Repository
github
Last release
5 years ago

curry-remap-keys JavaScript Style Guide NPM version NPM monthly downloads NPM total downloads

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 } = 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.

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

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

License

Copyright © 2018, José Antonio Reyes. Released under the MIT License.