Skip to content

Commit

Permalink
Add type Date.
Browse files Browse the repository at this point in the history
  • Loading branch information
Madeorsk committed Sep 18, 2022
1 parent 3ce2b74 commit c6cdfc7
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 13 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ class Person extends Model

@Property(SString)
email: string = undefined;

@Property(SDate)
createdAt: Date = undefined;
}
```

Expand Down Expand Up @@ -104,6 +107,7 @@ following a certain naming convention: "S{type_name}".
- `StringType` => `SString`
- `NumericType` => `SNumeric`
- `DecimalType` => `SDecimal`
- `DateType` => `SDate`
- `ArrayType` => `SArray`
- `ModelType` => `SModel`

Expand Down
22 changes: 22 additions & 0 deletions src/Model/Types/DateType.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import {Type} from "./Type";

/**
* Type of dates.
*/
export class DateType extends Type<string, Date>
{
deserialize(value: string): Date
{
return new Date(value);
}

serialize(value: Date): string
{
return value.toISOString();
}
}

/**
* Type of dates.
*/
export const SDate = new DateType();
2 changes: 1 addition & 1 deletion src/Model/Types/DecimalType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ export class DecimalType extends Type<string, number>
}

/**
* Type of decimal numbers;
* Type of decimal numbers.
*/
export const SDecimal = new DecimalType();
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
29 changes: 17 additions & 12 deletions tests/Model.test.ts
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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;
}
}

Expand Down Expand Up @@ -51,29 +55,30 @@ it("deserialize", () => {
id: 1,
title: "this is a test",
authors: [
{ name: "DOE", firstName: "John", email: "[email protected]" },
{ name: "TEST", firstName: "Another", email: "[email protected]" },
{ name: "DOE", firstName: "John", email: "[email protected]", createdAt: "2022-08-07T08:47:01.000Z", },
{ name: "TEST", firstName: "Another", email: "[email protected]", createdAt: "2022-09-07T18:32:55.000Z", },
],
text: "this is a long test.",
evaluation: "25.23",
}).serialize()).toStrictEqual({
id: 1,
title: "this is a test",
authors: [
{ name: "DOE", firstName: "John", email: "[email protected]" },
{ name: "TEST", firstName: "Another", email: "[email protected]" },
{ name: "DOE", firstName: "John", email: "[email protected]", createdAt: "2022-08-07T08:47:01.000Z", },
{ name: "TEST", firstName: "Another", email: "[email protected]", createdAt: "2022-09-07T18:32:55.000Z", },
],
text: "this is a long test.",
evaluation: "25.23",
});
});

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", "[email protected]"),
new Author("DOE", "John", "[email protected]", now),
];
article.text = "this is a long test.";
article.evaluation = 25.23;
Expand All @@ -85,7 +90,7 @@ it("create and check state then serialize", () => {
id: 1,
title: "this is a test",
authors: [
{ name: "DOE", firstName: "John", email: "[email protected]" },
{ name: "DOE", firstName: "John", email: "[email protected]", createdAt: now.toISOString() },
],
text: "this is a long test.",
evaluation: "25.23",
Expand All @@ -98,8 +103,8 @@ it("deserialize then save", () => {
id: 1,
title: "this is a test",
authors: [
{ name: "DOE", firstName: "John", email: "[email protected]" },
{ name: "TEST", firstName: "Another", email: "[email protected]" },
{ name: "DOE", firstName: "John", email: "[email protected]", createdAt: new Date(), },
{ name: "TEST", firstName: "Another", email: "[email protected]", createdAt: new Date(), },
],
text: "this is a long test.",
evaluation: "25.23",
Expand All @@ -124,8 +129,8 @@ it("save with modified submodels", () => {
id: 1,
title: "this is a test",
authors: [
{ name: "DOE", firstName: "John", email: "[email protected]" },
{ name: "TEST", firstName: "Another", email: "[email protected]" },
{ name: "DOE", firstName: "John", email: "[email protected]", createdAt: new Date(), },
{ name: "TEST", firstName: "Another", email: "[email protected]", createdAt: new Date(), },
],
text: "this is a long test.",
evaluation: "25.23",
Expand Down

0 comments on commit c6cdfc7

Please sign in to comment.