mapper-factory 2.0.3 → 3.0.1
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/README.md +0 -176
- package/dist/class.decorator.d.ts +1 -1
- package/dist/example.js +104 -120
- package/dist/functions.js +101 -193
- package/dist/index.d.ts +2 -4
- package/dist/index.js +2 -8
- 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 +219 -0
- package/package.json +12 -3
package/dist/functions.js
CHANGED
|
@@ -10,85 +10,57 @@ const field_decorator_1 = require("./field.decorator");
|
|
|
10
10
|
function toMap() {
|
|
11
11
|
const metadataList = (0, field_decorator_1.getMapFieldMetadataList)(this);
|
|
12
12
|
let obj = {};
|
|
13
|
+
const processProperty = (objCopy, propsStereoid, value, reverser) => {
|
|
14
|
+
let lastIndex;
|
|
15
|
+
for (let i = 0; i < propsStereoid.length; i++) {
|
|
16
|
+
const prop = propsStereoid[i];
|
|
17
|
+
if (prop.isArray) {
|
|
18
|
+
let arrIndex = prop.arrIndex.split(/\[(\w+)\]/g).filter(index => index !== '');
|
|
19
|
+
objCopy[prop.prop] = objCopy[prop.prop] || [];
|
|
20
|
+
objCopy = objCopy[prop.prop];
|
|
21
|
+
arrIndex.forEach((index, i) => {
|
|
22
|
+
objCopy[index] = objCopy[index] || (i == arrIndex.length - 1 ? {} : []);
|
|
23
|
+
if (i != arrIndex.length - 1)
|
|
24
|
+
objCopy = objCopy[index];
|
|
25
|
+
else
|
|
26
|
+
lastIndex = index;
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
else {
|
|
30
|
+
objCopy[prop.prop] = objCopy[prop.prop] || {};
|
|
31
|
+
if (i != propsStereoid.length - 1)
|
|
32
|
+
objCopy = objCopy[prop.prop];
|
|
33
|
+
else
|
|
34
|
+
lastIndex = prop.prop;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
objCopy[lastIndex] = reverser ? reverser(value, this) : value;
|
|
38
|
+
};
|
|
13
39
|
this && Object.keys(this).forEach(propertyName => {
|
|
14
|
-
|
|
15
|
-
|
|
40
|
+
const metadata = metadataList && metadataList[propertyName];
|
|
41
|
+
const src = metadata?.src || propertyName;
|
|
42
|
+
if (metadata) {
|
|
16
43
|
if (src.includes('.')) {
|
|
17
44
|
let props = src.split('.');
|
|
18
|
-
let propsStereoid = props.map(prop => {
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
};
|
|
25
|
-
});
|
|
26
|
-
let i;
|
|
27
|
-
let objCopy = obj;
|
|
28
|
-
let lastIndex;
|
|
29
|
-
for (i = 0; i < propsStereoid?.length; i++) {
|
|
30
|
-
if (propsStereoid[i].isArray) {
|
|
31
|
-
let arrIndex = propsStereoid[i].arrIndex?.split(/\[(\w+)\]/g)?.filter(index => index !== '');
|
|
32
|
-
objCopy[propsStereoid[i].prop] = objCopy[propsStereoid[i].prop] || [];
|
|
33
|
-
objCopy = objCopy[propsStereoid[i].prop];
|
|
34
|
-
arrIndex.forEach((index, i) => {
|
|
35
|
-
objCopy[index] = objCopy[index] || (i == arrIndex.length - 1 ? {} : []);
|
|
36
|
-
if (!(i == propsStereoid.length - 1))
|
|
37
|
-
objCopy = objCopy[index];
|
|
38
|
-
else
|
|
39
|
-
lastIndex = index;
|
|
40
|
-
});
|
|
41
|
-
}
|
|
42
|
-
else {
|
|
43
|
-
objCopy[propsStereoid[i].prop] = objCopy[propsStereoid[i].prop] || {};
|
|
44
|
-
if (!(i == propsStereoid?.length - 1))
|
|
45
|
-
objCopy = objCopy[propsStereoid[i].prop];
|
|
46
|
-
else
|
|
47
|
-
lastIndex = propsStereoid[i].prop;
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
if (Array.isArray(this[propertyName])) {
|
|
51
|
-
objCopy[lastIndex] = metadataList[propertyName].reverser ?
|
|
52
|
-
metadataList[propertyName].reverser(this[propertyName], this)
|
|
53
|
-
: this[propertyName].map(item => {
|
|
54
|
-
return item?.toMap ? item.toMap() : item;
|
|
55
|
-
});
|
|
56
|
-
}
|
|
57
|
-
else if (metadataList[propertyName].toMap) {
|
|
58
|
-
objCopy[lastIndex] = this[propertyName]?.toMap();
|
|
59
|
-
}
|
|
60
|
-
else {
|
|
61
|
-
objCopy[lastIndex] = metadataList[propertyName].reverser ? metadataList[propertyName].reverser(this[propertyName], this) : this[propertyName];
|
|
62
|
-
}
|
|
45
|
+
let propsStereoid = props.map(prop => ({
|
|
46
|
+
prop: prop.includes('[') ? prop.substring(0, prop.indexOf('[')) : prop,
|
|
47
|
+
isArray: prop.includes('[') && prop.includes(']'),
|
|
48
|
+
arrIndex: prop.substring(prop.indexOf('[')),
|
|
49
|
+
}));
|
|
50
|
+
processProperty(obj, propsStereoid, this[propertyName], metadata.reverser);
|
|
63
51
|
}
|
|
64
52
|
else {
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
if (Array.isArray(this[propertyName]) && !metadataList[propertyName]?.reverser) {
|
|
73
|
-
obj[src] = this[propertyName].map(item => {
|
|
74
|
-
return item?.toMap ? item.toMap() : item;
|
|
75
|
-
});
|
|
76
|
-
}
|
|
77
|
-
else if (metadataList[propertyName]?.reverser) {
|
|
78
|
-
obj[src] = metadataList[propertyName].reverser(this[propertyName], this);
|
|
79
|
-
}
|
|
80
|
-
else if (this[propertyName]?.toMap) {
|
|
81
|
-
obj[src] = this[propertyName]?.toMap();
|
|
82
|
-
}
|
|
83
|
-
else {
|
|
84
|
-
obj[src] = this[propertyName];
|
|
85
|
-
}
|
|
86
|
-
/*}*/
|
|
53
|
+
obj[src] = Array.isArray(this[propertyName]) && !metadata.reverser
|
|
54
|
+
? this[propertyName].map(item => item?.toMap ? item.toMap() : item)
|
|
55
|
+
: metadata.reverser
|
|
56
|
+
? metadata.reverser(this[propertyName], this)
|
|
57
|
+
: this[propertyName]?.toMap
|
|
58
|
+
? this[propertyName].toMap()
|
|
59
|
+
: this[propertyName];
|
|
87
60
|
}
|
|
88
61
|
}
|
|
89
62
|
else {
|
|
90
|
-
|
|
91
|
-
obj[propertyName] = (metadataList && metadataList[propertyName]?.reverser) ? metadataList[propertyName].reverser(this[propertyName], this) : this[propertyName];
|
|
63
|
+
obj[propertyName] = this[propertyName];
|
|
92
64
|
}
|
|
93
65
|
});
|
|
94
66
|
return obj;
|
|
@@ -101,26 +73,13 @@ exports.toMap = toMap;
|
|
|
101
73
|
* @returns Instance of this class
|
|
102
74
|
*/
|
|
103
75
|
function objToModel(obj) {
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
});
|
|
112
|
-
}
|
|
113
|
-
else {
|
|
114
|
-
this[propertyName] = obj[propertyName];
|
|
115
|
-
}
|
|
116
|
-
}
|
|
117
|
-
else {
|
|
118
|
-
this[propertyName] = obj[propertyName];
|
|
119
|
-
}
|
|
120
|
-
}
|
|
121
|
-
else {
|
|
122
|
-
this[propertyName] = obj[propertyName];
|
|
123
|
-
}
|
|
76
|
+
if (!obj)
|
|
77
|
+
return this;
|
|
78
|
+
Object.keys(obj).forEach(propertyName => {
|
|
79
|
+
const value = obj[propertyName];
|
|
80
|
+
this[propertyName] = Array.isArray(value)
|
|
81
|
+
? value.map(item => item)
|
|
82
|
+
: value;
|
|
124
83
|
});
|
|
125
84
|
return this;
|
|
126
85
|
}
|
|
@@ -131,14 +90,7 @@ exports.objToModel = objToModel;
|
|
|
131
90
|
* @returns true or false
|
|
132
91
|
*/
|
|
133
92
|
function empty() {
|
|
134
|
-
|
|
135
|
-
this && Object.keys(this).forEach(propertyName => {
|
|
136
|
-
if (this[propertyName] !== undefined && this[propertyName] !== null) {
|
|
137
|
-
check = false;
|
|
138
|
-
return;
|
|
139
|
-
}
|
|
140
|
-
});
|
|
141
|
-
return check;
|
|
93
|
+
return !Object.keys(this).some(propertyName => this[propertyName] !== undefined && this[propertyName] !== null);
|
|
142
94
|
}
|
|
143
95
|
exports.empty = empty;
|
|
144
96
|
/**
|
|
@@ -147,16 +99,7 @@ exports.empty = empty;
|
|
|
147
99
|
* @returns true or false
|
|
148
100
|
*/
|
|
149
101
|
function filled() {
|
|
150
|
-
|
|
151
|
-
return false;
|
|
152
|
-
let check = true;
|
|
153
|
-
this && Object.keys(this).forEach(propertyName => {
|
|
154
|
-
if (this[propertyName] === undefined || this[propertyName] === null) {
|
|
155
|
-
check = false;
|
|
156
|
-
return;
|
|
157
|
-
}
|
|
158
|
-
});
|
|
159
|
-
return check;
|
|
102
|
+
return Object.keys(this).length > 0 && Object.keys(this).every(propertyName => this[propertyName] !== undefined && this[propertyName] !== null);
|
|
160
103
|
}
|
|
161
104
|
exports.filled = filled;
|
|
162
105
|
/**
|
|
@@ -166,17 +109,8 @@ exports.filled = filled;
|
|
|
166
109
|
* @returns Value of the property
|
|
167
110
|
*/
|
|
168
111
|
function get(path) {
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
let rtn;
|
|
172
|
-
if (props?.length) {
|
|
173
|
-
rtn = this[props[0]];
|
|
174
|
-
for (let index in props) {
|
|
175
|
-
if (+index > 0)
|
|
176
|
-
rtn = rtn && rtn[props[index]];
|
|
177
|
-
}
|
|
178
|
-
}
|
|
179
|
-
return rtn;
|
|
112
|
+
const props = path.replace(/\[(\w+)\]/g, '.$1').split('.');
|
|
113
|
+
return props.reduce((acc, prop) => acc && acc[prop], this);
|
|
180
114
|
}
|
|
181
115
|
exports.get = get;
|
|
182
116
|
/**
|
|
@@ -186,14 +120,14 @@ exports.get = get;
|
|
|
186
120
|
* @param value Value of the property
|
|
187
121
|
*/
|
|
188
122
|
function set(path, value) {
|
|
189
|
-
|
|
190
|
-
let props = path.split('.');
|
|
123
|
+
const props = path.replace(/\[(\w+)\]/g, '.$1').split('.');
|
|
191
124
|
let obj = this;
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
125
|
+
props.slice(0, -1).forEach(prop => {
|
|
126
|
+
if (!obj[prop])
|
|
127
|
+
obj[prop] = {};
|
|
128
|
+
obj = obj[prop];
|
|
129
|
+
});
|
|
130
|
+
obj[props[props.length - 1]] = value;
|
|
197
131
|
}
|
|
198
132
|
exports.set = set;
|
|
199
133
|
/**
|
|
@@ -210,89 +144,63 @@ exports.copy = copy;
|
|
|
210
144
|
*/
|
|
211
145
|
function from(object) {
|
|
212
146
|
const metadataList = (0, field_decorator_1.getMapFieldMetadataList)(this);
|
|
147
|
+
const processProperty = (objCopy, propsStereoid) => {
|
|
148
|
+
for (let i = 0; i < propsStereoid.length; i++) {
|
|
149
|
+
const prop = propsStereoid[i];
|
|
150
|
+
if (prop.isArray) {
|
|
151
|
+
let arrIndex = prop.arrIndex.split(/\[(\w+)\]/g).filter(index => index !== '');
|
|
152
|
+
objCopy = objCopy[prop.prop];
|
|
153
|
+
arrIndex.forEach(index => {
|
|
154
|
+
objCopy = objCopy[index];
|
|
155
|
+
});
|
|
156
|
+
}
|
|
157
|
+
else {
|
|
158
|
+
objCopy = objCopy[prop.prop];
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
return objCopy;
|
|
162
|
+
};
|
|
163
|
+
const setProperty = (metaKey, value, object) => {
|
|
164
|
+
const metaProp = metadataList?.[metaKey];
|
|
165
|
+
if (metaProp?.transformer) {
|
|
166
|
+
this[metaKey] = metaProp.transformer(value, object);
|
|
167
|
+
}
|
|
168
|
+
else {
|
|
169
|
+
this[metaKey] = value;
|
|
170
|
+
}
|
|
171
|
+
};
|
|
213
172
|
object && Object.keys(object).forEach(propertyName => {
|
|
214
173
|
let metaKeys = metadataList && Object.keys(metadataList).filter(metadata => metadataList[metadata]?.src?.split('.')?.includes(propertyName));
|
|
215
174
|
if (metaKeys?.length) {
|
|
216
175
|
metaKeys.forEach(metaKey => {
|
|
217
|
-
|
|
176
|
+
const metaProp = metadataList?.[metaKey];
|
|
218
177
|
if (metaProp) {
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
});
|
|
228
|
-
let i;
|
|
229
|
-
let objCopy = { ...object };
|
|
230
|
-
for (i = 0; i < propsStereoid.length; i++) {
|
|
231
|
-
if (propsStereoid[i].isArray) {
|
|
232
|
-
let arrIndex = propsStereoid[i].arrIndex?.split(/\[(\w+)\]/g)?.filter(index => index !== '');
|
|
233
|
-
objCopy = objCopy[propsStereoid[i].prop];
|
|
234
|
-
arrIndex.forEach((index, i) => {
|
|
235
|
-
objCopy = objCopy[index];
|
|
236
|
-
});
|
|
237
|
-
}
|
|
238
|
-
else {
|
|
239
|
-
objCopy = objCopy[propsStereoid[i].prop];
|
|
240
|
-
}
|
|
241
|
-
}
|
|
242
|
-
if (metaProp?.transformer) {
|
|
243
|
-
this[metaKey] = metaProp.transformer(objCopy, object);
|
|
244
|
-
}
|
|
245
|
-
else {
|
|
246
|
-
this[metaKey] = objCopy;
|
|
247
|
-
}
|
|
248
|
-
}
|
|
249
|
-
else {
|
|
250
|
-
let metaKey = metadataList && Object.keys(metadataList).find(metadata => metadataList[metadata]?.src == propertyName);
|
|
251
|
-
if (metaKey) {
|
|
252
|
-
const src = metadataList[metaKey].src || propertyName;
|
|
253
|
-
if (metadataList[metaKey].transformer) {
|
|
254
|
-
this[metaKey] = metadataList[metaKey].transformer(object[src], object);
|
|
255
|
-
}
|
|
256
|
-
else {
|
|
257
|
-
this[metaKey] = object[src];
|
|
258
|
-
}
|
|
259
|
-
}
|
|
260
|
-
else {
|
|
261
|
-
if (metadataList[propertyName]?.transformer) {
|
|
262
|
-
this[propertyName] = metadataList[propertyName].transformer(object[propertyName], object);
|
|
263
|
-
}
|
|
264
|
-
else {
|
|
265
|
-
this[propertyName] = object[propertyName];
|
|
266
|
-
}
|
|
267
|
-
}
|
|
178
|
+
const props = metaProp.src.split('.');
|
|
179
|
+
const propsStereoid = props.map(prop => ({
|
|
180
|
+
prop: prop.includes('[') ? prop.substring(0, prop.indexOf('[')) : prop,
|
|
181
|
+
isArray: prop.includes('[') && prop.includes(']'),
|
|
182
|
+
arrIndex: prop.substring(prop.indexOf('[')),
|
|
183
|
+
}));
|
|
184
|
+
const value = processProperty({ ...object }, propsStereoid);
|
|
185
|
+
setProperty(metaKey, value, object);
|
|
268
186
|
}
|
|
269
187
|
});
|
|
270
188
|
}
|
|
271
189
|
else {
|
|
272
190
|
let metaKey = metadataList && Object.keys(metadataList).find(metadata => metadataList[metadata]?.src == propertyName);
|
|
273
191
|
if (metaKey) {
|
|
274
|
-
const src = metadataList[metaKey].src || propertyName;
|
|
275
|
-
|
|
276
|
-
this[metaKey] = metadataList[metaKey].transformer(object[src], object);
|
|
277
|
-
}
|
|
278
|
-
else {
|
|
279
|
-
this[metaKey] = object[src];
|
|
280
|
-
}
|
|
192
|
+
const src = metadataList?.[metaKey].src || propertyName;
|
|
193
|
+
setProperty(metaKey, object[src], object);
|
|
281
194
|
}
|
|
282
195
|
else {
|
|
283
|
-
|
|
284
|
-
this[propertyName] = metadataList[propertyName].transformer(object[propertyName], object);
|
|
285
|
-
}
|
|
286
|
-
else {
|
|
287
|
-
this[propertyName] = object[propertyName];
|
|
288
|
-
}
|
|
196
|
+
setProperty(propertyName, object[propertyName], object);
|
|
289
197
|
}
|
|
290
198
|
}
|
|
291
199
|
});
|
|
292
|
-
//
|
|
200
|
+
// Initialize properties with "initialize" metadata
|
|
293
201
|
metadataList && Object.keys(metadataList).forEach(metaName => {
|
|
294
|
-
if (metadataList[metaName]?.initialize && metadataList[metaName]?.transformer && this[metaName]
|
|
295
|
-
this[metaName] = metadataList[metaName]
|
|
202
|
+
if (metadataList[metaName]?.initialize && metadataList[metaName]?.transformer && this[metaName] === undefined) {
|
|
203
|
+
this[metaName] = metadataList[metaName].transformer(null, object);
|
|
296
204
|
}
|
|
297
205
|
});
|
|
298
206
|
return this;
|
package/dist/index.d.ts
CHANGED
|
@@ -1,12 +1,10 @@
|
|
|
1
1
|
import { MapClass, MapInterface } from "./class.decorator";
|
|
2
2
|
import { MapField } from "./field.decorator";
|
|
3
|
-
import { MapperFactory } from "./mapper";
|
|
4
|
-
import { objToModel, toMap, toModel } from "./mapper-functions";
|
|
5
3
|
import { ClassType } from "./types";
|
|
6
|
-
export { ClassType, MapClass, MapField, MapInterface
|
|
4
|
+
export { ClassType, MapClass, MapField, MapInterface };
|
|
7
5
|
/**
|
|
8
6
|
* npx tsc
|
|
9
7
|
* npx ts-node src/example.ts
|
|
10
|
-
* npm version patch
|
|
8
|
+
* npm version ( patch | minor | major )
|
|
11
9
|
* npm publish
|
|
12
10
|
*/
|
package/dist/index.js
CHANGED
|
@@ -1,19 +1,13 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports.MapField = exports.MapClass = void 0;
|
|
4
4
|
const class_decorator_1 = require("./class.decorator");
|
|
5
5
|
Object.defineProperty(exports, "MapClass", { enumerable: true, get: function () { return class_decorator_1.MapClass; } });
|
|
6
6
|
const field_decorator_1 = require("./field.decorator");
|
|
7
7
|
Object.defineProperty(exports, "MapField", { enumerable: true, get: function () { return field_decorator_1.MapField; } });
|
|
8
|
-
const mapper_1 = require("./mapper");
|
|
9
|
-
Object.defineProperty(exports, "MapperFactory", { enumerable: true, get: function () { return mapper_1.MapperFactory; } });
|
|
10
|
-
const mapper_functions_1 = require("./mapper-functions");
|
|
11
|
-
Object.defineProperty(exports, "objToModel", { enumerable: true, get: function () { return mapper_functions_1.objToModel; } });
|
|
12
|
-
Object.defineProperty(exports, "toMap", { enumerable: true, get: function () { return mapper_functions_1.toMap; } });
|
|
13
|
-
Object.defineProperty(exports, "toModel", { enumerable: true, get: function () { return mapper_functions_1.toModel; } });
|
|
14
8
|
/**
|
|
15
9
|
* npx tsc
|
|
16
10
|
* npx ts-node src/example.ts
|
|
17
|
-
* npm version patch
|
|
11
|
+
* npm version ( patch | minor | major )
|
|
18
12
|
* npm publish
|
|
19
13
|
*/
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { MapperFactory } from "./mapper";
|
|
1
|
+
import { MapperFactory } from "./mapper.to-remove";
|
|
2
2
|
export declare function toMap(model: MapperFactory): Object;
|
|
3
3
|
export declare function toModel<T extends MapperFactory>(obj: Object): T;
|
|
4
4
|
export declare function objToModel<T extends MapperFactory>(model: MapperFactory, obj: Object): T;
|
package/dist/mapper-functions.js
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.copy = exports.objToModel = exports.toModel = exports.toMap = void 0;
|
|
4
|
-
const
|
|
4
|
+
const mapper_to_remove_1 = require("./mapper.to-remove");
|
|
5
5
|
function toMap(model) {
|
|
6
6
|
return model.toMap();
|
|
7
7
|
}
|
|
8
8
|
exports.toMap = toMap;
|
|
9
9
|
function toModel(obj) {
|
|
10
|
-
return new
|
|
10
|
+
return new mapper_to_remove_1.MapperFactory(obj);
|
|
11
11
|
}
|
|
12
12
|
exports.toModel = toModel;
|
|
13
13
|
function objToModel(model, obj) {
|
|
@@ -15,6 +15,6 @@ function objToModel(model, obj) {
|
|
|
15
15
|
}
|
|
16
16
|
exports.objToModel = objToModel;
|
|
17
17
|
function copy(model) {
|
|
18
|
-
return new
|
|
18
|
+
return new mapper_to_remove_1.MapperFactory(model.toMap());
|
|
19
19
|
}
|
|
20
20
|
exports.copy = copy;
|
package/dist/mapper.d.ts
CHANGED
|
@@ -7,36 +7,180 @@ export declare class MapperFactory {
|
|
|
7
7
|
*/
|
|
8
8
|
constructor(object?: any);
|
|
9
9
|
/**
|
|
10
|
-
*
|
|
10
|
+
* Maps the properties of an object based on the provided metadata list.
|
|
11
11
|
*
|
|
12
|
-
* @
|
|
12
|
+
* @param object - The object whose properties need to be mapped.
|
|
13
|
+
* @param metadataList - The list of metadata used to map the object's properties.
|
|
14
|
+
*
|
|
15
|
+
* This method iterates over each property of the given object. For each property, it retrieves the corresponding metadata keys.
|
|
16
|
+
* If metadata keys are found, it maps the property using the `mapProperty` method for each metadata key.
|
|
17
|
+
* If no metadata keys are found, it maps the property directly using the `mapDirectProperty` method.
|
|
18
|
+
*/
|
|
19
|
+
private mapObject;
|
|
20
|
+
/**
|
|
21
|
+
* Retrieves the metadata keys associated with a given property name.
|
|
22
|
+
*
|
|
23
|
+
* @param metadataList - The list of metadata objects to search through.
|
|
24
|
+
* @param propertyName - The name of the property to find metadata keys for.
|
|
25
|
+
* @returns An array of metadata keys that are associated with the specified property name.
|
|
26
|
+
*/
|
|
27
|
+
private getMetaKeys;
|
|
28
|
+
/**
|
|
29
|
+
* Maps a property from the given object to the current instance based on the provided metadata.
|
|
30
|
+
*
|
|
31
|
+
* @param object - The source object containing the property to be mapped.
|
|
32
|
+
* @param metadataList - A list of metadata that provides mapping information.
|
|
33
|
+
* @param propertyName - The name of the property to be mapped.
|
|
34
|
+
* @param metaKey - The key used to retrieve the specific metadata for the property.
|
|
35
|
+
*
|
|
36
|
+
* This method performs the following steps:
|
|
37
|
+
* 1. Retrieves the metadata for the given metaKey.
|
|
38
|
+
* 2. If metadata exists, it gets the properties' stereoid and creates a copy of the object.
|
|
39
|
+
* 3. Iterates over the properties' stereoid and updates the object copy.
|
|
40
|
+
* 4. If a transformer function is defined in the metadata, it applies the transformer to the object copy and the original object.
|
|
41
|
+
* 5. If no metadata exists for the metaKey, it maps the property directly using the `mapDirectProperty` method.
|
|
42
|
+
*/
|
|
43
|
+
private mapProperty;
|
|
44
|
+
/**
|
|
45
|
+
* Parses a dot-separated string and returns an array of PropStereoid objects.
|
|
46
|
+
* Each PropStereoid object contains information about the property name,
|
|
47
|
+
* whether it is an array, and the array index if applicable.
|
|
48
|
+
*
|
|
49
|
+
* @param src - The dot-separated string to parse.
|
|
50
|
+
* @returns An array of PropStereoid objects.
|
|
51
|
+
*/
|
|
52
|
+
private getPropsStereoid;
|
|
53
|
+
/**
|
|
54
|
+
* Retrieves a copy of an object property based on the provided `PropStereoid`.
|
|
55
|
+
*
|
|
56
|
+
* @param objCopy - The object from which the property copy is to be retrieved.
|
|
57
|
+
* @param prop - The `PropStereoid` object containing property details.
|
|
58
|
+
* @returns A copy of the specified property from the object.
|
|
59
|
+
*
|
|
60
|
+
* @remarks
|
|
61
|
+
* If the property is an array, the method navigates through the array indices
|
|
62
|
+
* specified in `prop.arrIndex` to retrieve the nested property.
|
|
63
|
+
* Otherwise, it directly retrieves the property specified in `prop.prop`.
|
|
64
|
+
*
|
|
65
|
+
* @example
|
|
66
|
+
* ```typescript
|
|
67
|
+
* const obj = { a: { b: [ { c: 1 }, { c: 2 } ] } };
|
|
68
|
+
* const prop = { isArray: true, arrIndex: '[1]', prop: 'b' };
|
|
69
|
+
* const result = getObjectCopy(obj, prop);
|
|
70
|
+
* console.log(result); // Output: { c: 2 }
|
|
71
|
+
* ```
|
|
72
|
+
*/
|
|
73
|
+
private getObjectCopy;
|
|
74
|
+
/**
|
|
75
|
+
* Maps a direct property from the given object to the current instance based on the provided metadata list.
|
|
76
|
+
*
|
|
77
|
+
* @param object - The source object containing the properties to be mapped.
|
|
78
|
+
* @param metadataList - An optional list of metadata that defines how properties should be mapped and transformed.
|
|
79
|
+
* @param propertyName - The name of the property to be mapped from the source object.
|
|
80
|
+
*
|
|
81
|
+
* The method performs the following steps:
|
|
82
|
+
* 1. Checks if the metadata list contains a key that matches the source property name.
|
|
83
|
+
* 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.
|
|
84
|
+
* 3. If no matching metadata key is found, it directly maps the property from the source object, applying any transformer function if defined.
|
|
85
|
+
*/
|
|
86
|
+
private mapDirectProperty;
|
|
87
|
+
/**
|
|
88
|
+
* Initializes properties of the current object based on the provided metadata list.
|
|
89
|
+
*
|
|
90
|
+
* @param metadataList - An object containing metadata information for properties.
|
|
91
|
+
* @param object - The object to be used for initializing properties.
|
|
92
|
+
*
|
|
93
|
+
* The method iterates over the keys of the metadataList. For each key, if the corresponding metadata
|
|
94
|
+
* has an `initialize` property set to true, a `transformer` function, and the current object does not
|
|
95
|
+
* already have a property with the same name, it initializes the property using the transformer function.
|
|
96
|
+
*/
|
|
97
|
+
private initializeProperties;
|
|
98
|
+
/**
|
|
99
|
+
* Converts the current object instance to a mapped object.
|
|
100
|
+
*
|
|
101
|
+
* This method iterates over the properties of the current object and maps them
|
|
102
|
+
* according to the metadata provided by `getMapFieldMetadataList`. If a property
|
|
103
|
+
* has associated metadata, it uses the `mapToJson` method to map it. Otherwise,
|
|
104
|
+
* it directly assigns the property value to the resulting object, applying a
|
|
105
|
+
* reverser function if specified in the metadata.
|
|
106
|
+
*
|
|
107
|
+
* @returns {any} The mapped object.
|
|
13
108
|
*/
|
|
14
109
|
toMap(): any;
|
|
15
110
|
/**
|
|
16
|
-
*
|
|
111
|
+
* Maps the provided object to a JSON structure based on the given metadata list.
|
|
112
|
+
*
|
|
113
|
+
* @param metadataList - An object containing metadata information for mapping.
|
|
114
|
+
* @param obj - The object to be mapped to JSON.
|
|
115
|
+
* @param propertyName - The name of the property to be mapped.
|
|
116
|
+
*
|
|
117
|
+
* The method supports nested properties and arrays. If the source property name
|
|
118
|
+
* (src) contains dots, it indicates nested properties. The method will traverse
|
|
119
|
+
* the object structure accordingly and create nested objects or arrays as needed.
|
|
120
|
+
*
|
|
121
|
+
* The `getPropsStereoid` method is used to parse the nested property names and
|
|
122
|
+
* determine if they are arrays. The `getReversedValue` method is used to get the
|
|
123
|
+
* value to be assigned to the final property.
|
|
124
|
+
*
|
|
125
|
+
* Example:
|
|
126
|
+
* ```
|
|
127
|
+
* const metadataList = {
|
|
128
|
+
* 'nested.property': { src: 'nested.property' },
|
|
129
|
+
* 'array[0].item': { src: 'array[0].item' }
|
|
130
|
+
* };
|
|
131
|
+
* const obj = {};
|
|
132
|
+
* mapToJson(metadataList, obj, 'nested.property');
|
|
133
|
+
* mapToJson(metadataList, obj, 'array[0].item');
|
|
134
|
+
* ```
|
|
135
|
+
* The resulting `obj` will have the structure:
|
|
136
|
+
* ```
|
|
137
|
+
* {
|
|
138
|
+
* nested: {
|
|
139
|
+
* property: <value>
|
|
140
|
+
* },
|
|
141
|
+
* array: [
|
|
142
|
+
* { item: <value> }
|
|
143
|
+
* ]
|
|
144
|
+
* }
|
|
145
|
+
* ```
|
|
146
|
+
*/
|
|
147
|
+
private mapToJson;
|
|
148
|
+
/**
|
|
149
|
+
* Retrieves the reversed value of a property based on the provided metadata.
|
|
150
|
+
*
|
|
151
|
+
* @param metadataList - An object containing metadata for properties.
|
|
152
|
+
* @param propertyName - The name of the property to retrieve the reversed value for.
|
|
153
|
+
* @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.
|
|
154
|
+
*/
|
|
155
|
+
private getReversedValue;
|
|
156
|
+
/**
|
|
157
|
+
* Maps the properties of the given object to the current instance based on metadata.
|
|
17
158
|
*
|
|
18
|
-
* @param obj
|
|
19
|
-
* @returns
|
|
159
|
+
* @param obj - The source object whose properties are to be mapped.
|
|
160
|
+
* @returns The current instance with mapped properties.
|
|
20
161
|
*/
|
|
21
162
|
objToModel(obj: Object): this;
|
|
22
163
|
/**
|
|
23
|
-
*
|
|
164
|
+
* Checks if all properties of the current object are either `undefined` or `null`.
|
|
24
165
|
*
|
|
25
|
-
* @returns true or false
|
|
166
|
+
* @returns {boolean} `true` if all properties are `undefined` or `null`, otherwise `false`.
|
|
26
167
|
*/
|
|
27
168
|
empty(): boolean;
|
|
28
169
|
/**
|
|
29
|
-
*
|
|
170
|
+
* Retrieves a nested property value from the object based on the provided path.
|
|
171
|
+
* The path can include dot notation and array indices.
|
|
30
172
|
*
|
|
31
|
-
* @
|
|
32
|
-
* @
|
|
173
|
+
* @template T - The expected type of the property value.
|
|
174
|
+
* @param {string} path - The path to the property, using dot notation and array indices.
|
|
175
|
+
* @returns {T} - The value of the property at the specified path.
|
|
33
176
|
*/
|
|
34
177
|
get<T>(path: string): T;
|
|
35
178
|
/**
|
|
36
|
-
*
|
|
179
|
+
* Sets the value at the specified path within the object.
|
|
180
|
+
* The path is a string that can include dot notation and array indices.
|
|
37
181
|
*
|
|
38
|
-
* @param path
|
|
39
|
-
* @param value
|
|
182
|
+
* @param path - The path to the property to set, using dot notation and array indices.
|
|
183
|
+
* @param value - The value to set at the specified path.
|
|
40
184
|
*/
|
|
41
185
|
set(path: string, value: any): void;
|
|
42
186
|
}
|