map-factory
Advanced tools
Comparing version 1.7.0 to 1.7.1
@@ -32,3 +32,3 @@ "use strict"; | ||
registerMapping(mapping) { | ||
registerMapping_(mapping) { | ||
@@ -40,4 +40,7 @@ this.assignment.push(mapping); | ||
// create a mapping for a single or multiple source field(s) and return the mapping object | ||
// the mapping object enables the fluent/chainable interface | ||
const mapping = new _mapping2.default(source, this, this.options); | ||
this.registerMapping(mapping); | ||
this.registerMapping_(mapping); | ||
@@ -49,4 +52,5 @@ return mapping; | ||
// validate inputs | ||
if (!sourceArray) { | ||
// This should probably return undefined | ||
// TODO: This should probably return undefined | ||
return null; | ||
@@ -59,2 +63,3 @@ } | ||
// iterate over an array of values and map each one | ||
if (sourceArray.length > 0) { | ||
@@ -66,3 +71,3 @@ return sourceArray.map(item => { | ||
// This should probably return undefined | ||
// TODO: This should probably return undefined | ||
return null; | ||
@@ -73,2 +78,3 @@ } | ||
// validate inputs | ||
if (source === null || source === undefined) { | ||
@@ -78,2 +84,3 @@ throw new Error("A source object is required"); | ||
// ensure we have a destination object to target | ||
if (destination === null || destination === undefined) { | ||
@@ -89,4 +96,10 @@ destination = {}; | ||
// annoyingly, VS Code's auto format is at odds with eslint | ||
/* eslint-disable indent */ | ||
/* eslint-disable indent */ | ||
// map-factory supports 3 modes: | ||
// - single source mode -> mapper.map("field1") | ||
// - multiple sources mode -> mapper.map(["field1", "field2"]) | ||
// - or mode -> mapper.map("field1").or("field2") | ||
// Here we just route the mode to the appropriate logic | ||
switch (descriptor.mode) { | ||
@@ -115,3 +128,5 @@ case SINGLE_MODE: | ||
let targetPath = item.target; | ||
let transform = item.transform; | ||
let transform = item.transform, | ||
alwaysSet = item.alwaysSet, | ||
alwaysTransform = item.alwaysTransform; | ||
@@ -132,3 +147,3 @@ let isCustomTransform = true; | ||
return { mode: mode, targetPath: targetPath, sourcePath: sourcePath, transform: transform, isCustomTransform: isCustomTransform }; | ||
return { mode: mode, targetPath: targetPath, sourcePath: sourcePath, transform: transform, isCustomTransform: isCustomTransform, options: { alwaysSet: alwaysSet, alwaysTransform: alwaysTransform } }; | ||
} | ||
@@ -154,3 +169,4 @@ | ||
sourcePath = _ref.sourcePath, | ||
transform = _ref.transform; | ||
transform = _ref.transform, | ||
options = _ref.options; | ||
@@ -162,3 +178,3 @@ | ||
// Apply transform - will become optional | ||
if (this.exists_(value) || this.options.alwaysTransform === true) { | ||
if (this.exists_(value) || options.alwaysTransform === true) { | ||
value = transform(value); | ||
@@ -168,3 +184,3 @@ } | ||
// Set value on destination object | ||
if (this.exists_(value) || this.options.alwaysSet === true) { | ||
if (this.exists_(value) || options.alwaysSet === true) { | ||
return this.om.setKeyValue(destinationObject, targetPath, value); | ||
@@ -180,3 +196,4 @@ } | ||
transform = _ref2.transform, | ||
isCustomTransform = _ref2.isCustomTransform; | ||
isCustomTransform = _ref2.isCustomTransform, | ||
options = _ref2.options; | ||
@@ -205,13 +222,11 @@ | ||
// console.log("pre-transform value", values); | ||
let value; | ||
// Apply transform - will become optional | ||
if (anyValues || this.options.alwaysTransform === true) { | ||
// Apply transform if appropriate | ||
if (anyValues || options.alwaysTransform === true) { | ||
value = transform.apply(undefined, values); | ||
} | ||
// console.log("post-transform value", value); | ||
// Set value on destination object | ||
if (this.exists_(value) || this.options.alwaysSet === true) { | ||
if (this.exists_(value) || options.alwaysSet === true) { | ||
return this.om.setKeyValue(destinationObject, targetPath, value); | ||
@@ -227,3 +242,4 @@ } | ||
transform = _ref3.transform, | ||
isCustomTransform = _ref3.isCustomTransform; | ||
isCustomTransform = _ref3.isCustomTransform, | ||
options = _ref3.options; | ||
@@ -230,0 +246,0 @@ |
@@ -17,2 +17,10 @@ "use strict"; | ||
var _existingModifierSuite = require("./suites/existing-modifier-suite"); | ||
var _existingModifierSuite2 = _interopRequireDefault(_existingModifierSuite); | ||
var _alwaysModifierSuite = require("./suites/always-modifier-suite"); | ||
var _alwaysModifierSuite2 = _interopRequireDefault(_alwaysModifierSuite); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
@@ -107,3 +115,3 @@ | ||
group("when executing with options set the single source mapper", () => { | ||
group("when executing with options set, the single source mapper", () => { | ||
@@ -267,3 +275,3 @@ lab.test("suppresses a transform when the source value is not present", done => { | ||
group("when executing with options set the multi source mapper", () => { | ||
group("when executing with options set, the multi source mapper", () => { | ||
@@ -306,2 +314,73 @@ lab.test("suppresses a transform when the source values are all not present", done => { | ||
lab.test("suppresses a set if a transform returns null", done => { | ||
const source = { | ||
"my": { | ||
"source": { | ||
"is": {} | ||
}, | ||
"other": { | ||
"source": { | ||
"is": { | ||
"here": "value" | ||
} | ||
} | ||
} | ||
} | ||
}; | ||
const expected = {}; | ||
const map = (0, _index2.default)({ alwaysTransform: false, alwaysSet: false }); | ||
let count = 0; | ||
map(["my.source.is.missing", "my.other.source.is.here"]).to("your.source.is.missing", () => { | ||
count++; | ||
return null; | ||
}); | ||
const actual = map.execute(source); | ||
(0, _code.expect)(actual).to.equal(expected); | ||
(0, _code.expect)(count).to.equal(1); | ||
return done(); | ||
}); | ||
lab.test("suppresses a set if a transform returns undefined", done => { | ||
const source = { | ||
"my": { | ||
"source": { | ||
"is": {} | ||
}, | ||
"other": { | ||
"source": { | ||
"is": { | ||
"here": "value" | ||
} | ||
} | ||
} | ||
} | ||
}; | ||
const expected = {}; | ||
const map = (0, _index2.default)({ alwaysTransform: false, alwaysSet: false }); | ||
let count = 0; | ||
map(["my.source.is.missing", "my.other.source.is.here"]).to("your.source.is.missing", () => { | ||
count++; | ||
return undefined; | ||
}); | ||
const actual = map.execute(source); | ||
(0, _code.expect)(actual).to.equal(expected); | ||
(0, _code.expect)(count).to.equal(1); | ||
return done(); | ||
}); | ||
lab.test("a transform executes when one source value is present", done => { | ||
@@ -346,2 +425,65 @@ | ||
}); | ||
}); | ||
group("when executing with options set, the or-mode source map", () => { | ||
lab.test("a transform executes when one source value is present in or mode", done => { | ||
const source = { | ||
"my": { | ||
"source": { | ||
"is": { | ||
"here": "value" | ||
} | ||
}, | ||
"other": { | ||
"source": { | ||
"is": {} | ||
} | ||
} | ||
} | ||
}; | ||
const expected = { | ||
"your": { | ||
"source": { | ||
"is": { | ||
"here": "found" | ||
} | ||
} | ||
} | ||
}; | ||
const map = (0, _index2.default)({ alwaysTransform: false, alwaysSet: true }); | ||
map("my.source.is.here").or("my.other.source.is.missing").to("your.source.is.here", () => { | ||
return "found"; | ||
}); | ||
const actual = map.execute(source); | ||
(0, _code.expect)(actual).to.equal(expected); | ||
return done(); | ||
}); | ||
}); | ||
group("the always modifier", () => { | ||
_alwaysModifierSuite2.default.run(lab, { OPTIONS: null }); | ||
_alwaysModifierSuite2.default.run(lab, {}); | ||
_alwaysModifierSuite2.default.run(lab, { OPTIONS: { alwaysTransform: false, alwaysSet: false } }); | ||
_alwaysModifierSuite2.default.run(lab, { OPTIONS: { alwaysTransform: false, alwaysSet: true } }); | ||
_alwaysModifierSuite2.default.run(lab, { OPTIONS: { alwaysTransform: true, alwaysSet: false } }); | ||
_alwaysModifierSuite2.default.run(lab, { OPTIONS: { alwaysTransform: false, alwaysSet: true } }); | ||
}); | ||
group("the existing modifier", () => { | ||
_existingModifierSuite2.default.run(lab, { OPTIONS: null }); | ||
_existingModifierSuite2.default.run(lab, {}); | ||
_existingModifierSuite2.default.run(lab, { OPTIONS: { alwaysTransform: false, alwaysSet: false } }); | ||
_existingModifierSuite2.default.run(lab, { OPTIONS: { alwaysTransform: false, alwaysSet: true } }); | ||
_existingModifierSuite2.default.run(lab, { OPTIONS: { alwaysTransform: true, alwaysSet: false } }); | ||
_existingModifierSuite2.default.run(lab, { OPTIONS: { alwaysTransform: false, alwaysSet: true } }); | ||
}); |
{ | ||
"name": "map-factory", | ||
"version": "1.7.0", | ||
"version": "1.7.1", | ||
"description": "A simple object mapping utility that makes it easy to map data from one object to another. Create object mappers using fluent interface that supports deep references (dot notation), custom transformations, and object merging.", | ||
@@ -15,3 +15,3 @@ "main": "./dist/lib/index.js", | ||
"pretest": "npm run build", | ||
"test": "lab ./dist/test/ -v -S --assert code", | ||
"test": "./node_modules/lab/bin/lab ./dist/test/ -v -S --assert code", | ||
"pret": "npm run build", | ||
@@ -18,0 +18,0 @@ "t": "./test-one", |
174346
35
4738