diff --git a/src/property-mapper.js b/src/property-mapper.js index 5975442..9b57540 100644 --- a/src/property-mapper.js +++ b/src/property-mapper.js @@ -23,14 +23,37 @@ module.exports = function createPropertyMapper(propertyMap) { destinationProperty: destinationProperty } } - }); + }).reduce((mappings, item) => { + mappings[item.sourceProperty] = item; + return mappings; + }, {}); return function mapProperties(properties) { - return mappings.map((mapping) => { - if (properties[mapping.sourceProperty] != null) { + return Object.keys(properties).map((propertyName) => { + let propertyValue = properties[propertyName]; + + if (propertyName.includes(".")) { + /* Property names with a dot are attempts to directly access an + * object within a composite object; they need to be handled + * specially since they essentially bypass the mapping step. + * If there is *more* than a single dot, then this is an attempt + * to access a *nested* composite object, and therefore we should + * keep the entire property name after the first dot intact. */ + + let propertyNameSegments = propertyName.split("."); + let destinationObject = propertyNameSegments[0]; + let realPropertyName = propertyNameSegments.slice(1).join("."); + + return { + sourceProperty: propertyName, + destinationObject: destinationObject, + destinationProperty: realPropertyName, + value: propertyValue + } + } else if (mappings[propertyName] != null) { return Object.assign({ - value: properties[mapping.sourceProperty] - }, mapping); + value: propertyValue + }, mappings[propertyName]); } }).filter((item) => { return (item != null); @@ -76,7 +99,8 @@ let mapper = module.exports({ console.log(mapper({ foo: "hello one foo!", - bar: "hello bar!", + bar: "hello two bar!", twoFoo: "hello two foo!", - baz: "hello baz!" + baz: "hello baz!", + "one.bar": "hello one bar!" }));