node package manager

remapped

Translate objects using dot notated mappings.

remapped Build Status

Translate objects using dot notated mappings.

NPM

If you need to do something more complex than simply rearranging an object, you should use traverse.

API

remapped(source, mapping, defaults)

Translate a source object to a new form with a supplied mapping and optional defaults.

source the source data
mapping a destination object whose keys are dot notated string paths to values in the source
defaults a optional defaults object to fill values not found in source

Example

const remapped = require('remapped');
 
var source = {
  id: 0,
  name: 'tyler',
  age: 30,
  nested: {
    id: 0
  }
};
 
var mapping = {
  myId: 'id',
  myName: 'name',
  myAge: 'age',
  dotNotatedKey: 'nested.id',
  myArray: ['name', 'age', {objectAge: 'age'}],
  temp: {
    myNestedId: 'id'
  },
  defaultNull: 'missing.key',
  noDefault: 'missing.key'
};
 
var defaults = {
  defaultNull: null
};
 
remapped(source, mapping, defaults); // { 
                                     //   myId: 0, 
                                     //   myName: 'tyler', 
                                     //   myAge: 30, 
                                     //   dotNotatedKey: 1, 
                                     //   myArray: ['tyler', 30, {objectAge: 30}], 
                                     //   temp: { 
                                     //     myNestedId: 0 
                                     //   }, 
                                     //   defaultNull: null, 
                                     //   noDefault: undefined 
                                     // }; 

Release History

  • 2014-02-27 - v0.3.1 - harder check on falsy values
  • 2014-02-27 - v0.3.0 - allow default values
  • 2014-02-26 - v0.2.0 - use js-traverse
  • 2014-02-26 - v0.1.0 - initial release