Skip to content

kilwo/objectmapper

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ObjectMapper

Simple Javascript Object Mapper

Provides fluent API for mapping properites from one JavaScript object to another.

Basic Usage

  1. Include the Module
var ObjectMapper = require("./objectmapper");
  1. Specify the source object
ObjectMapper.From(src)
  1. 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")
  1. Execute the mapping:

To update an existing obejct

	.Update(dest);

OR

To return a new object based only on the mappings

	.ToNewObject();

Examples

Update an existing object

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);

Copy mapped fields into new object

var res = ObjectMapper.From(src)
	.Map("name", "fullname")
	.Map("dob.day", "my.dayofbirth")
	.Map((src) => {
		return src.age * 2;
	}, "age")
	.ToNewObject();

Using an object to supply mappings

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()

License

ObjectMapper.js is freely distributable under the terms of the MIT license.

About

Simple Javascript Object Mapper

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published