Skip to content

Commit 1065739

Browse files
style(): run prettier
1 parent cb847b3 commit 1065739

24 files changed

+994
-266
lines changed

.prettierrc.yml

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
1-
printWidth: 120
2-
tabWidth: 2
3-
useTabs: false
4-
semi: true
5-
singleQuote: true
6-
trailingComma: es5
7-
bracketSpacing: true
1+
82
arrowParens: avoid
3+
singleQuote: true
4+
trailingComma: es5

README.md

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,9 @@ const fromPlainUser = {
327327
lastName: 'Khudoiberdiev',
328328
};
329329

330-
console.log(plainToClass(User, fromPlainUser, { excludeExtraneousValues: true }));
330+
console.log(
331+
plainToClass(User, fromPlainUser, { excludeExtraneousValues: true })
332+
);
331333

332334
// User {
333335
// id: undefined,
@@ -852,8 +854,16 @@ class MyPayload {
852854
prop: string;
853855
}
854856

855-
const result1 = plainToClass(MyPayload, { prop: 1234 }, { enableImplicitConversion: true });
856-
const result2 = plainToClass(MyPayload, { prop: 1234 }, { enableImplicitConversion: false });
857+
const result1 = plainToClass(
858+
MyPayload,
859+
{ prop: 1234 },
860+
{ enableImplicitConversion: true }
861+
);
862+
const result2 = plainToClass(
863+
MyPayload,
864+
{ prop: 1234 },
865+
{ enableImplicitConversion: false }
866+
);
857867

858868
/**
859869
* result1 will be `{ prop: "1234" }` - notice how the prop value has been converted to string.

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

sample/sample4-generics/User.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,12 @@ export class User {
1010
@Exclude()
1111
password: string;
1212

13-
constructor(id: number, firstName: string, lastName: string, password: string) {
13+
constructor(
14+
id: number,
15+
firstName: string,
16+
lastName: string,
17+
password: string
18+
) {
1419
this.id = id;
1520
this.firstName = firstName;
1621
this.lastName = lastName;

sample/sample4-generics/app.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,18 @@ import 'es6-shim';
22
import 'reflect-metadata';
33
import { SimpleCollection } from './SimpleCollection';
44
import { User } from './User';
5-
import { classToPlain, plainToClass, plainToClassFromExist } from '../../src/index';
5+
import {
6+
classToPlain,
7+
plainToClass,
8+
plainToClassFromExist,
9+
} from '../../src/index';
610
import { SuperCollection } from './SuperCollection';
711

812
let collection = new SimpleCollection<User>();
9-
collection.items = [new User(1, 'Johny', 'Cage', '*******'), new User(2, 'Dima', 'Cage', '*******')];
13+
collection.items = [
14+
new User(1, 'Johny', 'Cage', '*******'),
15+
new User(2, 'Dima', 'Cage', '*******'),
16+
];
1017
collection.count = 2;
1118

1219
// using generics works only for classToPlain operations, since in runtime we can
@@ -33,4 +40,6 @@ let collectionJson = {
3340
],
3441
};
3542

36-
console.log(plainToClassFromExist(new SuperCollection<User>(User), collectionJson));
43+
console.log(
44+
plainToClassFromExist(new SuperCollection<User>(User), collectionJson)
45+
);

src/ClassTransformer.ts

Lines changed: 128 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,33 @@ export class ClassTransformer {
1212
/**
1313
* Converts class (constructor) object to plain (literal) object. Also works with arrays.
1414
*/
15-
classToPlain<T extends Record<string, any>>(object: T, options?: ClassTransformOptions): Record<string, any>;
16-
classToPlain<T extends Record<string, any>>(object: T[], options?: ClassTransformOptions): Record<string, any>[];
15+
classToPlain<T extends Record<string, any>>(
16+
object: T,
17+
options?: ClassTransformOptions
18+
): Record<string, any>;
19+
classToPlain<T extends Record<string, any>>(
20+
object: T[],
21+
options?: ClassTransformOptions
22+
): Record<string, any>[];
1723
classToPlain<T extends Record<string, any>>(
1824
object: T | T[],
1925
options?: ClassTransformOptions
2026
): Record<string, any> | Record<string, any>[] {
21-
const executor = new TransformOperationExecutor(TransformationType.CLASS_TO_PLAIN, {
22-
...defaultOptions,
23-
...options,
24-
});
25-
return executor.transform(undefined, object, undefined, undefined, undefined, undefined);
27+
const executor = new TransformOperationExecutor(
28+
TransformationType.CLASS_TO_PLAIN,
29+
{
30+
...defaultOptions,
31+
...options,
32+
}
33+
);
34+
return executor.transform(
35+
undefined,
36+
object,
37+
undefined,
38+
undefined,
39+
undefined,
40+
undefined
41+
);
2642
}
2743

