Skip to content

Commit b67e0fa

Browse files
committed
fix(TransformationType): move TransformationType to separate file, remove duplicate definition and use strings for the enum type. Fixes typestack#147 See also: angular/angular-cli#7613 (comment)
1 parent c3d0030 commit b67e0fa

8 files changed

+26
-30
lines changed

src/ClassTransformer.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import {ClassTransformOptions} from "./ClassTransformOptions";
2-
import {TransformOperationExecutor, TransformationType} from "./TransformOperationExecutor";
2+
import {TransformOperationExecutor} from "./TransformOperationExecutor";
3+
import {TransformationType} from "./TransformationType";
34

45
export type ClassType<T> = {
56
new (...args: any[]): T;

src/TransformOperationExecutor.ts

+1-6
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,7 @@
11
import {ClassTransformOptions} from "./ClassTransformOptions";
22
import {defaultMetadataStorage} from "./storage";
33
import {TypeOptions} from "./metadata/ExposeExcludeOptions";
4-
5-
export enum TransformationType {
6-
PLAIN_TO_CLASS,
7-
CLASS_TO_PLAIN,
8-
CLASS_TO_CLASS
9-
}
4+
import {TransformationType} from "./TransformationType";
105

116
export class TransformOperationExecutor {
127

src/TransformationType.ts

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/**
2+
* Enum representing the different transformation types.
3+
*/
4+
export enum TransformationType {
5+
PLAIN_TO_CLASS = "0",
6+
CLASS_TO_PLAIN = "1",
7+
CLASS_TO_CLASS = "2"
8+
}

src/decorators.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {ExposeOptions, ExcludeOptions, TypeOptions, TransformOptions} from "./me
66
import {ExcludeMetadata} from "./metadata/ExcludeMetadata";
77
import {TransformMetadata} from "./metadata/TransformMetadata";
88
import {ClassTransformOptions} from "./ClassTransformOptions";
9-
import {TransformationType} from "./TransformOperationExecutor";
9+
import {TransformationType} from "./TransformationType";
1010

1111
/**
1212
* Defines a custom logic for value transformation.

src/index.ts

+2-10
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import {ClassTransformOptions} from "./ClassTransformOptions";
33

44
export {ClassTransformer} from "./ClassTransformer";
55
export {ClassTransformOptions} from "./ClassTransformOptions";
6+
import {TransformationType} from "./TransformationType";
7+
68
export * from "./metadata/ExposeExcludeOptions";
79
export * from "./decorators";
810

@@ -90,13 +92,3 @@ export function deserialize<T>(cls: ClassType<T>, json: string, options?: ClassT
9092
export function deserializeArray<T>(cls: ClassType<T>, json: string, options?: ClassTransformOptions): T[] {
9193
return classTransformer.deserializeArray(cls, json, options);
9294
}
93-
94-
/**
95-
* Enum representing the different transformation types.
96-
*/
97-
98-
export enum TransformationType {
99-
PLAIN_TO_CLASS,
100-
CLASS_TO_PLAIN,
101-
CLASS_TO_CLASS
102-
}

src/metadata/MetadataStorage.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import {TypeMetadata} from "./TypeMetadata";
22
import {ExposeMetadata} from "./ExposeMetadata";
33
import {ExcludeMetadata} from "./ExcludeMetadata";
4-
import {TransformationType} from "../TransformOperationExecutor";
4+
import {TransformationType} from "../TransformationType";
55
import {TransformMetadata} from "./TransformMetadata";
66

77
/**

src/metadata/TransformMetadata.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {TransformOptions} from "./ExposeExcludeOptions";
2-
import {TransformationType} from "../TransformOperationExecutor";
2+
import {TransformationType} from "../TransformationType";
33

44
export class TransformMetadata {
55

test/functional/custom-transform.spec.ts

+10-10
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ import {expect} from "chai";
33
import {classToPlain, plainToClass} from "../../src/index";
44
import {defaultMetadataStorage} from "../../src/storage";
55
import {Expose, Transform, Type} from "../../src/decorators";
6+
import {TransformationType} from "../../src/TransformationType";
67
import * as moment from "moment";
7-
import {TransformationType} from "../../src/TransformOperationExecutor";
88

99
describe("custom transformation decorator", () => {
1010

@@ -158,7 +158,7 @@ describe("custom transformation decorator", () => {
158158
objArg.should.be.equal(user);
159159
typeArg.should.be.equal(TransformationType.CLASS_TO_PLAIN);
160160
});
161-
161+
162162
let model: any;
163163
it ("should serialize json into model instance of class Person", () => {
164164
expect(() => {
@@ -184,24 +184,24 @@ describe("custom transformation decorator", () => {
184184
}
185185
class Address {
186186
public street: string;
187-
188-
@Expose({ name: "tel" })
187+
188+
@Expose({ name: "tel" })
189189
public telephone: string;
190-
190+
191191
public zip: number;
192-
192+
193193
public country: string;
194194
}
195195
class Person {
196196
public name: string;
197-
197+
198198
@Type(() => Address)
199199
public address: Address;
200-
200+
201201
@Type(() => Hobby)
202202
@Transform(value => value.filter((hobby: any) => hobby.type === "sport"), { toClassOnly: true })
203203
public hobbies: Hobby[];
204-
204+
205205
public age: number;
206206
}
207207
model = plainToClass(Person, json);
@@ -210,7 +210,7 @@ describe("custom transformation decorator", () => {
210210
model.hobbies.forEach((hobby: Hobby) => expect(hobby instanceof Hobby && hobby.type === "sport"));
211211
}).to.not.throw();
212212
});
213-
213+
214214
it ("should serialize a model into json", () => {
215215
expect(() => {
216216
classToPlain(model);

0 commit comments

Comments
 (0)