Simple Javascript Object Mapper
Provides fluent API for mapping properites from one JavaScript object to another.
- Include the Module
var ObjectMapper = require("./objectmapper");
- Specify the source object
ObjectMapper.From(src)
- Map the properties
Map method adds the mapping (source,dest) to the current mapper and returns the mapper.
The source of a mapping can be either a string denoting the path into the src object, or a function. In the case of a function, the mapper will call the function passing in the source object as the only parameter.
.Map("name", "fullname")
.Map("dob.day", "my.dayofbirth")
.Map((src) => {
return src.age * 2;
}, "age")
- Execute the mapping:
To update an existing obejct
.Update(dest);
OR
To return a new object based only on the mappings
.ToNewObject();
Only properties supplied in the map commands will be changed. all other properties in dest will remain untouched.
ObjectMapper.From(src)
.Map("name", "fullname")
.Map("dob.day", "my.dayofbirth")
.Map((src) => {
return src.age * 2;
}, "age")
.Update(dest);
var res = ObjectMapper.From(src)
.Map("name", "fullname")
.Map("dob.day", "my.dayofbirth")
.Map((src) => {
return src.age * 2;
}, "age")
.ToNewObject();
Mappings are generated based on the supplied object. Destination object shape matches the template, the source locations are the template values. You can chain Map() and MapTemplate() calls to map a function.
var res2 = ObjectMapper.From(src)
.MapTemplate({
fullname: "name",
my: {
dayofbirth: "dob.day"
}
})
.Map((src) => {
return src.age * 2;
}, "age")
.ToNewObject(); //or .Update()
ObjectMapper.js is freely distributable under the terms of the MIT license.