-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbase.mapper.ts
More file actions
284 lines (259 loc) · 8.28 KB
/
base.mapper.ts
File metadata and controls
284 lines (259 loc) · 8.28 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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
import dateUtils from '@/utilities/date-utils';
export enum EnumPropertyMappingType {
Date_ApiToUi = 1,
Date_UiToApi = 2,
Date_ToApi = 3,
String_ToLowercase = 4,
String_ToCapitalize = 5,
String_ToUpparcase = 6,
}
/**
* Creates a mapper between two types
*/
export interface PropertyMappingOptions {
ignore?: boolean;
equalsTo?: string;
mappingType?: EnumPropertyMappingType;
mapperFunction?(source: any): any;
}
export class BaseMapper<Source, Target> {
protected propertyMapperOptions: {
[key: string]: PropertyMappingOptions;
} = {};
protected reversePropertyMapperOptions: {
[key: string]: PropertyMappingOptions;
} = {};
protected oneToOnePropKeys: string[] = [];
/**
* İf this is set to true maps all source props to
* target if there is no mapperOption set for that prop
*/
protected mapAllPropsOneToOne?: boolean = false;
constructor(mapAllPropsOneToOne?: boolean) {
this.mapAllPropsOneToOne = mapAllPropsOneToOne;
}
oneToOneProps(...propKeys: string[]) {
if (propKeys) this.oneToOnePropKeys = propKeys;
}
/**
* adds new mapper option for property
* @param propKey target prop key
* @param mapFrom map prop with this options
* @param mappingType for special behavior
*/
forProp(propKey: string, mapFrom: string | ((source: Source) => any), mappingType?: EnumPropertyMappingType) {
//TODO: if mapFrom and mappingType exists create reverse mapFrom with revers mappingType
if (typeof mapFrom === "string") {
this.propertyMapperOptions[propKey] = { equalsTo: mapFrom, mappingType };
} else if (mapFrom instanceof Function) {
this.propertyMapperOptions[propKey] = { mapperFunction: mapFrom, mappingType };
} else {
throw new Error(
"for key " + propKey + " mapFrom is not string or function !"
);
}
}
/**
* adds new mapper option for property
* @param propKey source prop key
* @param mapFrom map prop with this options
* @param mappingType for special behavior
*/
forPropReverse(propKey: string, mapFrom: string | ((source: Target) => any), mappingType?: EnumPropertyMappingType) {
if (typeof mapFrom === "string") {
this.reversePropertyMapperOptions[propKey] = { equalsTo: mapFrom, mappingType };
} else if (mapFrom instanceof Function) {
this.reversePropertyMapperOptions[propKey] = { mapperFunction: mapFrom, mappingType };
}
}
/**
* Ignore property of source object
* @param propKey property of source to be ignored
*/
ignoreProp(propKey: string) {
this.propertyMapperOptions[propKey] = { ignore: true };
}
/**
* Ignore property of source object
* @param propKey property of source to be ignored
*/
ignorePropReverse(propKey: string) {
this.reversePropertyMapperOptions[propKey] = { ignore: true };
}
/**
* Map prop using pre defined mapping options. If there is no
* option for that prop it will be mapped directly.
* @param source source object that will be mapped from
*/
map = (source: Source): Target => {
let target: any = this.mapDefault(source);
target = this.mapWithOptions(source, target);
return target;
};
/**
* Map prop using pre defined mapping options. If there is no
* option for that prop it will be mapped directly.
* @param source source object that will be mapped from
*/
mapReverse = (source: Target): Source => {
console.log('TCL: source', source)
let target: any = this.mapDefault(source, true);
target = this.mapWithOptions(source, target, true);
return target;
}
/**
* maps source object to target with same property keys
* if there is no predefined mapping options exists
* @param source Source object
* @param reverse İs reverse mapping
*/
protected mapDefault = <Type1, Type2>(
source: Type1,
reverse?: boolean
): Type2 => {
let target: any = {};
try {
let options = reverse
? this.reversePropertyMapperOptions
: this.propertyMapperOptions;
if (this.mapAllPropsOneToOne) {
for (var key in source) {
let propMapperOption = options[key];
if (!propMapperOption) {
target[key] = this.mapWithKeyString(key, source);
}
}
}
else {
this.oneToOnePropKeys.forEach(key => {
let propMapperOption = options[key];
if (!propMapperOption) {
target[key] = this.mapWithKeyString(key, source);
}
});
}
} catch (error) {
console.error(error);
}
return target;
};
/**
* Map with options
* @param source source object
* @param target mapped object
* @param reverse is reverse mapping
*/
protected mapWithOptions<T1, T2>(
source: T1,
target: T2,
reverse?: boolean
): T2 {
try {
let options = reverse
? this.reversePropertyMapperOptions
: this.propertyMapperOptions;
for (let key in options) {
let propMapperOption = options[key];
if (!propMapperOption.ignore) {
if (!!propMapperOption.equalsTo) {
(target as any)[key] = this.mapWithKeyString(
propMapperOption.equalsTo,
source
);
if (!!propMapperOption.mappingType) {
(target as any)[key] = this.mapWithMappingTypes(
(target as any)[key],
propMapperOption.mappingType);
}
} else if (propMapperOption.mapperFunction) {
(target as any)[key] = propMapperOption.mapperFunction(source);
}
}
}
return target;
} catch (error) {
console.error(error);
return target;
}
}
/**
* Map with using equalsTo string , if it containes '.' notations
* try to go deep in source object
* @param equalsTo source parameter key that will be mapped
* @param source source object
*/
protected mapWithKeyString<T1>(key: string, source: T1): any {
if (key.indexOf(".") > -1) {
let hierarchies = key.split(".");
let hierarchiesLength = hierarchies.length;
try {
if (hierarchiesLength > 1) {
let target: any = source;
for (let index = 0; index < hierarchiesLength; index++) {
if (target != undefined) {
target = (target as any)[hierarchies[index]];
}
}
if (target != undefined) return target;
}
} catch (error) {
console.error(error);
}
}
return (source as any)[key];
}
/**
* Map param with different predefined options
* @param sourceValue Source Property Value
* @param mappingType Mapping type for different mapping option
*/
protected mapWithMappingTypes(sourceValue: any, mappingType: EnumPropertyMappingType) {
try {
switch (mappingType) {
case EnumPropertyMappingType.Date_ApiToUi:
return dateUtils.dateFormatFromApiDateString(sourceValue);
case EnumPropertyMappingType.Date_ToApi:
return dateUtils.dateToApiDate(sourceValue);
case EnumPropertyMappingType.Date_UiToApi:
return dateUtils.dateStringToApiDate(sourceValue);
case EnumPropertyMappingType.String_ToLowercase:
return (sourceValue as string).toLocaleLowerCase();
case EnumPropertyMappingType.String_ToUpparcase:
return (sourceValue as string).toUpperCase();
case EnumPropertyMappingType.String_ToCapitalize:
return (sourceValue as string).charAt(0).toUpperCase() + (sourceValue as string).substr(1);
default:
return sourceValue;
break;
}
} catch (error) {
console.error(error);
}
}
mapArray = (sources: Source[]): Target[] => {
let targets: any[] = sources.map(this.map);
return targets;
};
mapArrayReverse = (sources: Target[]): Source[] => {
let targets: any[] = sources.map(this.mapReverse);
return targets;
};
mapDictionary = (sources: {
[key: string]: Source;
}): { [key: string]: Target } => {
let targets: any = {};
for (let key in sources) {
targets[key] = this.map(sources[key]);
}
return targets;
};
mapDictionaryReverse = (sources: {
[key: string]: Target;
}): { [key: string]: Source } => {
let targets: any = {};
for (let key in sources) {
targets[key] = this.mapReverse(sources[key]);
}
return targets;
};
}