@digitregroup/object-mapper

1.1.6 • Public • Published

object-mapper

Makes object-to-object transformation easy.

stars forks

Quick example

const sourceObject = {
  fabfour: { user_count: 4 },
  user: [
    {
      first:       'John',
      last:        'Lennon',
      instruments: [
        { name: 'Guitar', colour: 'Red' },
        { name: 'Bass guitar', colour: 'Blue' }
      ]
    },
    {
      first:   'Paul',
      last:    'McCartney',
      picture: ['picture0.jpg', 'picture1.png', 'picture2.gif' ]
    },
    { first: 'George', last: 'Harrison' },
    { first: 'Ringo', last: 'Starr' }
  ]
};

const mapping = {
  'ringo.last_name':        'user[first=Ringo].last',
  'paul.pictures':          'user.1.picture[]',
  'john.bass.colour':       'user.0.instruments.1.colour',
  'amount_of_band_members': 'fabfour.user_count',
};

const result = ObjectMapper.mapItem(sourceObject, mapping);

//{
//  "ringo": {
//    "last_name": "Starr"
//  },
//  "paul": {
//    "pictures": [
//      "picture0.jpg",
//      "picture1.png",
//      "picture2.gif"
//    ]
//  },
//  "john": {
//    "bass": {
//      "colour": "Blue"
//    }
//  },
//  "amount_of_band_members": 4
//}

Features

Dot notation matching

ObjectMapper.mapItem (
  {parent: {child: {value: 'bar'}}},
  {result: 'parent.child.value'}
);

// {"result":"bar"}

Query matching

ObjectMapper.mapItem (
  {
    children: [
      {name: 'foo', value: 'bar'},
      {name: 'baz', value: 'biz'}
    ]
  },
  {result: 'children[name=baz].value'}
);

// {"result":"biz"}

Data transformation

ObjectMapper.mapItem (
  {
    user: {
      name:      'lennon',
      firstname: 'john'
    }
  },
  {
    'result': [
      'user.name',
      name => name.toUpperCase(),
      uppername => uppername.split('')
    ]
  }
);

// {"result":["L","E","N","N","O","N"]}

Dot notation object conversion

ObjectMapper.mapItem (
  {foo: 'bar'},
  {'target.name.can.be.anything.0': 'foo'}
);

// {"target":{"name":{"can":{"be":{"anything":["bar"]}}}}}

Install

npm install --save @digitregroup/object-mapper

Dependencies

Package Version Dev
flat ^4.1.0
chai ^4.2.0
cross-env ^5.2.0
laravel-mix ^4.0.14
mocha ^6.0.2
nyc ^13.3.0
vue-template-compiler ^2.6.7

Contributing

Contributions welcome; Please submit all pull requests against the develop branch. If your pull request contains JavaScript patches or features, you should include relevant unit tests. Thanks!

Author

DigitRE Group

License

Package Sidebar

Install

npm i @digitregroup/object-mapper

Weekly Downloads

7

Version

1.1.6

License

MIT

Unpacked Size

42.8 kB

Total Files

8

Last publish

Collaborators

  • bouiboui
  • l0gin
  • dominique_digit
  • digital-factory-digitregroup