Skip to content

Commit 915159b

Browse files
committed
docs: fix examples and table of contents
1 parent 1b02c24 commit 915159b

File tree

1 file changed

+18
-26
lines changed

1 file changed

+18
-26
lines changed

README.md

Lines changed: 18 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@ Source code is available [here](https://github.com/pleerock/class-transformer-de
1919
- [Node.js](#nodejs)
2020
- [Browser](#browser)
2121
- [Methods](#methods)
22-
- [plainToClass](#plaintoclass)
23-
- [plainToClassFromExist](#plaintoclassfromexist)
24-
- [classToPlain](#classtoplain)
25-
- [classToClass](#classtoclass)
26-
- [serialize](#serialize)
27-
- [deserialize and deserializeArray](#deserialize-and-deserializearray)
22+
- [plainToClass](#plaintoclass)
23+
- [plainToClassFromExist](#plaintoclassfromexist)
24+
- [classToPlain](#classtoplain)
25+
- [classToClass](#classtoclass)
26+
- [serialize](#serialize)
27+
- [deserialize and deserializeArray](#deserialize-and-deserializearray)
2828
- [Enforcing type-safe instance](#enforcing-type-safe-instance)
2929
- [Working with nested objects](#working-with-nested-objects)
3030
- [Providing more than one type option](#providing-more-than-one-type-option)
@@ -36,7 +36,7 @@ Source code is available [here](https://github.com/pleerock/class-transformer-de
3636
- [Skipping private properties, or some prefixed properties](#skipping-private-properties-or-some-prefixed-properties)
3737
- [Using groups to control excluded properties](#using-groups-to-control-excluded-properties)
3838
- [Using versioning to control exposed and excluded properties](#using-versioning-to-control-exposed-and-excluded-properties)
39-
- [Сonverting date strings into Date objects](#%d0%a1onverting-date-strings-into-date-objects)
39+
- [Сonverting date strings into Date objects](#сonverting-date-strings-into-date-objects)
4040
- [Working with arrays](#working-with-arrays)
4141
- [Additional data transformation](#additional-data-transformation)
4242
- [Basic usage](#basic-usage)
@@ -211,7 +211,7 @@ Now you can use `users[0].getName()` and `users[0].isAdult()` methods.
211211

212212
## Methods
213213

214-
#### plainToClass
214+
### plainToClass
215215

216216
This method transforms a plain javascript object to instance of specific class.
217217

@@ -221,7 +221,7 @@ import {plainToClass} from "class-transformer";
221221
let users = plainToClass(User, userJson); // to convert user plain object a single user. also supports arrays
222222
```
223223

224-
#### plainToClassFromExist
224+
### plainToClassFromExist
225225

226226
This method transforms a plain object into a instance using a already filled Object which is a instance from the target class.
227227

@@ -232,7 +232,7 @@ defaultUser.role = 'user';
232232
let mixedUser = plainToClassFromExist(defaultUser, user); // mixed user should have the value role = user when no value is set otherwise.
233233
```
234234

235-
#### classToPlain
235+
### classToPlain
236236

237237
This method transforms your class object back to plain javascript object, that can be `JSON.stringify` later.
238238

@@ -241,7 +241,7 @@ import {classToPlain} from "class-transformer";
241241
let photo = classToPlain(photo);
242242
```
243243

244-
#### classToClass
244+
### classToClass
245245

246246
This method transforms your class object into new instance of the class object.
247247
This maybe treated as deep clone of your objects.
@@ -253,7 +253,7 @@ let photo = classToClass(photo);
253253

254254
You can also use a `ignoreDecorators` option in transformation options to ignore all decorators you classes is using.
255255

256-
#### serialize
256+
### serialize
257257

258258
You can serialize your model right to the json using `serialize` method:
259259

@@ -264,7 +264,7 @@ let photo = serialize(photo);
264264

265265
`serialize` works with both arrays and non-arrays.
266266

267-
#### deserialize and deserializeArray
267+
### deserialize and deserializeArray
268268

269269
You can deserialize your model to from a json using `deserialize` method:
270270

@@ -580,7 +580,7 @@ You can pass any number of prefixes and all properties that begin with these pre
580580
For example:
581581

582582
```typescript
583-
import {Expose} from "class-transformer";
583+
import {Expose, classToPlain} from "class-transformer";
584584

585585
export class User {
586586

@@ -596,15 +596,15 @@ export class User {
596596

597597
@Expose()
598598
get name() {
599-
return this.firstName + " " + this.lastName;
599+
return this._firstName + " " + this._lastName;
600600
}
601601

602602
}
603603

604604
const user = new User();
605605
user.id = 1;
606606
user.setName("Johny", "Cage");
607-
user._password = 123;
607+
user._password = "123";
608608

609609
const plainUser = classToPlain(user, { excludePrefixes: ["_"] });
610610
// here plainUser will be equal to
@@ -616,9 +616,8 @@ const plainUser = classToPlain(user, { excludePrefixes: ["_"] });
616616
You can use groups to control what data will be exposed and what will not be:
617617

618618
```typescript
619-
import {Exclude, Expose} from "class-transformer";
619+
import {Exclude, Expose, classToPlain} from "class-transformer";
620620

621-
@Exclude()
622621
export class User {
623622

624623
id: number;
@@ -631,10 +630,7 @@ export class User {
631630
@Expose({ groups: ["user"] }) // this means that this data will be exposed only to users
632631
password: string;
633632
}
634-
```
635633

636-
```typescript
637-
import {classToPlain} from "class-transformer";
638634
let user1 = classToPlain(user, { groups: ["user"] }); // will contain id, name, email and password
639635
let user2 = classToPlain(user, { groups: ["admin"] }); // will contain id, name and email
640636
```
@@ -645,9 +641,8 @@ If you are building an API that has different versions, class-transformer has ex
645641
You can control which properties of your model should be exposed or excluded in what version. Example:
646642

647643
```typescript
648-
import {Exclude, Expose} from "class-transformer";
644+
import {Exclude, Expose, classToPlain} from "class-transformer";
649645

650-
@Exclude()
651646
export class User {
652647

653648
id: number;
@@ -660,10 +655,7 @@ export class User {
660655
@Expose({ since: 2.1 }) // this means that this property will be exposed for version starting from 2.1
661656
password: string;
662657
}
663-
```
664658

665-
```typescript
666-
import {classToPlain} from "class-transformer";
667659
let user1 = classToPlain(user, { version: 0.5 }); // will contain id and name
668660
let user2 = classToPlain(user, { version: 0.7 }); // will contain id, name and email
669661
let user3 = classToPlain(user, { version: 1 }); // will contain id and name

0 commit comments

Comments
 (0)