From c6cdfc75eaf7e94df1a9145b49f1cc9513a85043 Mon Sep 17 00:00:00 2001 From: Madeorsk Date: Sun, 18 Sep 2022 19:27:23 +0200 Subject: [PATCH] Add type Date. --- README.md | 4 ++++ src/Model/Types/DateType.ts | 22 ++++++++++++++++++++++ src/Model/Types/DecimalType.ts | 2 +- src/index.ts | 1 + tests/Model.test.ts | 29 +++++++++++++++++------------ 5 files changed, 45 insertions(+), 13 deletions(-) create mode 100644 src/Model/Types/DateType.ts diff --git a/README.md b/README.md index fc7fcfc..989c5c5 100644 --- a/README.md +++ b/README.md @@ -44,6 +44,9 @@ class Person extends Model @Property(SString) email: string = undefined; + + @Property(SDate) + createdAt: Date = undefined; } ``` @@ -104,6 +107,7 @@ following a certain naming convention: "S{type_name}". - `StringType` => `SString` - `NumericType` => `SNumeric` - `DecimalType` => `SDecimal` +- `DateType` => `SDate` - `ArrayType` => `SArray` - `ModelType` => `SModel` diff --git a/src/Model/Types/DateType.ts b/src/Model/Types/DateType.ts new file mode 100644 index 0000000..0607a31 --- /dev/null +++ b/src/Model/Types/DateType.ts @@ -0,0 +1,22 @@ +import {Type} from "./Type"; + +/** + * Type of dates. + */ +export class DateType extends Type +{ + deserialize(value: string): Date + { + return new Date(value); + } + + serialize(value: Date): string + { + return value.toISOString(); + } +} + +/** + * Type of dates. + */ +export const SDate = new DateType(); diff --git a/src/Model/Types/DecimalType.ts b/src/Model/Types/DecimalType.ts index 7caa2d8..6849999 100644 --- a/src/Model/Types/DecimalType.ts +++ b/src/Model/Types/DecimalType.ts @@ -17,6 +17,6 @@ export class DecimalType extends Type } /** - * Type of decimal numbers; + * Type of decimal numbers. */ export const SDecimal = new DecimalType(); diff --git a/src/index.ts b/src/index.ts index 4bed17c..0d00cac 100644 --- a/src/index.ts +++ b/src/index.ts @@ -4,6 +4,7 @@ export * from "./Model/Model"; export * from "./Model/Types/Type"; export * from "./Model/Types/ArrayType"; +export * from "./Model/Types/DateType"; export * from "./Model/Types/DecimalType"; export * from "./Model/Types/ModelType"; export * from "./Model/Types/NumericType"; diff --git a/tests/Model.test.ts b/tests/Model.test.ts index 2e608d1..0257fcd 100644 --- a/tests/Model.test.ts +++ b/tests/Model.test.ts @@ -1,4 +1,4 @@ -import {SArray, SDecimal, SModel, SNumeric, SString, Identifier, Model, Property} from "../src"; +import {SArray, SDecimal, SModel, SNumeric, SString, SDate, Identifier, Model, Property} from "../src"; /** * Another test model. @@ -14,13 +14,17 @@ class Author extends Model @Property(SString) email: string = undefined; - constructor(name: string = undefined, firstName: string = undefined, email: string = undefined) + @Property(SDate) + createdAt: Date = undefined; + + constructor(name: string = undefined, firstName: string = undefined, email: string = undefined, createdAt: Date = undefined) { super(); this.name = name; this.firstName = firstName; this.email = email; + this.createdAt = createdAt; } } @@ -51,8 +55,8 @@ it("deserialize", () => { id: 1, title: "this is a test", authors: [ - { name: "DOE", firstName: "John", email: "test@test.test" }, - { name: "TEST", firstName: "Another", email: "another@test.test" }, + { name: "DOE", firstName: "John", email: "test@test.test", createdAt: "2022-08-07T08:47:01.000Z", }, + { name: "TEST", firstName: "Another", email: "another@test.test", createdAt: "2022-09-07T18:32:55.000Z", }, ], text: "this is a long test.", evaluation: "25.23", @@ -60,8 +64,8 @@ it("deserialize", () => { id: 1, title: "this is a test", authors: [ - { name: "DOE", firstName: "John", email: "test@test.test" }, - { name: "TEST", firstName: "Another", email: "another@test.test" }, + { name: "DOE", firstName: "John", email: "test@test.test", createdAt: "2022-08-07T08:47:01.000Z", }, + { name: "TEST", firstName: "Another", email: "another@test.test", createdAt: "2022-09-07T18:32:55.000Z", }, ], text: "this is a long test.", evaluation: "25.23", @@ -69,11 +73,12 @@ it("deserialize", () => { }); it("create and check state then serialize", () => { + const now = new Date(); const article = new Article(); article.id = 1; article.title = "this is a test"; article.authors = [ - new Author("DOE", "John", "test@test.test"), + new Author("DOE", "John", "test@test.test", now), ]; article.text = "this is a long test."; article.evaluation = 25.23; @@ -85,7 +90,7 @@ it("create and check state then serialize", () => { id: 1, title: "this is a test", authors: [ - { name: "DOE", firstName: "John", email: "test@test.test" }, + { name: "DOE", firstName: "John", email: "test@test.test", createdAt: now.toISOString() }, ], text: "this is a long test.", evaluation: "25.23", @@ -98,8 +103,8 @@ it("deserialize then save", () => { id: 1, title: "this is a test", authors: [ - { name: "DOE", firstName: "John", email: "test@test.test" }, - { name: "TEST", firstName: "Another", email: "another@test.test" }, + { name: "DOE", firstName: "John", email: "test@test.test", createdAt: new Date(), }, + { name: "TEST", firstName: "Another", email: "another@test.test", createdAt: new Date(), }, ], text: "this is a long test.", evaluation: "25.23", @@ -124,8 +129,8 @@ it("save with modified submodels", () => { id: 1, title: "this is a test", authors: [ - { name: "DOE", firstName: "John", email: "test@test.test" }, - { name: "TEST", firstName: "Another", email: "another@test.test" }, + { name: "DOE", firstName: "John", email: "test@test.test", createdAt: new Date(), }, + { name: "TEST", firstName: "Another", email: "another@test.test", createdAt: new Date(), }, ], text: "this is a long test.", evaluation: "25.23",