@digitregroup/object-mapper
v1.1.6
Published
Makes object-to-object transformation easy.
Downloads
50
Readme
object-mapper
Makes object-to-object transformation easy.
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
- MIT : http://opensource.org/licenses/MIT