mapper-factory 2.0.2 → 3.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/class.decorator.d.ts +1 -1
- package/dist/example.js +117 -104
- package/dist/functions.js +100 -192
- package/dist/index.d.ts +1 -2
- package/dist/index.js +1 -3
- package/dist/mapper-functions.d.ts +1 -1
- package/dist/mapper-functions.js +3 -3
- package/dist/mapper.d.ts +157 -13
- package/dist/mapper.js +281 -199
- package/dist/mapper.to-remove.d.ts +190 -0
- package/dist/mapper.to-remove.js +371 -0
- package/dist/test.d.ts +1 -0
- package/dist/test.js +200 -0
- package/package.json +12 -3
package/dist/mapper.js
CHANGED
|
@@ -10,203 +10,299 @@ class MapperFactory {
|
|
|
10
10
|
*/
|
|
11
11
|
constructor(object) {
|
|
12
12
|
const metadataList = (0, field_decorator_1.getMapFieldMetadataList)(this);
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
13
|
+
if (object) {
|
|
14
|
+
this.mapObject(object, metadataList);
|
|
15
|
+
}
|
|
16
|
+
this.initializeProperties(metadataList, object);
|
|
17
|
+
console.log('Final state of this in MapperFactory:', this);
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Maps the properties of an object based on the provided metadata list.
|
|
21
|
+
*
|
|
22
|
+
* @param object - The object whose properties need to be mapped.
|
|
23
|
+
* @param metadataList - The list of metadata used to map the object's properties.
|
|
24
|
+
*
|
|
25
|
+
* This method iterates over each property of the given object. For each property, it retrieves the corresponding metadata keys.
|
|
26
|
+
* If metadata keys are found, it maps the property using the `mapProperty` method for each metadata key.
|
|
27
|
+
* If no metadata keys are found, it maps the property directly using the `mapDirectProperty` method.
|
|
28
|
+
*/
|
|
29
|
+
mapObject(object, metadataList) {
|
|
30
|
+
Object.keys(object).forEach(propertyName => {
|
|
31
|
+
const metaKeys = this.getMetaKeys(metadataList, propertyName);
|
|
32
|
+
if (metaKeys.length) {
|
|
16
33
|
metaKeys.forEach(metaKey => {
|
|
17
|
-
|
|
18
|
-
if (metaProp) {
|
|
19
|
-
let props = metaProp.src?.split('.');
|
|
20
|
-
let propsStereoid = props.map(prop => {
|
|
21
|
-
let index = prop.indexOf('[');
|
|
22
|
-
return {
|
|
23
|
-
prop: index > 0 ? prop.substring(0, index) : prop,
|
|
24
|
-
isArray: prop.includes('[') && prop.includes(']'),
|
|
25
|
-
arrIndex: prop.substring(index),
|
|
26
|
-
};
|
|
27
|
-
});
|
|
28
|
-
let i;
|
|
29
|
-
let objCopy = { ...object };
|
|
30
|
-
for (i = 0; i < propsStereoid.length; i++) {
|
|
31
|
-
if (propsStereoid[i].isArray) {
|
|
32
|
-
let arrIndex = propsStereoid[i].arrIndex?.split(/\[(\w+)\]/g)?.filter(index => index !== '');
|
|
33
|
-
objCopy = objCopy[propsStereoid[i].prop];
|
|
34
|
-
arrIndex.forEach((index, i) => {
|
|
35
|
-
objCopy = objCopy[index];
|
|
36
|
-
});
|
|
37
|
-
}
|
|
38
|
-
else {
|
|
39
|
-
objCopy = objCopy[propsStereoid[i].prop];
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
if (metaProp?.transformer) {
|
|
43
|
-
this[metaKey] = metaProp.transformer(objCopy, object);
|
|
44
|
-
}
|
|
45
|
-
else {
|
|
46
|
-
this[metaKey] = objCopy;
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
else {
|
|
50
|
-
let metaKey = metadataList && Object.keys(metadataList).find(metadata => metadataList[metadata]?.src == propertyName);
|
|
51
|
-
if (metaKey) {
|
|
52
|
-
const src = metadataList[metaKey].src || propertyName;
|
|
53
|
-
if (metadataList[metaKey].transformer) {
|
|
54
|
-
this[metaKey] = metadataList[metaKey].transformer(object[src], object);
|
|
55
|
-
}
|
|
56
|
-
else {
|
|
57
|
-
this[metaKey] = object[src];
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
else {
|
|
61
|
-
if (metadataList[propertyName]?.transformer) {
|
|
62
|
-
this[propertyName] = metadataList[propertyName].transformer(object[propertyName], object);
|
|
63
|
-
}
|
|
64
|
-
else {
|
|
65
|
-
this[propertyName] = object[propertyName];
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
}
|
|
34
|
+
this.mapProperty(object, metadataList, propertyName, metaKey);
|
|
69
35
|
});
|
|
70
36
|
}
|
|
71
37
|
else {
|
|
72
|
-
|
|
73
|
-
if (metaKey) {
|
|
74
|
-
const src = metadataList[metaKey].src || propertyName;
|
|
75
|
-
if (metadataList[metaKey].transformer) {
|
|
76
|
-
this[metaKey] = metadataList[metaKey].transformer(object[src], object);
|
|
77
|
-
}
|
|
78
|
-
else {
|
|
79
|
-
this[metaKey] = object[src];
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
else {
|
|
83
|
-
if (metadataList && metadataList[propertyName]?.transformer) {
|
|
84
|
-
this[propertyName] = metadataList[propertyName].transformer(object[propertyName], object);
|
|
85
|
-
}
|
|
86
|
-
else {
|
|
87
|
-
this[propertyName] = object[propertyName];
|
|
88
|
-
}
|
|
89
|
-
}
|
|
38
|
+
this.mapDirectProperty(object, metadataList, propertyName);
|
|
90
39
|
}
|
|
91
40
|
});
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Retrieves the metadata keys associated with a given property name.
|
|
44
|
+
*
|
|
45
|
+
* @param metadataList - The list of metadata objects to search through.
|
|
46
|
+
* @param propertyName - The name of the property to find metadata keys for.
|
|
47
|
+
* @returns An array of metadata keys that are associated with the specified property name.
|
|
48
|
+
*/
|
|
49
|
+
getMetaKeys(metadataList, propertyName) {
|
|
50
|
+
return metadataList ? Object.keys(metadataList).filter(metadata => metadataList[metadata]?.src?.split('.')?.includes(propertyName)) : [];
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Maps a property from the given object to the current instance based on the provided metadata.
|
|
54
|
+
*
|
|
55
|
+
* @param object - The source object containing the property to be mapped.
|
|
56
|
+
* @param metadataList - A list of metadata that provides mapping information.
|
|
57
|
+
* @param propertyName - The name of the property to be mapped.
|
|
58
|
+
* @param metaKey - The key used to retrieve the specific metadata for the property.
|
|
59
|
+
*
|
|
60
|
+
* This method performs the following steps:
|
|
61
|
+
* 1. Retrieves the metadata for the given metaKey.
|
|
62
|
+
* 2. If metadata exists, it gets the properties' stereoid and creates a copy of the object.
|
|
63
|
+
* 3. Iterates over the properties' stereoid and updates the object copy.
|
|
64
|
+
* 4. If a transformer function is defined in the metadata, it applies the transformer to the object copy and the original object.
|
|
65
|
+
* 5. If no metadata exists for the metaKey, it maps the property directly using the `mapDirectProperty` method.
|
|
66
|
+
*/
|
|
67
|
+
mapProperty(object, metadataList, propertyName, metaKey) {
|
|
68
|
+
const metaProp = metadataList[metaKey];
|
|
69
|
+
if (metaProp) {
|
|
70
|
+
const propsStereoid = this.getPropsStereoid(metaProp.src);
|
|
71
|
+
let objCopy = { ...object };
|
|
72
|
+
propsStereoid.forEach(prop => {
|
|
73
|
+
objCopy = this.getObjectCopy(objCopy, prop);
|
|
74
|
+
});
|
|
75
|
+
this[metaKey] = metaProp.transformer ? metaProp.transformer(objCopy, object) : objCopy;
|
|
76
|
+
}
|
|
77
|
+
else {
|
|
78
|
+
this.mapDirectProperty(object, metadataList, propertyName);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Parses a dot-separated string and returns an array of PropStereoid objects.
|
|
83
|
+
* Each PropStereoid object contains information about the property name,
|
|
84
|
+
* whether it is an array, and the array index if applicable.
|
|
85
|
+
*
|
|
86
|
+
* @param src - The dot-separated string to parse.
|
|
87
|
+
* @returns An array of PropStereoid objects.
|
|
88
|
+
*/
|
|
89
|
+
getPropsStereoid(src) {
|
|
90
|
+
return src.split('.').map(prop => {
|
|
91
|
+
const index = prop.indexOf('[');
|
|
92
|
+
return {
|
|
93
|
+
prop: index > 0 ? prop.substring(0, index) : prop,
|
|
94
|
+
isArray: prop.includes('[') && prop.includes(']'),
|
|
95
|
+
arrIndex: prop.substring(index),
|
|
96
|
+
};
|
|
97
97
|
});
|
|
98
98
|
}
|
|
99
99
|
/**
|
|
100
|
-
*
|
|
100
|
+
* Retrieves a copy of an object property based on the provided `PropStereoid`.
|
|
101
|
+
*
|
|
102
|
+
* @param objCopy - The object from which the property copy is to be retrieved.
|
|
103
|
+
* @param prop - The `PropStereoid` object containing property details.
|
|
104
|
+
* @returns A copy of the specified property from the object.
|
|
105
|
+
*
|
|
106
|
+
* @remarks
|
|
107
|
+
* If the property is an array, the method navigates through the array indices
|
|
108
|
+
* specified in `prop.arrIndex` to retrieve the nested property.
|
|
109
|
+
* Otherwise, it directly retrieves the property specified in `prop.prop`.
|
|
110
|
+
*
|
|
111
|
+
* @example
|
|
112
|
+
* ```typescript
|
|
113
|
+
* const obj = { a: { b: [ { c: 1 }, { c: 2 } ] } };
|
|
114
|
+
* const prop = { isArray: true, arrIndex: '[1]', prop: 'b' };
|
|
115
|
+
* const result = getObjectCopy(obj, prop);
|
|
116
|
+
* console.log(result); // Output: { c: 2 }
|
|
117
|
+
* ```
|
|
118
|
+
*/
|
|
119
|
+
getObjectCopy(objCopy, prop) {
|
|
120
|
+
if (prop.isArray) {
|
|
121
|
+
const arrIndex = prop.arrIndex.split(/\[(\w+)\]/g).filter(index => index !== '');
|
|
122
|
+
objCopy = objCopy[prop.prop];
|
|
123
|
+
arrIndex.forEach(index => {
|
|
124
|
+
objCopy = objCopy[index];
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
else {
|
|
128
|
+
objCopy = objCopy[prop.prop];
|
|
129
|
+
}
|
|
130
|
+
return objCopy;
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* Maps a direct property from the given object to the current instance based on the provided metadata list.
|
|
101
134
|
*
|
|
102
|
-
* @
|
|
135
|
+
* @param object - The source object containing the properties to be mapped.
|
|
136
|
+
* @param metadataList - An optional list of metadata that defines how properties should be mapped and transformed.
|
|
137
|
+
* @param propertyName - The name of the property to be mapped from the source object.
|
|
138
|
+
*
|
|
139
|
+
* The method performs the following steps:
|
|
140
|
+
* 1. Checks if the metadata list contains a key that matches the source property name.
|
|
141
|
+
* 2. If a matching metadata key is found, it uses the metadata to determine the source property name (`src`) and applies any defined transformer function.
|
|
142
|
+
* 3. If no matching metadata key is found, it directly maps the property from the source object, applying any transformer function if defined.
|
|
143
|
+
*/
|
|
144
|
+
mapDirectProperty(object, metadataList, propertyName) {
|
|
145
|
+
const metaKey = metadataList ? Object.keys(metadataList).find(metadata => metadataList[metadata]?.src === propertyName) : null;
|
|
146
|
+
if (metaKey) {
|
|
147
|
+
const src = metadataList[metaKey].src || propertyName;
|
|
148
|
+
this[metaKey] = metadataList[metaKey].transformer ? metadataList[metaKey].transformer(object[src], object) : object[src];
|
|
149
|
+
}
|
|
150
|
+
else {
|
|
151
|
+
this[propertyName] = (metadataList && metadataList[propertyName]?.transformer) ? metadataList[propertyName].transformer(object[propertyName], object) : object[propertyName];
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
/**
|
|
155
|
+
* Initializes properties of the current object based on the provided metadata list.
|
|
156
|
+
*
|
|
157
|
+
* @param metadataList - An object containing metadata information for properties.
|
|
158
|
+
* @param object - The object to be used for initializing properties.
|
|
159
|
+
*
|
|
160
|
+
* The method iterates over the keys of the metadataList. For each key, if the corresponding metadata
|
|
161
|
+
* has an `initialize` property set to true, a `transformer` function, and the current object does not
|
|
162
|
+
* already have a property with the same name, it initializes the property using the transformer function.
|
|
163
|
+
*/
|
|
164
|
+
initializeProperties(metadataList, object) {
|
|
165
|
+
if (metadataList) {
|
|
166
|
+
Object.keys(metadataList).forEach(metaName => {
|
|
167
|
+
if (metadataList[metaName]?.initialize && metadataList[metaName]?.transformer && this[metaName] === undefined) {
|
|
168
|
+
this[metaName] = metadataList[metaName]?.transformer(null, object);
|
|
169
|
+
}
|
|
170
|
+
});
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
/**
|
|
174
|
+
* Converts the current object instance to a mapped object.
|
|
175
|
+
*
|
|
176
|
+
* This method iterates over the properties of the current object and maps them
|
|
177
|
+
* according to the metadata provided by `getMapFieldMetadataList`. If a property
|
|
178
|
+
* has associated metadata, it uses the `mapToJson` method to map it. Otherwise,
|
|
179
|
+
* it directly assigns the property value to the resulting object, applying a
|
|
180
|
+
* reverser function if specified in the metadata.
|
|
181
|
+
*
|
|
182
|
+
* @returns {any} The mapped object.
|
|
103
183
|
*/
|
|
104
184
|
toMap() {
|
|
105
185
|
const metadataList = (0, field_decorator_1.getMapFieldMetadataList)(this);
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
if (metadataList &&
|
|
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
|
-
|
|
186
|
+
const obj = {};
|
|
187
|
+
Object.keys(this).forEach(propertyName => {
|
|
188
|
+
if (metadataList && metadataList[propertyName]) {
|
|
189
|
+
this.mapToJson(metadataList, obj, propertyName);
|
|
190
|
+
}
|
|
191
|
+
else {
|
|
192
|
+
obj[propertyName] = metadataList && metadataList[propertyName]?.reverser ? metadataList[propertyName].reverser(this[propertyName], this) : this[propertyName];
|
|
193
|
+
}
|
|
194
|
+
if (!obj[propertyName]) {
|
|
195
|
+
delete obj[propertyName];
|
|
196
|
+
}
|
|
197
|
+
});
|
|
198
|
+
return obj;
|
|
199
|
+
}
|
|
200
|
+
/**
|
|
201
|
+
* Maps the provided object to a JSON structure based on the given metadata list.
|
|
202
|
+
*
|
|
203
|
+
* @param metadataList - An object containing metadata information for mapping.
|
|
204
|
+
* @param obj - The object to be mapped to JSON.
|
|
205
|
+
* @param propertyName - The name of the property to be mapped.
|
|
206
|
+
*
|
|
207
|
+
* The method supports nested properties and arrays. If the source property name
|
|
208
|
+
* (src) contains dots, it indicates nested properties. The method will traverse
|
|
209
|
+
* the object structure accordingly and create nested objects or arrays as needed.
|
|
210
|
+
*
|
|
211
|
+
* The `getPropsStereoid` method is used to parse the nested property names and
|
|
212
|
+
* determine if they are arrays. The `getReversedValue` method is used to get the
|
|
213
|
+
* value to be assigned to the final property.
|
|
214
|
+
*
|
|
215
|
+
* Example:
|
|
216
|
+
* ```
|
|
217
|
+
* const metadataList = {
|
|
218
|
+
* 'nested.property': { src: 'nested.property' },
|
|
219
|
+
* 'array[0].item': { src: 'array[0].item' }
|
|
220
|
+
* };
|
|
221
|
+
* const obj = {};
|
|
222
|
+
* mapToJson(metadataList, obj, 'nested.property');
|
|
223
|
+
* mapToJson(metadataList, obj, 'array[0].item');
|
|
224
|
+
* ```
|
|
225
|
+
* The resulting `obj` will have the structure:
|
|
226
|
+
* ```
|
|
227
|
+
* {
|
|
228
|
+
* nested: {
|
|
229
|
+
* property: <value>
|
|
230
|
+
* },
|
|
231
|
+
* array: [
|
|
232
|
+
* { item: <value> }
|
|
233
|
+
* ]
|
|
234
|
+
* }
|
|
235
|
+
* ```
|
|
236
|
+
*/
|
|
237
|
+
mapToJson(metadataList, obj, propertyName) {
|
|
238
|
+
const src = metadataList[propertyName].src || propertyName;
|
|
239
|
+
if (src.includes('.')) {
|
|
240
|
+
const propsStereoid = this.getPropsStereoid(src);
|
|
241
|
+
let objCopy = obj;
|
|
242
|
+
let lastIndex;
|
|
243
|
+
propsStereoid.forEach((prop, i) => {
|
|
244
|
+
if (prop.isArray) {
|
|
245
|
+
const arrIndex = prop.arrIndex.split(/\[(\w+)\]/g).filter(index => index !== '');
|
|
246
|
+
objCopy[prop.prop] = objCopy[prop.prop] || [];
|
|
247
|
+
objCopy = objCopy[prop.prop];
|
|
248
|
+
arrIndex.forEach((index, j) => {
|
|
249
|
+
objCopy[index] = objCopy[index] || (j === arrIndex.length - 1 ? {} : []);
|
|
250
|
+
if (j !== propsStereoid.length - 1) {
|
|
251
|
+
objCopy = objCopy[index];
|
|
135
252
|
}
|
|
136
253
|
else {
|
|
137
|
-
|
|
138
|
-
if (!(i == propsStereoid?.length - 1))
|
|
139
|
-
objCopy = objCopy[propsStereoid[i].prop];
|
|
140
|
-
else
|
|
141
|
-
lastIndex = propsStereoid[i].prop;
|
|
254
|
+
lastIndex = index;
|
|
142
255
|
}
|
|
143
|
-
}
|
|
144
|
-
if (Array.isArray(this[propertyName])) {
|
|
145
|
-
objCopy[lastIndex] = metadataList[propertyName].reverser ?
|
|
146
|
-
metadataList[propertyName].reverser(this[propertyName], this)
|
|
147
|
-
: this[propertyName].map(item => {
|
|
148
|
-
return item?.toMap ? item.toMap() : item;
|
|
149
|
-
});
|
|
150
|
-
}
|
|
151
|
-
else if (metadataList[propertyName].toMap) {
|
|
152
|
-
objCopy[lastIndex] = this[propertyName]?.toMap();
|
|
153
|
-
}
|
|
154
|
-
else {
|
|
155
|
-
objCopy[lastIndex] = metadataList[propertyName].reverser ? metadataList[propertyName].reverser(this[propertyName], this) : this[propertyName];
|
|
156
|
-
}
|
|
256
|
+
});
|
|
157
257
|
}
|
|
158
258
|
else {
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
if (revObj[key])
|
|
163
|
-
obj[key] = revObj[key];
|
|
164
|
-
});
|
|
259
|
+
objCopy[prop.prop] = objCopy[prop.prop] || {};
|
|
260
|
+
if (i !== propsStereoid.length - 1) {
|
|
261
|
+
objCopy = objCopy[prop.prop];
|
|
165
262
|
}
|
|
166
263
|
else {
|
|
167
|
-
|
|
168
|
-
obj[src] = this[propertyName].map(item => {
|
|
169
|
-
return item?.toMap ? item.toMap() : item;
|
|
170
|
-
});
|
|
171
|
-
}
|
|
172
|
-
else if (metadataList[propertyName]?.reverser) {
|
|
173
|
-
obj[src] = metadataList[propertyName].reverser(this[propertyName], this);
|
|
174
|
-
}
|
|
175
|
-
else if (this[propertyName]?.toMap) {
|
|
176
|
-
obj[src] = this[propertyName]?.toMap();
|
|
177
|
-
}
|
|
178
|
-
else {
|
|
179
|
-
obj[src] = this[propertyName];
|
|
180
|
-
}
|
|
264
|
+
lastIndex = prop.prop;
|
|
181
265
|
}
|
|
182
266
|
}
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
}
|
|
189
|
-
return obj;
|
|
267
|
+
});
|
|
268
|
+
objCopy[lastIndex] = this.getReversedValue(metadataList, propertyName);
|
|
269
|
+
}
|
|
270
|
+
else {
|
|
271
|
+
obj[src] = this.getReversedValue(metadataList, propertyName);
|
|
272
|
+
}
|
|
190
273
|
}
|
|
191
274
|
/**
|
|
192
|
-
*
|
|
275
|
+
* Retrieves the reversed value of a property based on the provided metadata.
|
|
193
276
|
*
|
|
194
|
-
* @param
|
|
195
|
-
* @
|
|
277
|
+
* @param metadataList - An object containing metadata for properties.
|
|
278
|
+
* @param propertyName - The name of the property to retrieve the reversed value for.
|
|
279
|
+
* @returns The reversed value of the property. If the property is an array, it applies the reverser function or maps each item to its mapped value. If the property has a `toMap` method, it calls that method. Otherwise, it applies the reverser function or returns the property value directly.
|
|
280
|
+
*/
|
|
281
|
+
getReversedValue(metadataList, propertyName) {
|
|
282
|
+
if (Array.isArray(this[propertyName])) {
|
|
283
|
+
return metadataList[propertyName].reverser ? metadataList[propertyName].reverser(this[propertyName], this) : this[propertyName].map(item => item?.toMap ? item.toMap() : item);
|
|
284
|
+
}
|
|
285
|
+
else if (metadataList[propertyName].toMap) {
|
|
286
|
+
return this[propertyName]?.toMap();
|
|
287
|
+
}
|
|
288
|
+
else {
|
|
289
|
+
return metadataList[propertyName].reverser ? metadataList[propertyName].reverser(this[propertyName], this) : this[propertyName];
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
/**
|
|
293
|
+
* Maps the properties of the given object to the current instance based on metadata.
|
|
294
|
+
*
|
|
295
|
+
* @param obj - The source object whose properties are to be mapped.
|
|
296
|
+
* @returns The current instance with mapped properties.
|
|
196
297
|
*/
|
|
197
298
|
objToModel(obj) {
|
|
198
299
|
const metadataList = (0, field_decorator_1.getMapFieldMetadataList)(this);
|
|
199
|
-
|
|
200
|
-
if (metadataList &&
|
|
300
|
+
Object.keys(obj).forEach(propertyName => {
|
|
301
|
+
if (metadataList && metadataList[propertyName]) {
|
|
201
302
|
if (metadataList[propertyName].transformer) {
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
});
|
|
206
|
-
}
|
|
207
|
-
else {
|
|
208
|
-
this[propertyName] = obj[propertyName];
|
|
209
|
-
}
|
|
303
|
+
this[propertyName] = Array.isArray(obj[propertyName])
|
|
304
|
+
? obj[propertyName].map(item => item)
|
|
305
|
+
: metadataList[propertyName].transformer(obj[propertyName], obj);
|
|
210
306
|
}
|
|
211
307
|
else {
|
|
212
308
|
this[propertyName] = obj[propertyName];
|
|
@@ -219,47 +315,34 @@ class MapperFactory {
|
|
|
219
315
|
return this;
|
|
220
316
|
}
|
|
221
317
|
/**
|
|
222
|
-
*
|
|
318
|
+
* Checks if all properties of the current object are either `undefined` or `null`.
|
|
223
319
|
*
|
|
224
|
-
* @returns true or false
|
|
320
|
+
* @returns {boolean} `true` if all properties are `undefined` or `null`, otherwise `false`.
|
|
225
321
|
*/
|
|
226
322
|
empty() {
|
|
227
|
-
|
|
228
|
-
this && Object.keys(this).forEach(propertyName => {
|
|
229
|
-
if (this[propertyName] !== undefined && this[propertyName] !== null) {
|
|
230
|
-
check = false;
|
|
231
|
-
}
|
|
232
|
-
});
|
|
233
|
-
return check;
|
|
323
|
+
return !Object.keys(this).some(propertyName => this[propertyName] !== undefined && this[propertyName] !== null);
|
|
234
324
|
}
|
|
235
325
|
/**
|
|
236
|
-
*
|
|
326
|
+
* Retrieves a nested property value from the object based on the provided path.
|
|
327
|
+
* The path can include dot notation and array indices.
|
|
237
328
|
*
|
|
238
|
-
* @
|
|
239
|
-
* @
|
|
329
|
+
* @template T - The expected type of the property value.
|
|
330
|
+
* @param {string} path - The path to the property, using dot notation and array indices.
|
|
331
|
+
* @returns {T} - The value of the property at the specified path.
|
|
240
332
|
*/
|
|
241
333
|
get(path) {
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
let rtn;
|
|
245
|
-
if (props?.length) {
|
|
246
|
-
rtn = this[props[0]];
|
|
247
|
-
for (let index in props) {
|
|
248
|
-
if (+index > 0)
|
|
249
|
-
rtn = rtn && rtn[props[index]];
|
|
250
|
-
}
|
|
251
|
-
}
|
|
252
|
-
return rtn;
|
|
334
|
+
const props = path.replace(/\[(\w+)\]/g, '.$1').split('.');
|
|
335
|
+
return props.reduce((acc, prop) => acc && acc[prop], this);
|
|
253
336
|
}
|
|
254
337
|
/**
|
|
255
|
-
*
|
|
338
|
+
* Sets the value at the specified path within the object.
|
|
339
|
+
* The path is a string that can include dot notation and array indices.
|
|
256
340
|
*
|
|
257
|
-
* @param path
|
|
258
|
-
* @param value
|
|
341
|
+
* @param path - The path to the property to set, using dot notation and array indices.
|
|
342
|
+
* @param value - The value to set at the specified path.
|
|
259
343
|
*/
|
|
260
344
|
set(path, value) {
|
|
261
|
-
|
|
262
|
-
let props = path.split('.');
|
|
345
|
+
const props = path.replace(/\[(\w+)\]/g, '.$1').split('.');
|
|
263
346
|
let obj = this;
|
|
264
347
|
let i;
|
|
265
348
|
for (i = 0; i < props?.length - 1; i++) {
|
|
@@ -273,7 +356,7 @@ exports.MapperFactory = MapperFactory;
|
|
|
273
356
|
* @deprecated The method should not be used
|
|
274
357
|
*/
|
|
275
358
|
function MapperFactoryFun(baseClass = Object) {
|
|
276
|
-
class Mapper extends baseClass {
|
|
359
|
+
return class Mapper extends baseClass {
|
|
277
360
|
/**
|
|
278
361
|
* Constructor of the mapper.
|
|
279
362
|
*
|
|
@@ -283,7 +366,6 @@ function MapperFactoryFun(baseClass = Object) {
|
|
|
283
366
|
constructor(object, ...rest) {
|
|
284
367
|
super(...rest);
|
|
285
368
|
}
|
|
286
|
-
}
|
|
287
|
-
return Mapper;
|
|
369
|
+
};
|
|
288
370
|
}
|
|
289
371
|
exports.MapperFactoryFun = MapperFactoryFun;
|