@airpower/transformer 0.0.5 → 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,21 +149,20 @@ 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
  }
162
159
  /**
163
160
  * ### 创建一个当前类的实例
161
+ * @param Class 类
164
162
  * @param recoverBy `可选` 初始化用于覆盖对象实例的 `JSON`
165
163
  */
166
- static newInstance(recoverBy) {
167
- const instance = new this();
164
+ static newInstance(Class, recoverBy) {
165
+ const instance = new Class();
168
166
  if (recoverBy) {
169
167
  return instance.recoverBy(recoverBy);
170
168
  }
@@ -174,9 +172,10 @@ class Transformer {
174
172
  * ### 转换 `JSON` 为实体
175
173
  * 会自动进行数据别名转换
176
174
  * @param json `JSON`
177
- * @param instance 实体
175
+ * @param Class 实体类
178
176
  */
179
- static parse(json = {}, instance) {
177
+ static parse(json = {}, Class) {
178
+ const instance = new Class();
180
179
  const fieldList = Object.keys(instance);
181
180
  for (const field of fieldList) {
182
181
  const jsonKey = this.getJsonKey(instance, field);
@@ -199,7 +198,7 @@ class Transformer {
199
198
  if (typeof fieldData === "object" && Array.isArray(fieldData)) {
200
199
  for (let i = 0; i < fieldData.length; i += 1) {
201
200
  if (FieldTypeClass) {
202
- fieldValueList[i] = this.parse(fieldData[i], new FieldTypeClass());
201
+ fieldValueList[i] = this.parse(fieldData[i], FieldTypeClass);
203
202
  }
204
203
  }
205
204
  }
@@ -223,7 +222,7 @@ class Transformer {
223
222
  instance[field] = !!fieldData;
224
223
  break;
225
224
  default:
226
- instance[field] = this.parse(fieldData, new FieldTypeClass());
225
+ instance[field] = this.parse(fieldData, FieldTypeClass);
227
226
  }
228
227
  }
229
228
  for (const fieldKey of fieldList) {
@@ -19,16 +19,17 @@ export declare class Transformer {
19
19
  static fromJsonArray<T extends Transformer>(this: ITransformerConstructor<T>, jsonArray?: IJson | IJson[]): T[];
20
20
  /**
21
21
  * ### 创建一个当前类的实例
22
+ * @param Class 类
22
23
  * @param recoverBy `可选` 初始化用于覆盖对象实例的 `JSON`
23
24
  */
24
- static newInstance<T extends Transformer>(this: ITransformerConstructor<T>, recoverBy?: IJson): T;
25
+ static newInstance<T extends Transformer>(Class: ITransformerConstructor<T>, recoverBy?: IJson): T;
25
26
  /**
26
27
  * ### 转换 `JSON` 为实体
27
28
  * 会自动进行数据别名转换
28
29
  * @param json `JSON`
29
- * @param instance 实体
30
+ * @param Class 实体类
30
31
  */
31
- static parse<T extends Transformer>(json: IJson | undefined, instance: T): T;
32
+ static parse<T extends Transformer>(json: IJson | undefined, Class: ITransformerConstructor<T>): T;
32
33
  /**
33
34
  * ### 获取 JSON 的 key
34
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.5",
4
+ "version": "0.0.7",
5
5
  "description": "AirPower-Transformer 是一个基于 TypeScript 的数据转换器,提供了从 JSON 对象到 类实例 对象之间互相转换的系列装饰器和方法。",
6
6
  "author": {
7
7
  "name": "Hamm",