map-factory
Advanced tools
Comparing version 1.0.1 to 1.1.0
@@ -0,1 +1,9 @@ | ||
### 1.1.0 | ||
Added feature to ```map.execute``` which allows you to provide an existing object as the target object. | ||
### 1.0.1 | ||
Previous did not publish properly. | ||
### 1.0.0 | ||
@@ -2,0 +10,0 @@ |
export interface IMapFactory { | ||
(stringOrArray: string | string[]): IMapping; | ||
execute(obj?: any): any; | ||
execute(source?: any, destination?: any): any; | ||
} | ||
@@ -5,0 +5,0 @@ export interface IMapping { |
@@ -13,4 +13,4 @@ "use strict"; | ||
}.bind(me); | ||
map.execute = function (obj) { | ||
return this.mapper.execute(obj); | ||
map.execute = function (source, destination) { | ||
return this.mapper.execute(source, destination); | ||
}.bind(me); | ||
@@ -17,0 +17,0 @@ return map; |
@@ -7,5 +7,5 @@ import { IMapping } from "./interfaces"; | ||
registerMapping(mapping: IMapping): void; | ||
execute(sourceObject: any): any; | ||
execute(source: any, destination: any): any; | ||
private createMapData(); | ||
private appendMultiSelections(source, target, multiMaps); | ||
} |
@@ -12,11 +12,14 @@ "use strict"; | ||
}; | ||
Mapper.prototype.execute = function (sourceObject) { | ||
if (sourceObject === null || sourceObject === undefined) { | ||
throw new Error("sourceObject is required"); | ||
Mapper.prototype.execute = function (source, destination) { | ||
if (source === null || source === undefined) { | ||
throw new Error("A source object is required"); | ||
} | ||
if (destination === null || destination === undefined) { | ||
destination = {}; | ||
} | ||
if (this.mapCache === null) { | ||
this.mapCache = this.createMapData(); | ||
} | ||
var output = objectMapper(sourceObject, {}, this.mapCache.transform); | ||
return this.appendMultiSelections(sourceObject, output, this.mapCache.multiMaps); | ||
var output = objectMapper(source, destination, this.mapCache.transform); | ||
return this.appendMultiSelections(source, output, this.mapCache.multiMaps); | ||
}; | ||
@@ -23,0 +26,0 @@ Mapper.prototype.createMapData = function () { |
@@ -164,2 +164,28 @@ "use strict"; | ||
}, | ||
"An existing object can be provided as the target object": function (test) { | ||
var expected = { | ||
"field": { | ||
"name": "name1", | ||
"id": "123" | ||
}, | ||
"existing": "data" | ||
}; | ||
// Start example | ||
var source = { | ||
"fieldName": "name1", | ||
"fieldId": "123", | ||
"fieldDescription": "description" | ||
}; | ||
var destination = { | ||
"existing": "data" | ||
}; | ||
var map = createMapper(); | ||
map("fieldName").to("field.name"); | ||
map("fieldId").to("field.id"); | ||
var result = map.execute(source, destination); | ||
console.log(result); | ||
// End example | ||
test.deepEqual(result, expected); | ||
test.done(); | ||
}, | ||
"Select from multiple sources at once": function (test) { | ||
@@ -166,0 +192,0 @@ var expected = { |
@@ -127,2 +127,43 @@ "use strict"; | ||
}; | ||
var sourceAndDestinationGroup = { | ||
"Can map fields from a source onto an existing destination object": function (test) { | ||
var source = { | ||
"fieldName": "name1" | ||
}; | ||
var destination = { | ||
"existing": "field" | ||
}; | ||
var expected = { | ||
"field": { | ||
"name": "name1" | ||
}, | ||
"existing": "field" | ||
}; | ||
var map = map_factory_1["default"](); | ||
map("fieldName").to("field.name"); | ||
var actual = map.execute(source, destination); | ||
test.deepEqual(actual, expected); | ||
test.done(); | ||
}, | ||
"Can map a field from source over an existing field on a destination object": function (test) { | ||
var source = { | ||
"fieldName": "name1" | ||
}; | ||
var destination = { | ||
"field": { | ||
"name": "wrong" | ||
} | ||
}; | ||
var expected = { | ||
"field": { | ||
"name": "name1" | ||
} | ||
}; | ||
var map = map_factory_1["default"](); | ||
map("fieldName").to("field.name"); | ||
var actual = map.execute(source, destination); | ||
test.deepEqual(actual, expected); | ||
test.done(); | ||
} | ||
}; | ||
var customFunctionsGroup = { | ||
@@ -217,4 +258,5 @@ "Calls a function and alters the resulting object": function (test) { | ||
exports.basicMapping = basicMappingGroup; | ||
exports.sourceAndDestination = sourceAndDestinationGroup; | ||
exports.customFunctions = customFunctionsGroup; | ||
exports.multipleSelection = multipleSelectionGroup; | ||
//# sourceMappingURL=map-factory-test.js.map |
{ | ||
"name": "map-factory", | ||
"version": "1.0.1", | ||
"version": "1.1.0", | ||
"description": "Object mapping tool", | ||
@@ -18,7 +18,7 @@ "main": "./dist/lib/index.js", | ||
"premajor": "npm run build", | ||
"major": "npm version major -m \"published to npm as v%s\" && git push --all && git push --tags && npm publish", | ||
"major": "npm version major -m \"published to npm as v%s\" && git push --follow-tags && npm publish", | ||
"preminor": "npm run build", | ||
"minor": "npm version minor -m \"published to npm as v%s\" && git push --all && git push --tags && npm publish", | ||
"minor": "npm version minor -m \"published to npm as v%s\" && git push --follow-tags && npm publish", | ||
"prepatch": "npm run build", | ||
"patch": "npm version patch -m \"published to npm as v%s\" && git push --all && git push --tags && npm publish" | ||
"patch": "npm version patch -m \"published to npm as v%s\" && git push --follow-tags && npm publish" | ||
}, | ||
@@ -25,0 +25,0 @@ "bugs": { |
@@ -206,2 +206,38 @@ # map-factory | ||
An existing object can be provided as the target object. | ||
```js | ||
const createMapper = require("map-factory"); | ||
const source = { | ||
"fieldName": "name1", | ||
"fieldId": "123", | ||
"fieldDescription": "description" | ||
}; | ||
const destination = { | ||
"existing": "data" | ||
}; | ||
const map = createMapper(); | ||
map("fieldName").to("field.name"); | ||
map("fieldId").to("field.id"); | ||
const result = map.execute(source, destination); | ||
console.log(result); | ||
/* | ||
{ | ||
"existing": "data", | ||
"field": { | ||
"name": "name1", | ||
"id": "123" | ||
} | ||
} | ||
*/ | ||
``` | ||
### Select from multiple sources at once | ||
@@ -208,0 +244,0 @@ You can also provide an array or source fields and they can be extracted together. You must provide a transform for the target field. |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
44882
642
276
0