4
4
[ ![ codecov] ( https://codecov.io/gh/typestack/class-transformer/branch/develop/graph/badge.svg )] ( https://codecov.io/gh/typestack/class-transformer )
5
5
[ ![ npm version] ( https://badge.fury.io/js/class-transformer.svg )] ( https://badge.fury.io/js/class-transformer )
6
6
7
- Its ES6 and Typescript era. Nowadays you are working with classes and constructor objects more then ever.
7
+ Its ES6 and Typescript era. Nowadays you are working with classes and constructor objects more than ever.
8
8
Class-transformer allows you to transform plain object to some instance of class and versa.
9
9
Also it allows to serialize / deserialize object based on criteria.
10
10
This tool is super useful on both frontend and backend.
@@ -131,17 +131,17 @@ So what to do? How to make a `users` array of instances of `User` objects instea
131
131
Solution is to create new instances of User object and manually copy all properties to new objects.
132
132
But things may go wrong very fast once you have a more complex object hierarchy.
133
133
134
- Alternatives? Yes, you can use class-transformer. Purpose of this library is to help you to map you plain javascript
134
+ Alternatives? Yes, you can use class-transformer. Purpose of this library is to help you to map your plain javascript
135
135
objects to the instances of classes you have.
136
136
137
137
This library also great for models exposed in your APIs,
138
138
because it provides a great tooling to control what your models are exposing in your API.
139
- Here is example how it will look like:
139
+ Here is an example how it will look like:
140
140
141
141
``` typescript
142
142
fetch (' users.json' ).then ((users : Object []) => {
143
143
const realUsers = plainToClass (User , users );
144
- // now each user in realUsers is instance of User class
144
+ // now each user in realUsers is an instance of User class
145
145
});
146
146
```
147
147
@@ -226,7 +226,7 @@ let users = plainToClass(User, userJson); // to convert user plain object a sing
226
226
227
227
### plainToClassFromExist[ ⬆] ( #table-of-contents )
228
228
229
- This method transforms a plain object into a instance using a already filled Object which is a instance from the target class.
229
+ This method transforms a plain object into an instance using an already filled Object which is an instance of the target class.
230
230
231
231
``` typescript
232
232
const defaultUser = new User ();
@@ -246,19 +246,19 @@ let photo = classToPlain(photo);
246
246
247
247
### classToClass[ ⬆] ( #table-of-contents )
248
248
249
- This method transforms your class object into new instance of the class object.
250
- This maybe treated as deep clone of your objects.
249
+ This method transforms your class object into a new instance of the class object.
250
+ This may be treated as deep clone of your objects.
251
251
252
252
``` typescript
253
253
import { classToClass } from ' class-transformer' ;
254
254
let photo = classToClass (photo );
255
255
```
256
256
257
- You can also use a ` ignoreDecorators ` option in transformation options to ignore all decorators you classes is using.
257
+ You can also use an ` ignoreDecorators ` option in transformation options to ignore all decorators you classes is using.
258
258
259
259
### serialize[ ⬆] ( #table-of-contents )
260
260
261
- You can serialize your model right to the json using ` serialize ` method:
261
+ You can serialize your model right to json using ` serialize ` method:
262
262
263
263
``` typescript
264
264
import { serialize } from ' class-transformer' ;
@@ -269,14 +269,14 @@ let photo = serialize(photo);
269
269
270
270
### deserialize and deserializeArray[ ⬆] ( #table-of-contents )
271
271
272
- You can deserialize your model to from a json using ` deserialize ` method:
272
+ You can deserialize your model from json using the ` deserialize ` method:
273
273
274
274
``` typescript
275
275
import { deserialize } from ' class-transformer' ;
276
276
let photo = deserialize (Photo , photo );
277
277
```
278
278
279
- To make deserialization to work with arrays use ` deserializeArray ` method:
279
+ To make deserialization work with arrays, use the ` deserializeArray ` method:
280
280
281
281
``` typescript
282
282
import { deserializeArray } from ' class-transformer' ;
@@ -342,7 +342,7 @@ console.log(plainToClass(User, fromPlainUser, { excludeExtraneousValues: true })
342
342
## Working with nested objects[ ⬆] ( #table-of-contents )
343
343
344
344
When you are trying to transform objects that have nested objects,
345
- its required to known what type of object you are trying to transform.
345
+ it's required to known what type of object you are trying to transform.
346
346
Since Typescript does not have good reflection abilities yet,
347
347
we should implicitly specify what type of object each property contain.
348
348
This is done using ` @Type ` decorator.
@@ -374,8 +374,8 @@ let album = plainToClass(Album, albumJson);
374
374
### Providing more than one type option[ ⬆] ( #table-of-contents )
375
375
376
376
In case the nested object can be of different types, you can provide an additional options object,
377
- that specifies a discriminator. The discriminator option must define a ` property ` that holds the sub
378
- type name for the object and the possible ` subTypes ` , the nested object can converted to. A sub type
377
+ that specifies a discriminator. The discriminator option must define a ` property ` that holds the subtype
378
+ name for the object and the possible ` subTypes ` that the nested object can converted to. A sub type
379
379
has a ` value ` , that holds the constructor of the Type and the ` name ` , that can match with the ` property `
380
380
of the discriminator.
381
381
@@ -444,7 +444,7 @@ in the options to keep the discriminator property also inside your resulting cla
444
444
445
445
## Exposing getters and method return values[ ⬆] ( #table-of-contents )
446
446
447
- You can expose what your getter or method return by setting a ` @Expose() ` decorator to those getters or methods:
447
+ You can expose what your getter or method return by setting an ` @Expose() ` decorator to those getters or methods:
448
448
449
449
``` typescript
450
450
import { Expose } from ' class-transformer' ;
@@ -469,8 +469,8 @@ export class User {
469
469
470
470
## Exposing properties with different names[ ⬆] ( #table-of-contents )
471
471
472
- If you want to expose some of properties with a different name,
473
- you can do it by specifying a ` name ` option to ` @Expose ` decorator:
472
+ If you want to expose some of the properties with a different name,
473
+ you can do that by specifying a ` name ` option to ` @Expose ` decorator:
474
474
475
475
``` typescript
476
476
import { Expose } from ' class-transformer' ;
@@ -511,7 +511,7 @@ export class User {
511
511
}
512
512
```
513
513
514
- Now when you transform a User, ` password ` property will be skipped and not be included in the transformed result.
514
+ Now when you transform a User, the ` password ` property will be skipped and not be included in the transformed result.
515
515
516
516
## Skipping depend of operation[ ⬆] ( #table-of-contents )
517
517
@@ -530,7 +530,7 @@ export class User {
530
530
}
531
531
```
532
532
533
- Now ` password ` property will be excluded only during ` classToPlain ` operation. Oppositely , use ` toClassOnly ` option.
533
+ Now ` password ` property will be excluded only during ` classToPlain ` operation. Vice versa , use the ` toClassOnly ` option.
534
534
535
535
## Skipping all properties of the class[ ⬆] ( #table-of-contents )
536
536
@@ -760,7 +760,7 @@ export class Photo {
760
760
id: number ;
761
761
762
762
@Type (() => Date )
763
- @Transform (value => moment (value ), { toClassOnly: true })
763
+ @Transform (({ value }) => moment (value ), { toClassOnly: true })
764
764
date: Moment ;
765
765
}
766
766
```
@@ -773,15 +773,17 @@ it will convert a date value in your photo object to moment date.
773
773
774
774
The ` @Transform ` decorator is given more arguments to let you configure how you want the transformation to be done.
775
775
776
- ```
777
- @Transform((value, obj, type) => value)
776
+ ``` ts
777
+ @Transform (({ value , key , obj , type } ) => value )
778
778
```
779
779
780
- | Argument | Description |
781
- | -------- | --------------------------------------------- |
782
- | ` value ` | The property value before the transformation. |
783
- | ` obj ` | The transformation source object. |
784
- | ` type ` | The transformation type. |
780
+ | Argument | Description |
781
+ | --------- | ------------------------------------------------------- |
782
+ | ` value ` | The property value before the transformation. |
783
+ | ` key ` | The name of the transformed property. |
784
+ | ` obj ` | The transformation source object. |
785
+ | ` type ` | The transformation type. |
786
+ | ` options ` | The options object passed to the transformation method. |
785
787
786
788
## Other decorators[ ⬆] ( #table-of-contents )
787
789
0 commit comments