@airpower/transformer 0.0.6 → 0.0.7
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/main.js +8 -10
- package/dist/transformer/Transformer.d.ts +2 -2
- package/package.json +1 -1
package/dist/main.js
CHANGED
|
@@ -138,8 +138,7 @@ class Transformer {
|
|
|
138
138
|
* @param json `JSON`
|
|
139
139
|
*/
|
|
140
140
|
static fromJson(json = {}) {
|
|
141
|
-
|
|
142
|
-
return Transformer.parse(json, instance);
|
|
141
|
+
return Transformer.parse(json, this);
|
|
143
142
|
}
|
|
144
143
|
/**
|
|
145
144
|
* ### 从 `JSON` 数组转换到当前类的对象数组
|
|
@@ -150,12 +149,10 @@ class Transformer {
|
|
|
150
149
|
const instanceList = [];
|
|
151
150
|
if (Array.isArray(jsonArray)) {
|
|
152
151
|
for (let i = 0; i < jsonArray.length; i += 1) {
|
|
153
|
-
|
|
154
|
-
instanceList.push(Transformer.parse(jsonArray[i], instance));
|
|
152
|
+
instanceList.push(Transformer.parse(jsonArray[i], this));
|
|
155
153
|
}
|
|
156
154
|
} else {
|
|
157
|
-
|
|
158
|
-
instanceList.push(Transformer.parse(jsonArray, instance));
|
|
155
|
+
instanceList.push(Transformer.parse(jsonArray, this));
|
|
159
156
|
}
|
|
160
157
|
return instanceList;
|
|
161
158
|
}
|
|
@@ -175,9 +172,10 @@ class Transformer {
|
|
|
175
172
|
* ### 转换 `JSON` 为实体
|
|
176
173
|
* 会自动进行数据别名转换
|
|
177
174
|
* @param json `JSON`
|
|
178
|
-
* @param
|
|
175
|
+
* @param Class 实体类
|
|
179
176
|
*/
|
|
180
|
-
static parse(json = {},
|
|
177
|
+
static parse(json = {}, Class) {
|
|
178
|
+
const instance = new Class();
|
|
181
179
|
const fieldList = Object.keys(instance);
|
|
182
180
|
for (const field of fieldList) {
|
|
183
181
|
const jsonKey = this.getJsonKey(instance, field);
|
|
@@ -200,7 +198,7 @@ class Transformer {
|
|
|
200
198
|
if (typeof fieldData === "object" && Array.isArray(fieldData)) {
|
|
201
199
|
for (let i = 0; i < fieldData.length; i += 1) {
|
|
202
200
|
if (FieldTypeClass) {
|
|
203
|
-
fieldValueList[i] = this.parse(fieldData[i],
|
|
201
|
+
fieldValueList[i] = this.parse(fieldData[i], FieldTypeClass);
|
|
204
202
|
}
|
|
205
203
|
}
|
|
206
204
|
}
|
|
@@ -224,7 +222,7 @@ class Transformer {
|
|
|
224
222
|
instance[field] = !!fieldData;
|
|
225
223
|
break;
|
|
226
224
|
default:
|
|
227
|
-
instance[field] = this.parse(fieldData,
|
|
225
|
+
instance[field] = this.parse(fieldData, FieldTypeClass);
|
|
228
226
|
}
|
|
229
227
|
}
|
|
230
228
|
for (const fieldKey of fieldList) {
|
|
@@ -27,9 +27,9 @@ export declare class Transformer {
|
|
|
27
27
|
* ### 转换 `JSON` 为实体
|
|
28
28
|
* 会自动进行数据别名转换
|
|
29
29
|
* @param json `JSON`
|
|
30
|
-
* @param
|
|
30
|
+
* @param Class 实体类
|
|
31
31
|
*/
|
|
32
|
-
static parse<T extends Transformer>(json: IJson | undefined,
|
|
32
|
+
static parse<T extends Transformer>(json: IJson | undefined, Class: ITransformerConstructor<T>): T;
|
|
33
33
|
/**
|
|
34
34
|
* ### 获取 JSON 的 key
|
|
35
35
|
* @param instance 目标实例
|