2844
/**
@@ -45,11 +61,21 @@ export class ClassTransformer {
4561
plainObject: P | P[],
4662
options?: ClassTransformOptions
4763
): T | T[] {
48-
const executor = new TransformOperationExecutor(TransformationType.CLASS_TO_PLAIN, {
49-
...defaultOptions,
50-
...options,
51-
});
52-
return executor.transform(plainObject, object, undefined, undefined, undefined, undefined);
64+
const executor = new TransformOperationExecutor(
65+
TransformationType.CLASS_TO_PLAIN,
66+
{
67+
...defaultOptions,
68+
...options,
69+
}
70+
);
71+
return executor.transform(
72+
plainObject,
73+
object,
74+
undefined,
75+
undefined,
76+
undefined,
77+
undefined
78+
);
5379
}
5480

5581
/**
@@ -70,11 +96,21 @@ export class ClassTransformer {
7096
plain: V | V[],
7197
options?: ClassTransformOptions
7298
): T | T[] {
73-
const executor = new TransformOperationExecutor(TransformationType.PLAIN_TO_CLASS, {
74-
...defaultOptions,
75-
...options,
76-
});
77-
return executor.transform(undefined, plain, cls, undefined, undefined, undefined);
99+
const executor = new TransformOperationExecutor(
100+
TransformationType.PLAIN_TO_CLASS,
101+
{
102+
...defaultOptions,
103+
...options,
104+
}
105+
);
106+
return executor.transform(
107+
undefined,
108+
plain,
109+
cls,
110+
undefined,
111+
undefined,
112+
undefined
113+
);
78114
}
79115

80116
/**
@@ -87,17 +123,31 @@ export class ClassTransformer {
87123
plain: V,
88124
options?: ClassTransformOptions
89125
): T;
90-
plainToClassFromExist<T extends Record<string, any>, V>(clsObject: T, plain: V, options?: ClassTransformOptions): T[];
126+
plainToClassFromExist<T extends Record<string, any>, V>(
127+
clsObject: T,
128+
plain: V,
129+
options?: ClassTransformOptions
130+
): T[];
91131
plainToClassFromExist<T extends Record<string, any>, V>(
92132
clsObject: T,
93133
plain: V | V[],
94134
options?: ClassTransformOptions
95135
): T | T[] {
96-
const executor = new TransformOperationExecutor(TransformationType.PLAIN_TO_CLASS, {
97-
...defaultOptions,
98-
...options,
99-
});
100-
return executor.transform(clsObject, plain, undefined, undefined, undefined, undefined);
136+
const executor = new TransformOperationExecutor(
137+
TransformationType.PLAIN_TO_CLASS,
138+
{
139+
...defaultOptions,
140+
...options,
141+
}
142+
);
143+
return executor.transform(
144+
clsObject,
145+
plain,
146+
undefined,
147+
undefined,
148+
undefined,
149+
undefined
150+
);
101151
}
102152

103153
/**
@@ -106,26 +156,58 @@ export class ClassTransformer {
106156
classToClass<T>(object: T, options?: ClassTransformOptions): T;
107157
classToClass<T>(object: T[], options?: ClassTransformOptions): T[];
108158
classToClass<T>(object: T | T[], options?: ClassTransformOptions): T | T[] {
109-
const executor = new TransformOperationExecutor(TransformationType.CLASS_TO_CLASS, {
110-
...defaultOptions,
111-
...options,
112-
});
113-
return executor.transform(undefined, object, undefined, undefined, undefined, undefined);
159+
const executor = new TransformOperationExecutor(
160+
TransformationType.CLASS_TO_CLASS,
161+
{
162+
...defaultOptions,
163+
...options,
164+
}
165+
);
166+
return executor.transform(
167+
undefined,
168+
object,
169+
undefined,
170+
undefined,
171+
undefined,
172+
undefined
173+
);
114174
}
115175

116176
/**
117177
* Converts class (constructor) object to plain (literal) object.
118178
* Uses given plain object as source object (it means fills given plain object with data from class object).
119179
* Also works with arrays.
120180
*/
121-
classToClassFromExist<T>(object: T, fromObject: T, options?: ClassTransformOptions): T;
122-
classToClassFromExist<T>(object: T, fromObjects: T[], options?: ClassTransformOptions): T[];
123-
classToClassFromExist<T>(object: T, fromObject: T | T[], options?: ClassTransformOptions): T | T[] {
124-
const executor = new TransformOperationExecutor(TransformationType.CLASS_TO_CLASS, {
125-
...defaultOptions,
126-
...options,
127-
});
128-
return executor.transform(fromObject, object, undefined, undefined, undefined, undefined);
181+
classToClassFromExist<T>(
182+
object: T,
183+
fromObject: T,
184+
options?: ClassTransformOptions
185+
): T;
186+
classToClassFromExist<T>(
187+
object: T,
188+
fromObjects: T[],
189+
options?: ClassTransformOptions
190+
): T[];
191+
classToClassFromExist<T>(
192+
object: T,
193+
fromObject: T | T[],
194+
options?: ClassTransformOptions
195+
): T | T[] {
196+
const executor = new TransformOperationExecutor(
197+
TransformationType.CLASS_TO_CLASS,
198+
{
199+
...defaultOptions,
200+
...options,
201+
}
202+
);
203+
return executor.transform(
204+
fromObject,
205+
object,
206+
undefined,
207+
undefined,
208+
undefined,
209+
undefined
210+
);
129211
}
130212

131213
/**
@@ -140,15 +222,23 @@ export class ClassTransformer {
140222
/**
141223
* Deserializes given JSON string to a object of the given class.
142224
*/
143-
deserialize<T>(cls: ClassConstructor<T>, json: string, options?: ClassTransformOptions): T {
225+
deserialize<T>(
226+
cls: ClassConstructor<T>,
227+
json: string,
228+
options?: ClassTransformOptions
229+
): T {
144230
const jsonObject: T = JSON.parse(json);
145231
return this.plainToClass(cls, jsonObject, options);
146232
}
147233

148234
/**
149235
* Deserializes given JSON string to an array of objects of the given class.
150236
*/
151-
deserializeArray<T>(cls: ClassConstructor<T>, json: string, options?: ClassTransformOptions): T[] {
237+
deserializeArray<T>(
238+
cls: ClassConstructor<T>,
239+
json: string,
240+
options?: ClassTransformOptions
241+
): T[] {
152242
const jsonObject: any[] = JSON.parse(json);
153243
return this.plainToClass(cls, jsonObject, options);
154244
}

0 commit comments

Comments
 (0)