-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathjsAutomapper.js
More file actions
124 lines (114 loc) · 5.23 KB
/
Copy pathjsAutomapper.js
File metadata and controls
124 lines (114 loc) · 5.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
var automapper = (function (app) {
if(app.automapper) {
return app.automapper;
}
var dictionary = {};
app.automapper = {
createMap : function (sourceKey, destinationKey) {
var combinedKey = sourceKey + destinationKey, functions;
dictionary[combinedKey] = {};
functions = {
forMember : function (key, e) {
dictionary[combinedKey][key] = e;
return functions;
},
forAllMembers : function(func) {
dictionary[combinedKey].__forAllMembers = func;
return functions;
}
};
return functions;
},
map : function (sourceKey, destinationKey, sourceValue, destinationValue, lazy) {
if(!sourceValue && sourceValue !== false) {
return;
}
function getValue(item) {
if(typeof item === "function" && !lazy) {
return item();
}
return item;
}
var combinedKey = sourceKey + destinationKey;
var mappings = dictionary[combinedKey], output, key,
extensions = {
ignore : function () {
// don't do anything
},
mapFrom : function (sourceMemberKey) {
if (!this.__sourceValue.hasOwnProperty(sourceMemberKey)) {
throw sourceKey + "." + sourceMemberKey + " has not been defined.";
}
var value = getValue(this.__sourceValue[sourceMemberKey]);
if(mappings.__forAllMembers) {
mappings.__forAllMembers(this.__destinationValue, this.__key, value);
} else {
this.__destinationValue[this.__key] = value;
}
}
};
if(!mappings) {
throw "Could not find mapping with a source of " + sourceKey + " and a destination of " + destinationKey;
}
function mapItem(destinationValue, sourceValue) {
for (key in destinationValue) {
if (!destinationValue.hasOwnProperty(key)) {
continue;
}
if (mappings.hasOwnProperty(key) && mappings[key]) {
if (typeof mappings[key] === "function") {
extensions.__key = key;
extensions.__sourceValue = sourceValue;
extensions.__destinationValue = destinationValue;
output = mappings[key].call(extensions);
} else { // forMember second parameter was not a function
output = mappings[key];
}
// object was returned from the 'forMember' call
if (output) {
var value = getValue(output);
if(mappings.__forAllMembers) {
mappings.__forAllMembers(destinationValue, key, value);
} else {
destinationValue[key] = value;
}
}
}
else if (!sourceValue.hasOwnProperty(key)) {
throw sourceKey + "." + key + " has not been defined.";
} else {
var value = getValue(sourceValue[key]);
if(mappings.__forAllMembers) {
mappings.__forAllMembers(destinationValue, key, value);
} else {
destinationValue[key] = value;
}
}
}
}
// actually do the mapping here
if(sourceValue instanceof Array) {
if(destinationValue instanceof Array) {
// loop
for(var i = 0; i < sourceValue.length; i += 1) {
if(!destinationValue[i]) {
if(typeof destinationKey !== "function") {
throw "destinationKey of mapping must be a function in order to initialize the array";
}
destinationValue[i] = destinationKey();
}
mapItem(destinationValue[i], sourceValue[i]);
}
} else {
throw "Cannot map array to object";
}
}
else if(destinationValue instanceof Array) {
throw "Cannot map object to array";
} else {
mapItem(destinationValue, sourceValue);
}
}
};
return app.automapper;
}(this));