1.2.2 • Published 9 years ago

@schornio/map-object v1.2.2

Weekly downloads
1
License
ISC
Repository
github
Last release
9 years ago

map-object

Build Status

Usage

_constructor(map, options)

  • map: map schema
  • options: (optional) configuration
    • strictMode: error on unsuccessful mapping (default: ignore)
    • context: provide a context object

prototype.map(obj, callback)

  • obj: object to map
  • callback(error, obj): on mapping finished

Map Schema

{
  <string: destination name>: <string: source path>,
  <string: destination name>: <string: '$' + context source path>,
  <string: destination name>: <object: nested schema>
}

Example

var MapObjLib = require('@schornio/map-object');

var obj = {
  simpleProperty: 42,
  complexProperty: {
    child: 126
  }
};

var map = {
  flattern: 'complexProperty.child',
  explode: {
    child: 'simpleProperty'
  }
};

var objMap = new MapObjLib(map);
objMap.map(obj, function(error, result) {
  /* result == {
    flattern: 126,
    explode: {
      child: 42
    }
  }*/
});