map-factory
Advanced tools
Changelog
3.1.0
New set()
feature to allow you to set a value on the target object without requiring a source field.
mapper
.set("my.target.mappingStatus", "mapped");
// OR
mapper
.set("my.target.id", () => createId());
Changelog
3.0.0
Fixes a bug when dealing with arrays of arrays with mapping fields involved in parent-child relationships. This is a breaking change as the values supplied to transforms must preserve a nested array structure to be properly set on the target object. The 2.x versions did not preserve this structure.
The getValue
function will also preserve this structure too.
const createMapper = require("map-factory");
let mapper = createMapper();
let src = {
one: [
{ name: "first", two: [{ three: { value1: "A1", value2: "A2" } }, { three: { value1: "B1", value2: "B2" } }] },
{ name: "second", two: [{ three: { value1: "C1", value2: "C2" } }, { three: { value1: "D1", value2: "D2" } }] }
]
};
mapper
.map("one[].name").to("combined[].name")
.map("one[].two[].three[].value1").to("combined[].values[]", value => {
// A transform in v2 received ["A1","B1","C1","D1"]
// The transform in v3 will now receive [["A1","B1"],["C1","D1"]]
return value;
});
let actual = mapper.execute(src);
// The broken result in v2 {"combined":[{"name":"first","values":[["A1","B1","C1","D1"]]},{"name":"second"}]}
// The correct result in v3 {"combined":[{"name":"first","values":["A1","B1"]},{"name":"second","values":["C1","D1"]}]}
Additionally the with()
modifier has been added that will allow more fine grain control when working with arrays of arrays. More details can be found in the README.
Changelog
2.4.1
Fixed bug where each()
returned a null when supplied with an empty array instead of returing an empty array.
Changelog
2.4.0
Added the removing()
method that allows you to specify which fields you don't want instead of explicitly stating the fields you do want.
const mapper = createMapper();
const src = {
user: {
name: "Tim",
occupation: "Enchanter",
password: "scary bunny"
}
};
mapper
.map("user").removing("password").to("user");
.execute(src);
/*
The expected result is:
{
user: {
name: "Tim",
occupation: "Enchanter"
}
}
*/
Changelog
2.3.1
Fixed a bug where an array of undefined values was calling the transform and set when it shouldn't have.
Changelog
2.3.0
Added chain
method to the mapper which allows you to chain multiple mappers together and execute sequentially.
const source = {
"foo": "bar",
"bar": "foo"
};
const mapper = createMapper();
const secondaryMapper = createMapper();
mapper.map("foo");
secondaryMapper.map("foo").to("bar");
const result = mapper.chain(secondaryMapper).execute(source);
/**
The expected result will be
{
"bar": "bar"
}
**/
Changelog
2.2.0
Added executeAsync()
to the mapper which will return a Promise.