Skip to content

Commit 01db222

Browse files
refactor: rename methods with name class* to instance*
BREAKING CHANGE
1 parent 818897b commit 01db222

21 files changed

+276
-264
lines changed

src/ClassTransformer.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ 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>[];
17-
classToPlain<T extends Record<string, any>>(
15+
instanceToPlain<T extends Record<string, any>>(object: T, options?: ClassTransformOptions): Record<string, any>;
16+
instanceToPlain<T extends Record<string, any>>(object: T[], options?: ClassTransformOptions): Record<string, any>[];
17+
instanceToPlain<T extends Record<string, any>>(
1818
object: T | T[],
1919
options?: ClassTransformOptions
2020
): Record<string, any> | Record<string, any>[] {
@@ -55,17 +55,17 @@ export class ClassTransformer {
5555
/**
5656
* Converts plain (literal) object to class (constructor) object. Also works with arrays.
5757
*/
58-
plainToClass<T extends Record<string, any>, V extends Array<any>>(
58+
plainToInstance<T extends Record<string, any>, V extends Array<any>>(
5959
cls: ClassConstructor<T>,
6060
plain: V,
6161
options?: ClassTransformOptions
6262
): T[];
63-
plainToClass<T extends Record<string, any>, V>(
63+
plainToInstance<T extends Record<string, any>, V>(
6464
cls: ClassConstructor<T>,
6565
plain: V,
6666
options?: ClassTransformOptions
6767
): T;
68-
plainToClass<T extends Record<string, any>, V>(
68+
plainToInstance<T extends Record<string, any>, V>(
6969
cls: ClassConstructor<T>,
7070
plain: V | V[],
7171
options?: ClassTransformOptions
@@ -103,9 +103,9 @@ export class ClassTransformer {
103103
/**
104104
* Converts class (constructor) object to new class (constructor) object. Also works with arrays.
105105
*/
106-
classToClass<T>(object: T, options?: ClassTransformOptions): T;
107-
classToClass<T>(object: T[], options?: ClassTransformOptions): T[];
108-
classToClass<T>(object: T | T[], options?: ClassTransformOptions): T | T[] {
106+
instanceToInstance<T>(object: T, options?: ClassTransformOptions): T;
107+
instanceToInstance<T>(object: T[], options?: ClassTransformOptions): T[];
108+
instanceToInstance<T>(object: T | T[], options?: ClassTransformOptions): T | T[] {
109109
const executor = new TransformOperationExecutor(TransformationType.CLASS_TO_CLASS, {
110110
...defaultOptions,
111111
...options,
@@ -134,22 +134,22 @@ export class ClassTransformer {
134134
serialize<T>(object: T, options?: ClassTransformOptions): string;
135135
serialize<T>(object: T[], options?: ClassTransformOptions): string;
136136
serialize<T>(object: T | T[], options?: ClassTransformOptions): string {
137-
return JSON.stringify(this.classToPlain(object, options));
137+
return JSON.stringify(this.instanceToPlain(object, options));
138138
}
139139

140140
/**
141141
* Deserializes given JSON string to a object of the given class.
142142
*/
143143
deserialize<T>(cls: ClassConstructor<T>, json: string, options?: ClassTransformOptions): T {
144144
const jsonObject: T = JSON.parse(json);
145-
return this.plainToClass(cls, jsonObject, options);
145+
return this.plainToInstance(cls, jsonObject, options);
146146
}
147147

148148
/**
149149
* Deserializes given JSON string to an array of objects of the given class.
150150
*/
151151
deserializeArray<T>(cls: ClassConstructor<T>, json: string, options?: ClassTransformOptions): T[] {
152152
const jsonObject: any[] = JSON.parse(json);
153-
return this.plainToClass(cls, jsonObject, options);
153+
return this.plainToInstance(cls, jsonObject, options);
154154
}
155155
}

src/decorators/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
export * from './exclude.decorator';
22
export * from './expose.decorator';
3-
export * from './transform-class-to-class.decorator';
4-
export * from './transform-class-to-plain.decorator';
5-
export * from './transform-plain-to-class.decorator';
3+
export * from './transform-instance-to-instance.decorator';
4+
export * from './transform-instance-to-plain.decorator';
5+
export * from './transform-plain-to-instance.decorator';
66
export * from './transform.decorator';
77
export * from './type.decorator';

src/decorators/transform-class-to-class.decorator.ts renamed to src/decorators/transform-instance-to-instance.decorator.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { ClassTransformOptions } from '../interfaces';
66
*
77
* Can be applied to functions and getters/setters only.
88
*/
9-
export function TransformClassToClass(params?: ClassTransformOptions): MethodDecorator {
9+
export function TransformInstanceToInstance(params?: ClassTransformOptions): MethodDecorator {
1010
return function (target: Record<string, any>, propertyKey: string | Symbol, descriptor: PropertyDescriptor): void {
1111
const classTransformer: ClassTransformer = new ClassTransformer();
1212
const originalMethod = descriptor.value;
@@ -16,8 +16,8 @@ export function TransformClassToClass(params?: ClassTransformOptions): MethodDec
1616
const isPromise =
1717
!!result && (typeof result === 'object' || typeof result === 'function') && typeof result.then === 'function';
1818
return isPromise
19-
? result.then((data: any) => classTransformer.classToClass(data, params))
20-
: classTransformer.classToClass(result, params);
19+
? result.then((data: any) => classTransformer.instanceToInstance(data, params))
20+
: classTransformer.instanceToInstance(result, params);
2121
};
2222
};
2323
}

src/decorators/transform-class-to-plain.decorator.ts renamed to src/decorators/transform-instance-to-plain.decorator.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { ClassTransformOptions } from '../interfaces';
66
*
77
* Can be applied to functions and getters/setters only.
88
*/
9-
export function TransformClassToPlain(params?: ClassTransformOptions): MethodDecorator {
9+
export function TransformInstanceToPlain(params?: ClassTransformOptions): MethodDecorator {
1010
return function (target: Record<string, any>, propertyKey: string | Symbol, descriptor: PropertyDescriptor): void {
1111
const classTransformer: ClassTransformer = new ClassTransformer();
1212
const originalMethod = descriptor.value;
@@ -16,8 +16,8 @@ export function TransformClassToPlain(params?: ClassTransformOptions): MethodDec
1616
const isPromise =
1717
!!result && (typeof result === 'object' || typeof result === 'function') && typeof result.then === 'function';
1818
return isPromise
19-
? result.then((data: any) => classTransformer.classToPlain(data, params))
20-
: classTransformer.classToPlain(result, params);
19+
? result.then((data: any) => classTransformer.instanceToPlain(data, params))
20+
: classTransformer.instanceToPlain(result, params);
2121
};
2222
};
2323
}

src/decorators/transform-plain-to-class.decorator.ts renamed to src/decorators/transform-plain-to-instance.decorator.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { ClassTransformOptions, ClassConstructor } from '../interfaces';
66
*
77
* Can be applied to functions and getters/setters only.
88
*/
9-
export function TransformPlainToClass(
9+
export function TransformPlainToInstance(
1010
classType: ClassConstructor<any>,
1111
params?: ClassTransformOptions
1212
): MethodDecorator {
@@ -19,8 +19,8 @@ export function TransformPlainToClass(
1919
const isPromise =
2020
!!result && (typeof result === 'object' || typeof result === 'function') && typeof result.then === 'function';
2121
return isPromise
22-
? result.then((data: any) => classTransformer.plainToClass(classType, data, params))
23-
: classTransformer.plainToClass(classType, result, params);
22+
? result.then((data: any) => classTransformer.plainToInstance(classType, data, params))
23+
: classTransformer.plainToInstance(classType, result, params);
2424
};
2525
};
2626
}

src/index.ts

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,21 @@ const classTransformer = new ClassTransformer();
1212
/**
1313
* Converts class (constructor) object to plain (literal) object. Also works with arrays.
1414
*/
15-
export function classToPlain<T>(object: T, options?: ClassTransformOptions): Record<string, any>;
16-
export function classToPlain<T>(object: T[], options?: ClassTransformOptions): Record<string, any>[];
17-
export function classToPlain<T>(
15+
export function instanceToPlain<T>(object: T, options?: ClassTransformOptions): Record<string, any>;
16+
export function instanceToPlain<T>(object: T[], options?: ClassTransformOptions): Record<string, any>[];
17+
export function instanceToPlain<T>(
1818
object: T | T[],
1919
options?: ClassTransformOptions
2020
): Record<string, any> | Record<string, any>[] {
21-
return classTransformer.classToPlain(object, options);
21+
return classTransformer.instanceToPlain(object, options);
2222
}
2323

2424
/**
2525
* Converts class (constructor) object to plain (literal) object.
2626
* Uses given plain object as source object (it means fills given plain object with data from class object).
2727
* Also works with arrays.
28+
*
29+
* @deprecated This function is being removed.
2830
*/
2931
export function classToPlainFromExist<T>(
3032
object: T,
@@ -47,10 +49,14 @@ export function classToPlainFromExist<T>(
4749
/**
4850
* Converts plain (literal) object to class (constructor) object. Also works with arrays.
4951
*/
50-
export function plainToClass<T, V>(cls: ClassConstructor<T>, plain: V[], options?: ClassTransformOptions): T[];
51-
export function plainToClass<T, V>(cls: ClassConstructor<T>, plain: V, options?: ClassTransformOptions): T;
52-
export function plainToClass<T, V>(cls: ClassConstructor<T>, plain: V | V[], options?: ClassTransformOptions): T | T[] {
53-
return classTransformer.plainToClass(cls, plain as any, options);
52+
export function plainToInstance<T, V>(cls: ClassConstructor<T>, plain: V[], options?: ClassTransformOptions): T[];
53+
export function plainToInstance<T, V>(cls: ClassConstructor<T>, plain: V, options?: ClassTransformOptions): T;
54+
export function plainToInstance<T, V>(
55+
cls: ClassConstructor<T>,
56+
plain: V | V[],
57+
options?: ClassTransformOptions
58+
): T | T[] {
59+
return classTransformer.plainToInstance(cls, plain as any, options);
5460
}
5561

5662
/**
@@ -69,10 +75,10 @@ export function plainToClassFromExist<T, V>(clsObject: T, plain: V | V[], option
6975
/**
7076
* Converts class (constructor) object to new class (constructor) object. Also works with arrays.
7177
*/
72-
export function classToClass<T>(object: T, options?: ClassTransformOptions): T;
73-
export function classToClass<T>(object: T[], options?: ClassTransformOptions): T[];
74-
export function classToClass<T>(object: T | T[], options?: ClassTransformOptions): T | T[] {
75-
return classTransformer.classToClass(object, options);
78+
export function instanceToInstance<T>(object: T, options?: ClassTransformOptions): T;
79+
export function instanceToInstance<T>(object: T[], options?: ClassTransformOptions): T[];
80+
export function instanceToInstance<T>(object: T | T[], options?: ClassTransformOptions): T | T[] {
81+
return classTransformer.instanceToInstance(object, options);
7682
}
7783

7884
/**
@@ -93,7 +99,7 @@ export function classToClassFromExist<T>(object: T, fromObject: T | T[], options
9399
*
94100
* @deprecated This function is being removed. Please use
95101
* ```
96-
* JSON.stringify(classToPlain(object, options))
102+
* JSON.stringify(instanceToPlain(object, options))
97103
* ```
98104
*/
99105
export function serialize<T>(object: T, options?: ClassTransformOptions): string;
@@ -107,7 +113,7 @@ export function serialize<T>(object: T | T[], options?: ClassTransformOptions):
107113
*
108114
* @deprecated This function is being removed. Please use the following instead:
109115
* ```
110-
* plainToClass(cls, JSON.parse(json), options)
116+
* instanceToClass(cls, JSON.parse(json), options)
111117
* ```
112118
*/
113119
export function deserialize<T>(cls: ClassConstructor<T>, json: string, options?: ClassTransformOptions): T {
@@ -119,7 +125,7 @@ export function deserialize<T>(cls: ClassConstructor<T>, json: string, options?:
119125
*
120126
* @deprecated This function is being removed. Please use the following instead:
121127
* ```
122-
* JSON.parse(json).map(value => plainToClass(cls, value, options))
128+
* JSON.parse(json).map(value => instanceToClass(cls, value, options))
123129
* ```
124130
*
125131
*/

src/interfaces/decorator-options/expose-options.interface.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export interface ExposeOptions {
1212
*
1313
* Example:
1414
* ```ts
15-
* classToPlain(payload, { version: 1.0 });
15+
* instanceToPlain(payload, { version: 1.0 });
1616
* ```
1717
*/
1818
since?: number;
@@ -22,7 +22,7 @@ export interface ExposeOptions {
2222
*
2323
* Example:
2424
* ```ts
25-
* classToPlain(payload, { version: 1.0 });
25+
* instanceToPlain(payload, { version: 1.0 });
2626
* ```
2727
*/
2828
until?: number;
@@ -34,7 +34,7 @@ export interface ExposeOptions {
3434
*
3535
* Example:
3636
* ```ts
37-
* classToPlain(payload, { groups: ['user'] });
37+
* instanceToPlain(payload, { groups: ['user'] });
3838
* ```
3939
*/
4040
groups?: string[];

src/interfaces/decorator-options/transform-options.interface.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export interface TransformOptions {
77
*
88
* Example:
99
* ```ts
10-
* classToPlain(payload, { version: 1.0 });
10+
* instanceToPlain(payload, { version: 1.0 });
1111
* ```
1212
*/
1313
since?: number;
@@ -17,7 +17,7 @@ export interface TransformOptions {
1717
*
1818
* Example:
1919
* ```ts
20-
* classToPlain(payload, { version: 1.0 });
20+
* instanceToPlain(payload, { version: 1.0 });
2121
* ```
2222
*/
2323
until?: number;
@@ -29,7 +29,7 @@ export interface TransformOptions {
2929
*
3030
* Example:
3131
* ```ts
32-
* classToPlain(payload, { groups: ['user'] });
32+
* instanceToPlain(payload, { groups: ['user'] });
3333
* ```
3434
*/
3535
groups?: string[];

0 commit comments

Comments
 (0)