re-mapper
v1.0.1
Published
JavaScript object mapper
Downloads
5
Readme
ReMapper
Remapper maps one object into another using a mapping configuration overrides. By default, all properties from the source object will be remapped as-is to the destination object unless, an override is provided.
It was created to help with mapping between JavaScript objects and sql/relation database models.
Usage
Basic mapping example:
import ReMapper from 're-mapper';
const mapper = new ReMapper({
'firstName': 'first_name',
'lastName': 'last_name',
});
const source = {
id: 1,
firstName: 'John',
lastName: 'Citizen',
email: '[email protected]'
};
const mapped = mapper.map(source);
// mapped:
// {
// id: 1,
// first_name: 'John',
// last_name: 'Citizen',
// email: '[email protected]'
// }
Array mapping example:
const sourceList = [
{
id: 1,
firstName: 'John',
lastName: 'Citizen',
email: '[email protected]'
},
{
id: 2,
firstName: 'Jane',
lastName: 'Doe',
email: '[email protected]'
}
];
const mappedList = sourceList.map(mapper.map);
The reverse mapping is also available with the reversMap
method which inverts the mapping definition.
Example:
import ReMapper from 're-mapper';
const mapper = new ReMapper({
'firstName': 'first_name',
'lastName': 'last_name',
});
const mapped = {
id: 1,
first_name: 'John',
last_name: 'Citizen',
email: '[email protected]'
};
const unmapped = mapper.reverseMap(source);
// unmapped:
// {
// id: 1,
// firstName: 'John',
// lastName: 'Citizen',
// email: '[email protected]'
// }