mapper-factory 1.0.37 → 1.0.40
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 +11 -0
- package/dist/class.decorator.js +17 -0
- package/dist/example.js +47 -0
- package/dist/field.decorator.js +1 -1
- package/dist/functions.d.ts +49 -0
- package/dist/functions.js +300 -0
- package/dist/index.d.ts +2 -1
- package/dist/index.js +5 -1
- package/dist/mapper-functions.d.ts +5 -0
- package/dist/mapper-functions.js +20 -0
- package/package.json +1 -1
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export declare function MapperFactory(): (constructor: any) => void;
|
|
2
|
+
export interface MapperInterface<T> {
|
|
3
|
+
from: (object?: any) => T;
|
|
4
|
+
toMap: () => any;
|
|
5
|
+
toModel: (object: any) => T;
|
|
6
|
+
empty: () => boolean;
|
|
7
|
+
filled: () => boolean;
|
|
8
|
+
get: (path: string) => T;
|
|
9
|
+
set: (path: string, value: any) => void;
|
|
10
|
+
copy: () => T;
|
|
11
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.MapperFactory = void 0;
|
|
4
|
+
const functions_1 = require("../src/functions");
|
|
5
|
+
function MapperFactory() {
|
|
6
|
+
return function (constructor) {
|
|
7
|
+
constructor.prototype.from = functions_1.constructorMap;
|
|
8
|
+
constructor.prototype.toMap = functions_1.toMap;
|
|
9
|
+
constructor.prototype.toModel = functions_1.objToModel;
|
|
10
|
+
constructor.prototype.empty = functions_1.empty;
|
|
11
|
+
constructor.prototype.filled = functions_1.filled;
|
|
12
|
+
constructor.prototype.get = functions_1.get;
|
|
13
|
+
constructor.prototype.set = functions_1.set;
|
|
14
|
+
constructor.prototype.copy = functions_1.copy;
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
exports.MapperFactory = MapperFactory;
|
package/dist/example.js
CHANGED
|
@@ -9,6 +9,8 @@ var __metadata = (this && this.__metadata) || function (k, v) {
|
|
|
9
9
|
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
10
10
|
};
|
|
11
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
const _1 = require(".");
|
|
13
|
+
const class_decorator_1 = require("../src/class.decorator");
|
|
12
14
|
const field_decorator_1 = require("../src/field.decorator");
|
|
13
15
|
const mapper_1 = require("../src/mapper");
|
|
14
16
|
class History extends mapper_1.MapperFactory {
|
|
@@ -91,30 +93,37 @@ let u = new User(JSONObject);
|
|
|
91
93
|
console.log("\nTEST constructor");
|
|
92
94
|
console.log(JSONObject);
|
|
93
95
|
console.log(u);
|
|
96
|
+
console.log("\n\n");
|
|
94
97
|
//TEST objToModel method with JS Object
|
|
95
98
|
let u1 = new User();
|
|
96
99
|
console.log("\nTEST objToModel method with JS Object");
|
|
97
100
|
console.log(u1.objToModel(u));
|
|
101
|
+
console.log("\n\n");
|
|
98
102
|
//TEST objToModel method with JSON Object
|
|
99
103
|
console.log("\nTEST objToModel method with JSON Object");
|
|
100
104
|
console.log(u.objToModel({ name: "Rick TEST-objToModel", roles: ["CEO TEST-objToModel", "EMPLOYEE TEST-objToModel"] }));
|
|
105
|
+
console.log("\n\n");
|
|
101
106
|
//TEST toMap method
|
|
102
107
|
console.log("\nTEST toMap method");
|
|
103
108
|
console.log(u.toMap());
|
|
109
|
+
console.log("\n\n");
|
|
104
110
|
//TEST empty method
|
|
105
111
|
console.log("\nTEST empty method");
|
|
106
112
|
console.log(u.empty());
|
|
113
|
+
console.log("\n\n");
|
|
107
114
|
//TEST get AND set method
|
|
108
115
|
u.set("employees[1].name", "name editato");
|
|
109
116
|
console.log("\nTEST get AND set method");
|
|
110
117
|
console.log(u);
|
|
111
118
|
console.log(u.get("employees[1].name"));
|
|
119
|
+
console.log("\n\n");
|
|
112
120
|
//TEST deep copy
|
|
113
121
|
console.log("\nTEST deep copy");
|
|
114
122
|
let dpCopy = new User(u.toMap());
|
|
115
123
|
dpCopy.name = "nome dpCopy";
|
|
116
124
|
console.log(u);
|
|
117
125
|
console.log(dpCopy);
|
|
126
|
+
console.log("\n\n");
|
|
118
127
|
//TEST trasformer/reverser
|
|
119
128
|
console.log("\nTEST reverser");
|
|
120
129
|
let h1 = new History({ name: "h1" });
|
|
@@ -122,14 +131,52 @@ let h2 = new History({ name: "h2" });
|
|
|
122
131
|
u.histories = [h1, h2];
|
|
123
132
|
console.log(u);
|
|
124
133
|
console.log(u.toMap());
|
|
134
|
+
console.log("\n\n");
|
|
125
135
|
//TEST ref
|
|
126
136
|
console.log("\nTEST REF");
|
|
127
137
|
let hTest = new History({ monday: "0", tuesday: "1", control: "control" });
|
|
128
138
|
console.log(hTest);
|
|
129
139
|
hTest.daysActive = ['1', '0'];
|
|
130
140
|
console.log(hTest.toMap());
|
|
141
|
+
console.log("\n\n");
|
|
131
142
|
//TEST concat with point
|
|
132
143
|
console.log("\nTEST CONCAT W POINT");
|
|
133
144
|
let hTest2 = new History({ test: { concatenation: "resolve " }, control: "control" });
|
|
134
145
|
console.log(hTest2);
|
|
135
146
|
console.log(hTest2.toMap());
|
|
147
|
+
console.log("\n\n");
|
|
148
|
+
//TEST FUNC MAPPER
|
|
149
|
+
console.log("\nTEST FUNC MAPPER");
|
|
150
|
+
console.log(emp1);
|
|
151
|
+
console.log((0, _1.toMap)(emp1));
|
|
152
|
+
const testEmp = (0, _1.toModel)((0, _1.toMap)(emp1));
|
|
153
|
+
console.log("USER MODEL: ", testEmp);
|
|
154
|
+
console.log((0, _1.objToModel)(emp1, { name: "test", surname: "prova" }));
|
|
155
|
+
console.log("\n\n");
|
|
156
|
+
//TEST NEW MapperFactory
|
|
157
|
+
console.log("\nTEST NEW MapperFactory");
|
|
158
|
+
let Test = class Test {
|
|
159
|
+
};
|
|
160
|
+
__decorate([
|
|
161
|
+
(0, field_decorator_1.MapField)({
|
|
162
|
+
src: 'b',
|
|
163
|
+
transformer: value => 'test transformer',
|
|
164
|
+
reverser: value => ({ a: 'test reverser' }),
|
|
165
|
+
initialize: true,
|
|
166
|
+
}),
|
|
167
|
+
__metadata("design:type", String)
|
|
168
|
+
], Test.prototype, "a", void 0);
|
|
169
|
+
Test = __decorate([
|
|
170
|
+
(0, class_decorator_1.MapperFactory)()
|
|
171
|
+
], Test);
|
|
172
|
+
const test = new Test().from();
|
|
173
|
+
console.log("TEST 1: ", test);
|
|
174
|
+
console.log("EMPTY: ", test.empty());
|
|
175
|
+
console.log("FILLED: ", test.filled());
|
|
176
|
+
test.from({ b: 'filled' });
|
|
177
|
+
console.log("TEST 2: ", test);
|
|
178
|
+
console.log("EMPTY: ", test.empty());
|
|
179
|
+
console.log("FILLED: ", test.filled());
|
|
180
|
+
console.log("TO MODEL: ", test.toModel({ a: 'test to model' }));
|
|
181
|
+
console.log("TO MAP: ", test.toMap());
|
|
182
|
+
console.log("\n\n");
|
package/dist/field.decorator.js
CHANGED
|
@@ -9,7 +9,7 @@ function isClass(func) {
|
|
|
9
9
|
}
|
|
10
10
|
exports.isClass = isClass;
|
|
11
11
|
function getPrototype(target) {
|
|
12
|
-
return isClass(target) || !target
|
|
12
|
+
return isClass(target) || !target?.prototype ? !target?.constructor ? target : target?.constructor : target?.prototype;
|
|
13
13
|
}
|
|
14
14
|
exports.getPrototype = getPrototype;
|
|
15
15
|
const MapField = ({ transformer, reverser, src, initialize = false, } = {}) => {
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Convert the instance of this class to JSON Object.
|
|
3
|
+
*
|
|
4
|
+
* @returns JSON object mapped considering metadata "src" and "reverser"
|
|
5
|
+
*/
|
|
6
|
+
export declare function toMap(): any;
|
|
7
|
+
/**
|
|
8
|
+
* Convert a JSON Object to an instance of this class.
|
|
9
|
+
*
|
|
10
|
+
* @param obj JSON Object
|
|
11
|
+
* @returns Instance of this class
|
|
12
|
+
*/
|
|
13
|
+
export declare function objToModel(obj: Object): any;
|
|
14
|
+
/**
|
|
15
|
+
* Check if this instance is empty.
|
|
16
|
+
*
|
|
17
|
+
* @returns true or false
|
|
18
|
+
*/
|
|
19
|
+
export declare function empty(): boolean;
|
|
20
|
+
/**
|
|
21
|
+
* Check if this instance is filled.
|
|
22
|
+
*
|
|
23
|
+
* @returns true or false
|
|
24
|
+
*/
|
|
25
|
+
export declare function filled(): boolean;
|
|
26
|
+
/**
|
|
27
|
+
* GET property value from a string path.
|
|
28
|
+
*
|
|
29
|
+
* @param path String path
|
|
30
|
+
* @returns Value of the property
|
|
31
|
+
*/
|
|
32
|
+
export declare function get<T>(path: string): T;
|
|
33
|
+
/**
|
|
34
|
+
* SET property value from a string path.
|
|
35
|
+
*
|
|
36
|
+
* @param path String path
|
|
37
|
+
* @param value Value of the property
|
|
38
|
+
*/
|
|
39
|
+
export declare function set(path: string, value: any): void;
|
|
40
|
+
/**
|
|
41
|
+
* Deep copy of the object caller
|
|
42
|
+
*/
|
|
43
|
+
export declare function copy<T>(): T;
|
|
44
|
+
/**
|
|
45
|
+
* Constructor of the mapper.
|
|
46
|
+
*
|
|
47
|
+
* @param object object to be mapped considering metadata "src", "transformer" and "reverser"
|
|
48
|
+
*/
|
|
49
|
+
export declare function constructorMap(object: any): any;
|
|
@@ -0,0 +1,300 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.constructorMap = exports.copy = exports.set = exports.get = exports.filled = exports.empty = exports.objToModel = exports.toMap = void 0;
|
|
4
|
+
const field_decorator_1 = require("./field.decorator");
|
|
5
|
+
/**
|
|
6
|
+
* Convert the instance of this class to JSON Object.
|
|
7
|
+
*
|
|
8
|
+
* @returns JSON object mapped considering metadata "src" and "reverser"
|
|
9
|
+
*/
|
|
10
|
+
function toMap() {
|
|
11
|
+
const metadataList = (0, field_decorator_1.getMapFieldMetadataList)(this);
|
|
12
|
+
let obj = {};
|
|
13
|
+
this && Object.keys(this).forEach(propertyName => {
|
|
14
|
+
if (metadataList && Object.keys(metadataList).some(prop => prop == propertyName)) {
|
|
15
|
+
const src = metadataList[propertyName].src || propertyName;
|
|
16
|
+
if (src.includes('.')) {
|
|
17
|
+
let props = src.split('.');
|
|
18
|
+
let propsStereoid = props.map(prop => {
|
|
19
|
+
let index = prop.indexOf('[');
|
|
20
|
+
return {
|
|
21
|
+
prop: index > 0 ? prop.substring(0, index) : prop,
|
|
22
|
+
isArray: prop.includes('[') && prop.includes(']'),
|
|
23
|
+
arrIndex: prop.substring(index),
|
|
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
|
+
}
|
|
63
|
+
}
|
|
64
|
+
else {
|
|
65
|
+
/*if (metadataList[propertyName]?.initialize && metadataList[propertyName]?.reverser) {
|
|
66
|
+
const revObj = metadataList[propertyName]?.reverser(this[propertyName]);
|
|
67
|
+
revObj && Object.keys(revObj).forEach(key => {
|
|
68
|
+
if (revObj[key])
|
|
69
|
+
obj[key] = revObj[key];
|
|
70
|
+
});
|
|
71
|
+
} else {*/
|
|
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
|
+
/*}*/
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
else {
|
|
90
|
+
if (!obj[propertyName])
|
|
91
|
+
obj[propertyName] = (metadataList && metadataList[propertyName]?.reverser) ? metadataList[propertyName].reverser(this[propertyName], this) : this[propertyName];
|
|
92
|
+
}
|
|
93
|
+
});
|
|
94
|
+
return obj;
|
|
95
|
+
}
|
|
96
|
+
exports.toMap = toMap;
|
|
97
|
+
/**
|
|
98
|
+
* Convert a JSON Object to an instance of this class.
|
|
99
|
+
*
|
|
100
|
+
* @param obj JSON Object
|
|
101
|
+
* @returns Instance of this class
|
|
102
|
+
*/
|
|
103
|
+
function objToModel(obj) {
|
|
104
|
+
const metadataList = (0, field_decorator_1.getMapFieldMetadataList)(this);
|
|
105
|
+
obj && Object.keys(obj).forEach(propertyName => {
|
|
106
|
+
if (metadataList && Object.keys(metadataList).some(prop => prop == propertyName)) {
|
|
107
|
+
if (metadataList[propertyName].transformer) {
|
|
108
|
+
if (Array.isArray(obj[propertyName])) {
|
|
109
|
+
this[propertyName] = obj[propertyName].map(item => {
|
|
110
|
+
return item;
|
|
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
|
+
}
|
|
124
|
+
});
|
|
125
|
+
return this;
|
|
126
|
+
}
|
|
127
|
+
exports.objToModel = objToModel;
|
|
128
|
+
/**
|
|
129
|
+
* Check if this instance is empty.
|
|
130
|
+
*
|
|
131
|
+
* @returns true or false
|
|
132
|
+
*/
|
|
133
|
+
function empty() {
|
|
134
|
+
let check = true;
|
|
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;
|
|
142
|
+
}
|
|
143
|
+
exports.empty = empty;
|
|
144
|
+
/**
|
|
145
|
+
* Check if this instance is filled.
|
|
146
|
+
*
|
|
147
|
+
* @returns true or false
|
|
148
|
+
*/
|
|
149
|
+
function filled() {
|
|
150
|
+
if (Object.keys(this)?.length == 0)
|
|
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;
|
|
160
|
+
}
|
|
161
|
+
exports.filled = filled;
|
|
162
|
+
/**
|
|
163
|
+
* GET property value from a string path.
|
|
164
|
+
*
|
|
165
|
+
* @param path String path
|
|
166
|
+
* @returns Value of the property
|
|
167
|
+
*/
|
|
168
|
+
function get(path) {
|
|
169
|
+
let pathReplaced = path.replace(/\[(\w+)\]/g, '.$1');
|
|
170
|
+
let props = pathReplaced.split('.');
|
|
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;
|
|
180
|
+
}
|
|
181
|
+
exports.get = get;
|
|
182
|
+
/**
|
|
183
|
+
* SET property value from a string path.
|
|
184
|
+
*
|
|
185
|
+
* @param path String path
|
|
186
|
+
* @param value Value of the property
|
|
187
|
+
*/
|
|
188
|
+
function set(path, value) {
|
|
189
|
+
path = path.replace(/\[(\w+)\]/g, '.$1');
|
|
190
|
+
let props = path.split('.');
|
|
191
|
+
let obj = this;
|
|
192
|
+
let i;
|
|
193
|
+
for (i = 0; i < props?.length - 1; i++) {
|
|
194
|
+
props?.[i] && (obj = obj?.[props?.[i]]);
|
|
195
|
+
}
|
|
196
|
+
props?.[i] && obj && (obj[props?.[i]] = value);
|
|
197
|
+
}
|
|
198
|
+
exports.set = set;
|
|
199
|
+
/**
|
|
200
|
+
* Deep copy of the object caller
|
|
201
|
+
*/
|
|
202
|
+
function copy() {
|
|
203
|
+
return this.constructorMap(this.toMap());
|
|
204
|
+
}
|
|
205
|
+
exports.copy = copy;
|
|
206
|
+
/**
|
|
207
|
+
* Constructor of the mapper.
|
|
208
|
+
*
|
|
209
|
+
* @param object object to be mapped considering metadata "src", "transformer" and "reverser"
|
|
210
|
+
*/
|
|
211
|
+
function constructorMap(object) {
|
|
212
|
+
const metadataList = (0, field_decorator_1.getMapFieldMetadataList)(this);
|
|
213
|
+
object && Object.keys(object).forEach(propertyName => {
|
|
214
|
+
let metaKeys = metadataList && Object.keys(metadataList).filter(metadata => metadataList[metadata]?.src?.split('.')?.includes(propertyName));
|
|
215
|
+
if (metaKeys?.length) {
|
|
216
|
+
metaKeys.forEach(metaKey => {
|
|
217
|
+
let metaProp = metadataList[metaKey];
|
|
218
|
+
if (metaProp) {
|
|
219
|
+
let props = metaProp.src?.split('.');
|
|
220
|
+
let propsStereoid = props.map(prop => {
|
|
221
|
+
let index = prop.indexOf('[');
|
|
222
|
+
return {
|
|
223
|
+
prop: index > 0 ? prop.substring(0, index) : prop,
|
|
224
|
+
isArray: prop.includes('[') && prop.includes(']'),
|
|
225
|
+
arrIndex: prop.substring(index),
|
|
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
|
+
}
|
|
268
|
+
}
|
|
269
|
+
});
|
|
270
|
+
}
|
|
271
|
+
else {
|
|
272
|
+
let metaKey = metadataList && Object.keys(metadataList).find(metadata => metadataList[metadata]?.src == propertyName);
|
|
273
|
+
if (metaKey) {
|
|
274
|
+
const src = metadataList[metaKey].src || propertyName;
|
|
275
|
+
if (metadataList[metaKey].transformer) {
|
|
276
|
+
this[metaKey] = metadataList[metaKey].transformer(object[src], object);
|
|
277
|
+
}
|
|
278
|
+
else {
|
|
279
|
+
this[metaKey] = object[src];
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
else {
|
|
283
|
+
if (metadataList && metadataList[propertyName]?.transformer) {
|
|
284
|
+
this[propertyName] = metadataList[propertyName].transformer(object[propertyName], object);
|
|
285
|
+
}
|
|
286
|
+
else {
|
|
287
|
+
this[propertyName] = object[propertyName];
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
});
|
|
292
|
+
//MAP CASE initialize = true
|
|
293
|
+
metadataList && Object.keys(metadataList).forEach(metaName => {
|
|
294
|
+
if (metadataList[metaName]?.initialize && metadataList[metaName]?.transformer) {
|
|
295
|
+
this[metaName] = metadataList[metaName]?.transformer(null, object);
|
|
296
|
+
}
|
|
297
|
+
});
|
|
298
|
+
return this;
|
|
299
|
+
}
|
|
300
|
+
exports.constructorMap = constructorMap;
|
package/dist/index.d.ts
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import { MapField } from "./field.decorator";
|
|
2
2
|
import { MapperFactory } from "./mapper";
|
|
3
|
+
import { objToModel, toMap, toModel } from "./mapper-functions";
|
|
3
4
|
import { ClassType } from "./types";
|
|
4
|
-
export { MapField, MapperFactory,
|
|
5
|
+
export { ClassType, MapField, MapperFactory, objToModel, toMap, toModel };
|
|
5
6
|
/**
|
|
6
7
|
* npx tsc
|
|
7
8
|
* npx ts-node src/example.ts
|
package/dist/index.js
CHANGED
|
@@ -1,10 +1,14 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.MapperFactory = exports.MapField = void 0;
|
|
3
|
+
exports.toModel = exports.toMap = exports.objToModel = exports.MapperFactory = exports.MapField = void 0;
|
|
4
4
|
const field_decorator_1 = require("./field.decorator");
|
|
5
5
|
Object.defineProperty(exports, "MapField", { enumerable: true, get: function () { return field_decorator_1.MapField; } });
|
|
6
6
|
const mapper_1 = require("./mapper");
|
|
7
7
|
Object.defineProperty(exports, "MapperFactory", { enumerable: true, get: function () { return mapper_1.MapperFactory; } });
|
|
8
|
+
const mapper_functions_1 = require("./mapper-functions");
|
|
9
|
+
Object.defineProperty(exports, "objToModel", { enumerable: true, get: function () { return mapper_functions_1.objToModel; } });
|
|
10
|
+
Object.defineProperty(exports, "toMap", { enumerable: true, get: function () { return mapper_functions_1.toMap; } });
|
|
11
|
+
Object.defineProperty(exports, "toModel", { enumerable: true, get: function () { return mapper_functions_1.toModel; } });
|
|
8
12
|
/**
|
|
9
13
|
* npx tsc
|
|
10
14
|
* npx ts-node src/example.ts
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { MapperFactory } from "./mapper";
|
|
2
|
+
export declare function toMap(model: MapperFactory): Object;
|
|
3
|
+
export declare function toModel<T extends MapperFactory>(obj: Object): T;
|
|
4
|
+
export declare function objToModel<T extends MapperFactory>(model: MapperFactory, obj: Object): T;
|
|
5
|
+
export declare function copy<T extends MapperFactory>(model: MapperFactory): T;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.copy = exports.objToModel = exports.toModel = exports.toMap = void 0;
|
|
4
|
+
const mapper_1 = require("./mapper");
|
|
5
|
+
function toMap(model) {
|
|
6
|
+
return model.toMap();
|
|
7
|
+
}
|
|
8
|
+
exports.toMap = toMap;
|
|
9
|
+
function toModel(obj) {
|
|
10
|
+
return new mapper_1.MapperFactory(obj);
|
|
11
|
+
}
|
|
12
|
+
exports.toModel = toModel;
|
|
13
|
+
function objToModel(model, obj) {
|
|
14
|
+
return model.objToModel(obj);
|
|
15
|
+
}
|
|
16
|
+
exports.objToModel = objToModel;
|
|
17
|
+
function copy(model) {
|
|
18
|
+
return new mapper_1.MapperFactory(model.toMap());
|
|
19
|
+
}
|
|
20
|
+
exports.copy = copy;
|