@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 CHANGED
@@ -138,8 +138,7 @@ class Transformer {
138
138
  * @param json `JSON`
139
139
  */
140
140
  static fromJson(json = {}) {
141
- const instance = new this();
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
- const instance = new this();
154
- instanceList.push(Transformer.parse(jsonArray[i], instance));
152
+ instanceList.push(Transformer.parse(jsonArray[i], this));
155
153
  }
156
154
  } else {
157
- const instance = new this();
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 instance 实体
175
+ * @param Class 实体类
179
176
  */
180
- static parse(json = {}, instance) {
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], new FieldTypeClass());
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, new FieldTypeClass());
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 instance 实体
30
+ * @param Class 实体类
31
31
  */
32
- static parse<T extends Transformer>(json: IJson | undefined, instance: T): T;
32
+ static parse<T extends Transformer>(json: IJson | undefined, Class: ITransformerConstructor<T>): T;
33
33
  /**
34
34
  * ### 获取 JSON 的 key
35
35
  * @param instance 目标实例
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@airpower/transformer",
3
3
  "type": "module",
4
- "version": "0.0.6",
4
+ "version": "0.0.7",
5
5
  "description": "AirPower-Transformer 是一个基于 TypeScript 的数据转换器,提供了从 JSON 对象到 类实例 对象之间互相转换的系列装饰器和方法。",
6
6
  "author": {
7
7
  "name": "Hamm",