jomap

0.0.2 • Public • Published

JS Object Mapper (jomap)

jomap is a little library built to solve a problem - getting rid of code that mapped one object to another.

Install

npm install jomap

Usage

var jomap = require('jomap');

Options

var samoleOptions = {
    auto: true, //Map all destination properties. Used for merge
    interceptors: [
        {   
            to: 'info',
            converter: function(person) {
                return "" + person.name + " " + person.age;}
        }
    ],
    map: {
        from: { to: 'to' },
        age: { to: 'age.days', converter: function(age) {return age * 365;}},
        home: { to: 'homeAddress' }
    } 
}

Simple mapping

sample = {
    name: 'John Doe',
    age: 27,
    address: 'earth'
}
 
options = {
    map: {
      name: 'fullName',
      age: 'age',
      address: 'home'
  }
};
 
result = jomap.map(sample, options);

Returns

{
    fullName: 'John Doe',
    age: 27,
    home: 'earth'
}

Converters

options = {
    map: {
      name: 'fullName',
      age: { to: 'age.days', converter: function(age) {return age * 365;}},
      address: 'home'
  }
};
 
result = jomap.map(sample, options);

Returns

{
    fullName: 'John Doe',
    age: {days: 27 * 365},
    home: 'earth'
}

Interceptors

Interceptor - is a global converter. You need provide converter func and destinamion property name.

 
options = {
    interceptors: [
    {
        to: 'info',
        converter: function(person) {
          return "" + person.name + " " + person.age;
        }
    }
    ],
    map: {
      address: 'address'
  }
};
 
result = jomap.map(sample, options);

Returns

{
    info: 'John Doe 27',
    address: 'earth'
}

Licence

(The MIT License)

Copyright (c) 2012 Vitaly Domnikov

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Readme

Keywords

none

Package Sidebar

Install

npm i jomap

Weekly Downloads

0

Version

0.0.2

License

MIT

Last publish

Collaborators

  • dotcypress