map-factory
Advanced tools
Comparing version 1.2.3 to 1.3.0
@@ -0,1 +1,13 @@ | ||
### 1.2.3 | ||
Actually fixed the links in the docs | ||
### 1.2.2 | ||
Tried and failed to fix links in docs | ||
### 1.2.1 | ||
Improved documentation | ||
### 1.2.0 | ||
@@ -2,0 +14,0 @@ |
@@ -12,3 +12,4 @@ export interface IKeyDefinition { | ||
map(stringOrArray: string | string[]): IMapping; | ||
execute(source?: any, destination?: any): any; | ||
execute(source: any, destination?: any): any; | ||
each(sourceArray: any[]): any; | ||
} | ||
@@ -20,3 +21,4 @@ export interface IMapping { | ||
map(stringOrArray: string | string[]): IMapping; | ||
execute(source?: any, destination?: any): any; | ||
execute(source: any, destination?: any): any; | ||
each(sourceArray: any[]): any; | ||
} |
@@ -16,2 +16,5 @@ "use strict"; | ||
}.bind(me); | ||
mapper.each = function (sourceArray) { | ||
return this.mapper.each(sourceArray); | ||
}.bind(me); | ||
return mapper; | ||
@@ -18,0 +21,0 @@ } |
@@ -8,2 +8,3 @@ import { IMapping } from "./interfaces"; | ||
map(source: string | string[]): Mapping; | ||
each(sourceArray: any[]): any[]; | ||
execute(source: any, destination: any): any; | ||
@@ -10,0 +11,0 @@ private createMapData(); |
@@ -18,2 +18,14 @@ "use strict"; | ||
}; | ||
Mapper.prototype.each = function (sourceArray) { | ||
var _this = this; | ||
if (sourceArray === null || sourceArray === undefined) { | ||
throw new Error("A sourceArray object is required"); | ||
} | ||
if (Array.isArray(sourceArray) !== true) { | ||
throw new Error("The sourceArray parameter must be an array"); | ||
} | ||
return sourceArray.map(function (item) { | ||
return _this.execute(item, null); | ||
}); | ||
}; | ||
Mapper.prototype.execute = function (source, destination) { | ||
@@ -20,0 +32,0 @@ if (source === null || source === undefined) { |
@@ -9,3 +9,4 @@ import { IMapping, IKeyDefinition } from "./interfaces"; | ||
execute(source?: any, destination?: any): any; | ||
each(sourceArray: any): any; | ||
to(target: string, fnc?: Function): any; | ||
} |
@@ -16,2 +16,5 @@ "use strict"; | ||
}; | ||
Mapping.prototype.each = function (sourceArray) { | ||
return this.mapper.each(sourceArray); | ||
}; | ||
Mapping.prototype.to = function (target, fnc) { | ||
@@ -18,0 +21,0 @@ if (!target) { |
@@ -114,2 +114,25 @@ "use strict"; | ||
}, | ||
"provides the each() method to help work with arrays and multiple mappers": function (test) { | ||
var source = { | ||
one: [{ value: "a", drop: "me" }, { value: "b", drop: "me" }, { value: "c", drop: "me" }], | ||
two: [{ value: "a", drop: "me" }, { value: "b", drop: "me" }, { value: "c", drop: "me" }], | ||
three: [{ value: "a", drop: "me" }, { value: "b", drop: "me" }, { value: "c", drop: "me" }] | ||
}; | ||
var expected = { | ||
one: [{ item: "a" }, { item: "b" }, { item: "c" }], | ||
two: [{ item: "a" }, { item: "b" }, { item: "c" }], | ||
three: [{ item: "a" }, { item: "b" }, { item: "c" }] | ||
}; | ||
var mainMapper = createMapper(); | ||
var childMapper = createMapper(); | ||
childMapper | ||
.map("value").to("item"); | ||
mainMapper | ||
.map("one").to("one", function (array) { return childMapper.each(array); }) | ||
.map("two").to("two", function (array) { return childMapper.each(array); }) | ||
.map("three").to("three", function (array) { return childMapper.each(array); }); | ||
var actual = mainMapper.execute(source); | ||
test.deepEqual(actual, expected); | ||
return test.done(); | ||
}, | ||
"More complicated transformations can be handled by providing a function": function (test) { | ||
@@ -116,0 +139,0 @@ var expected = { |
@@ -19,2 +19,14 @@ "use strict"; | ||
return test.done(); | ||
}, | ||
"each method works from the index": function (test) { | ||
var source = [{ | ||
"fieldName": "name1" | ||
}]; | ||
var expected = [{ | ||
"fieldName": "name1" | ||
}]; | ||
var map = createMapper(); | ||
var actual = map("fieldName").each(source); | ||
test.deepEqual(actual, expected); | ||
return test.done(); | ||
} | ||
@@ -21,0 +33,0 @@ }; |
@@ -116,2 +116,76 @@ "use strict"; | ||
}; | ||
var eachGroup = { | ||
"Can process an array correctly": function (test) { | ||
var source = [{ | ||
"fieldName": "name1" | ||
}, { | ||
"fieldName": "name2" | ||
}]; | ||
var expected = [ | ||
{ | ||
"field": { | ||
"name": "name1" | ||
} | ||
}, | ||
{ | ||
"field": { | ||
"name": "name2" | ||
} | ||
}]; | ||
var map = map_factory_1["default"](); | ||
map("fieldName").to("field.name"); | ||
var actual = map.each(source); | ||
test.deepEqual(actual, expected); | ||
return test.done(); | ||
}, | ||
"An empty array does not cause an error": function (test) { | ||
var source = []; | ||
var expected = []; | ||
var map = map_factory_1["default"](); | ||
map("fieldName").to("field.name"); | ||
var actual = map.each(source); | ||
test.deepEqual(actual, expected); | ||
return test.done(); | ||
}, | ||
"Multiple mappers can be used together": function (test) { | ||
var source = { | ||
one: [{ value: "a", drop: "me" }, { value: "b", drop: "me" }, { value: "c", drop: "me" }], | ||
two: [{ value: "a", drop: "me" }, { value: "b", drop: "me" }, { value: "c", drop: "me" }], | ||
three: [{ value: "a", drop: "me" }, { value: "b", drop: "me" }, { value: "c", drop: "me" }] | ||
}; | ||
var expected = { | ||
one: [{ newOne: "a" }, { newOne: "b" }, { newOne: "c" }], | ||
two: [{ newOne: "a" }, { newOne: "b" }, { newOne: "c" }], | ||
three: [{ newOne: "a" }, { newOne: "b" }, { newOne: "c" }] | ||
}; | ||
var mainMapper = map_factory_1["default"](); | ||
var childMapper = map_factory_1["default"](); | ||
childMapper | ||
.map("value").to("newOne"); | ||
mainMapper | ||
.map("one").to("one", function (array) { return childMapper.each(array); }) | ||
.map("two").to("two", function (array) { return childMapper.each(array); }) | ||
.map("three").to("three", function (array) { return childMapper.each(array); }); | ||
var actual = mainMapper.execute(source); | ||
test.deepEqual(actual, expected); | ||
return test.done(); | ||
}, | ||
"A null parameter throws an error": function (test) { | ||
var map = map_factory_1["default"](); | ||
map("fieldName").to("field.name"); | ||
test.throws(function () { | ||
var actual = map.each(null); | ||
}); | ||
return test.done(); | ||
}, | ||
"A non-array throws an error": function (test) { | ||
var map = map_factory_1["default"](); | ||
var source = { "a": "b" }; | ||
map("fieldName").to("field.name"); | ||
test.throws(function () { | ||
var actual = map.each(source); | ||
}); | ||
return test.done(); | ||
} | ||
}; | ||
var defaultGroup = { | ||
@@ -133,15 +207,18 @@ "Can map one field that exists to another": function (test) { | ||
}, | ||
"Throws if no source is provided": function (test) { | ||
var expected = { | ||
"field": { | ||
"name": "name1" | ||
} | ||
}; | ||
"Throws if a null source is provided": function (test) { | ||
var map = map_factory_1["default"](); | ||
map("fieldName").to("field.name"); | ||
test.throws(function () { | ||
var actual = map.execute(); | ||
var actual = map.execute(null); | ||
}); | ||
return test.done(); | ||
}, | ||
"Throws if an undefined source is provided": function (test) { | ||
var map = map_factory_1["default"](); | ||
map("fieldName").to("field.name"); | ||
test.throws(function () { | ||
var actual = map.execute(undefined); | ||
}); | ||
return test.done(); | ||
}, | ||
"Can reuse the map for multiple transforms": function (test) { | ||
@@ -376,2 +453,3 @@ var source = { | ||
exports.fluentChaining = fluentChainingGroup; | ||
exports.each = eachGroup; | ||
//# sourceMappingURL=map-factory-test.js.map |
{ | ||
"name": "map-factory", | ||
"version": "1.2.3", | ||
"version": "1.3.0", | ||
"description": "Object mapping tool", | ||
@@ -5,0 +5,0 @@ "main": "./dist/lib/index.js", |
@@ -56,3 +56,3 @@ # map-factory | ||
const result = map.execute(source); | ||
const result = mapper.execute(source); | ||
``` | ||
@@ -192,2 +192,35 @@ | ||
``` | ||
**map-factory** also provides the ```each()``` method to help work with arrays and multiple mappers. This is useful when child objects within your main object have the same structure. | ||
```js | ||
const createMapper = require("map-factory"); | ||
const assert = require("assert"); | ||
const source = { | ||
one: [{value: "a", drop: "me" }, {value: "b", drop: "me" }, {value: "c", drop: "me" }], | ||
two: [{value: "a", drop: "me" }, {value: "b", drop: "me" }, {value: "c", drop: "me" }], | ||
three: [{value: "a", drop: "me" }, {value: "b", drop: "me" }, {value: "c", drop: "me" }] | ||
}; | ||
const mainMapper = createMapper(); | ||
const childMapper = createMapper(); | ||
childMapper | ||
.map("value").to("item"); | ||
mainMapper | ||
.map("one").to("one", array => childMapper.each(array)) | ||
.map("two").to("two", array => childMapper.each(array)) | ||
.map("three").to("three", array => childMapper.each(array)); | ||
const actual = mainMapper.execute(source); | ||
assert.deepEqual(actual, { | ||
one: [{item: "a" }, {item: "b" }, {item: "c" }], | ||
two: [{item: "a" }, {item: "b" }, {item: "c" }], | ||
three: [{item: "a" }, {item: "b" }, {item: "c" }] | ||
}); | ||
``` | ||
### Transformations | ||
@@ -194,0 +227,0 @@ More complicated transformations can be handled by providing a function. The selected source data will be passed to the function. |
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
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
80008
999
526