parky

0.5.1 • Public • Published

parky Build Status

Re-mapping object keys to new names

NPM

Usage

Install with npm install --save parky.

var parky = require('parky');
var user = {
  username: 'bob',
  email: 'bob@gmail.com'
};
var result = parky(user, {
  username: 'user_name'
});
 
console.log(result);
// { 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.

parky(user, { contactName: undefined }, true);
// Throws an error

Constructor Usage

var user = { username: 'dragonx', email: 'd@x.com' };
var parky = new Parky({
  keyMap: {
    username: 'user_name'
  },
  reuseObject: true,
  throwOnInvalid: true
});
 
var result = parky.map(user);
// { user_name: '..', email: '..' }
 
var alternate = parky.map(user, {
  email: 'user_email'
});
// { user_name: '..', user_email: '..' }

Methods

map

parky.map(data, map)

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.reverseMap(data, reverseMap)

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 = new Parky({
  keyMap: {
    lastModified: 'last_modified'
  }
});
var postRaw = {
  title: 'Hello',
  last_modified: 'today'
};
 
var normalized = parky.reverseMap(postRaw);
 
//  value of `normalized`
//
//  {
//    title: 'Hello',
//    lastModified: 'today'
//  }
 
 
var raw = parky.map(normalized);
 
//  value of `raw`
//
//  {
//    title: 'Hello',
//    last_modified: 'today'
//  }

Test

npm test

Package Sidebar

Install

npm i parky

Weekly Downloads

0

Version

0.5.1

License

ISC

Last publish

Collaborators

  • knownasilya