1
1
# class-transformer
2
2
3
- [ ![ Build Status] ( https://travis-ci.org/pleerock /class-transformer.svg?branch=master )] ( https://travis-ci.org/pleerock /class-transformer )
4
- [ ![ codecov] ( https://codecov.io/gh/pleerock /class-transformer/branch/master/graph/badge.svg )] ( https://codecov.io/gh/pleerock /class-transformer )
3
+ [ ![ Build Status] ( https://travis-ci.org/typestack /class-transformer.svg?branch=master )] ( https://travis-ci.org/typestack /class-transformer )
4
+ [ ![ codecov] ( https://codecov.io/gh/typestack /class-transformer/branch/master/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
- [ ![ Dependency Status] ( https://david-dm.org/pleerock/class-transformer.svg )] ( https://david-dm.org/pleerock/class-transformer )
7
- [ ![ devDependency Status] ( https://david-dm.org/pleerock/class-transformer/dev-status.svg )] ( https://david-dm.org/pleerock/class-transformer#info=devDependencies )
8
- [ ![ Join the chat at https://gitter.im/pleerock/class-transformer ] ( https://badges.gitter.im/pleerock/class-transformer.svg )] ( https://gitter.im/pleerock/class-transformer?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge )
6
+ [ ![ Dependency Status] ( https://david-dm.org/typestack/class-transformer.svg )] ( https://david-dm.org/typestack/class-transformer )
7
+ [ ![ Join the chat at https://gitter.im/typestack/class-transformer ] ( https://badges.gitter.im/typestack/class-transformer.svg )] ( https://gitter.im/typestack/class-transformer?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge )
9
8
10
9
Its ES6 and Typescript era. Nowadays you are working with classes and constructor objects more then ever.
11
10
Class-transformer allows you to transform plain object to some instance of class and versa.
@@ -225,14 +224,14 @@ You can deserialize your model to from a json using `deserialize` method:
225
224
226
225
` ` ` typescript
227
226
import {deserialize} from "class-transformer";
228
- let photo = deserialize(photo);
227
+ let photo = deserialize(Photo, photo);
229
228
` ` `
230
229
231
230
To make deserialization to work with arrays use ` deserializeArray ` method :
232
231
233
232
` ` ` typescript
234
233
import {deserializeArray} from "class-transformer";
235
- let photos = deserializeArray(photos);
234
+ let photos = deserializeArray(Photo, photos);
236
235
` ` `
237
236
238
237
## Working with nested objects
@@ -566,8 +565,33 @@ export class Photo {
566
565
567
566
Library will handle proper transformation automatically.
568
567
568
+ ES6 collections ` Set ` and ` Map ` also require the ` @Type ` decorator:
569
+
570
+ ``` typescript
571
+ export class Skill {
572
+ name: string ;
573
+ }
574
+
575
+ export class Weapon {
576
+ name: string ;
577
+ range: number ;
578
+ }
579
+
580
+ export class Player {
581
+ name: string ;
582
+
583
+ @Type (() => Skill )
584
+ skills: Set <Skill >;
585
+
586
+ @Type (() => Weapon )
587
+ weapons: Map <string , Weapon >;
588
+ }
589
+ ```
590
+
569
591
## Additional data transformation
570
592
593
+ ### Basic usage
594
+
571
595
You can perform additional data transformation using ` @Transform ` decorator.
572
596
For example, you want to make your ` Date ` object to be a ` moment ` object when you are
573
597
transforming object from plain to class:
@@ -591,6 +615,20 @@ Now when you call `plainToClass` and send a plain representation of the Photo ob
591
615
it will convert a date value in your photo object to moment date.
592
616
` @Transform ` decorator also supports groups and versioning.
593
617
618
+ ### Advanced usage
619
+
620
+ The ` @Transform ` decorator is given more arguments to let you configure how you want the transformation to be done.
621
+
622
+ ```
623
+ @Transform((value, obj, type) => value)
624
+ ```
625
+
626
+ | Argument | Description
627
+ | --------------------| ---------------------------------------------------------------------------------|
628
+ | ` value ` | The property value before the transformation.
629
+ | ` obj ` | The transformation source object.
630
+ | ` type ` | The transformation type.
631
+
594
632
## Other decorators
595
633
| Signature | Example | Description
596
634
| --------------------| ------------------------------------------| ---------------------------------------------|
@@ -644,7 +682,7 @@ the exposed variables. email property is also exposed becuase we metioned the gr
644
682
## Working with generics
645
683
646
684
Generics are not supported because TypeScript does not have good reflection abilities yet.
647
- Once TypeScript team provide us better runtime type reelection tools, generics will be implemented.
685
+ Once TypeScript team provide us better runtime type reflection tools, generics will be implemented.
648
686
There are some tweaks however you can use, that maybe can solve your problem.
649
687
[ Checkout this example.] ( https://github.com/pleerock/class-transformer/tree/master/sample/sample4-generics )
650
688
@@ -667,7 +705,7 @@ this.http
667
705
.map (res => res .json ())
668
706
.map (res => plainToClass (User , res as Object []))
669
707
.subscribe (users => {
670
- // now "users" is type of User[] and each user have getName() and isAdult() methods available
708
+ // now "users" is type of User[] and each user has getName() and isAdult() methods available
671
709
console .log (users );
672
710
});
673
711
```
0 commit comments