From 6f62b2d0532c1b89a8ca260ec7e6de181823f0df Mon Sep 17 00:00:00 2001 From: Madeorsk Date: Sun, 18 Sep 2022 19:47:38 +0200 Subject: [PATCH] Add bool type and README improvements. --- README.md | 6 ++++++ src/Model/Types/BoolType.ts | 22 ++++++++++++++++++++++ src/index.ts | 1 + tests/Model.test.ts | 23 +++++++++++++---------- 4 files changed, 42 insertions(+), 10 deletions(-) create mode 100644 src/Model/Types/BoolType.ts diff --git a/README.md b/README.md index 989c5c5..822eb17 100644 --- a/README.md +++ b/README.md @@ -47,6 +47,9 @@ class Person extends Model @Property(SDate) createdAt: Date = undefined; + + @Property(SBool) + active: boolean = true; } ``` @@ -85,9 +88,11 @@ Types are defined by a class extending `Type`. Sharkitek defines some basic types by default, in these classes: +- `BoolType`: boolean value in the model, boolean value in the serialized object. - `StringType`: string in the model, string in the serialized object. - `NumericType`: number in the model, number in the serialized object. - `DecimalType`: number in the model, formatted string in the serialized object. +- `DateType`: date in the model, ISO formatted date in the serialized object. - `ArrayType`: array in the model, array in the serialized object. - `ModelType`: instance of a specific class in the model, object in the serialized object. @@ -104,6 +109,7 @@ class Example extends Model To ease the use of these classes and reduce read complexity, some constant variables and functions are defined in the library, following a certain naming convention: "S{type_name}". +- `BoolType` => `SBool` - `StringType` => `SString` - `NumericType` => `SNumeric` - `DecimalType` => `SDecimal` diff --git a/src/Model/Types/BoolType.ts b/src/Model/Types/BoolType.ts new file mode 100644 index 0000000..deaa8e4 --- /dev/null +++ b/src/Model/Types/BoolType.ts @@ -0,0 +1,22 @@ +import {Type} from "./Type"; + +/** + * Type of any boolean value. + */ +export class BoolType extends Type +{ + deserialize(value: boolean): boolean + { + return !!value; // ensure bool type. + } + + serialize(value: boolean): boolean + { + return !!value; // ensure bool type. + } +} + +/** + * Type of any boolean value. + */ +export const SBool = new BoolType(); diff --git a/src/index.ts b/src/index.ts index 0d00cac..8e0be62 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/BoolType"; export * from "./Model/Types/DateType"; export * from "./Model/Types/DecimalType"; export * from "./Model/Types/ModelType"; diff --git a/tests/Model.test.ts b/tests/Model.test.ts index 0257fcd..806da3a 100644 --- a/tests/Model.test.ts +++ b/tests/Model.test.ts @@ -1,4 +1,4 @@ -import {SArray, SDecimal, SModel, SNumeric, SString, SDate, Identifier, Model, Property} from "../src"; +import {SArray, SDecimal, SModel, SNumeric, SString, SDate, SBool, Identifier, Model, Property} from "../src"; /** * Another test model. @@ -17,6 +17,9 @@ class Author extends Model @Property(SDate) createdAt: Date = undefined; + @Property(SBool) + active: boolean = true; + constructor(name: string = undefined, firstName: string = undefined, email: string = undefined, createdAt: Date = undefined) { super(); @@ -55,8 +58,8 @@ it("deserialize", () => { id: 1, title: "this is a test", authors: [ - { 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", }, + { name: "DOE", firstName: "John", email: "test@test.test", createdAt: "2022-08-07T08:47:01.000Z", active: true, }, + { name: "TEST", firstName: "Another", email: "another@test.test", createdAt: "2022-09-07T18:32:55.000Z", active: false, }, ], text: "this is a long test.", evaluation: "25.23", @@ -64,8 +67,8 @@ it("deserialize", () => { id: 1, title: "this is a test", authors: [ - { 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", }, + { name: "DOE", firstName: "John", email: "test@test.test", createdAt: "2022-08-07T08:47:01.000Z", active: true, }, + { name: "TEST", firstName: "Another", email: "another@test.test", createdAt: "2022-09-07T18:32:55.000Z", active: false, }, ], text: "this is a long test.", evaluation: "25.23", @@ -90,7 +93,7 @@ it("create and check state then serialize", () => { id: 1, title: "this is a test", authors: [ - { name: "DOE", firstName: "John", email: "test@test.test", createdAt: now.toISOString() }, + { name: "DOE", firstName: "John", email: "test@test.test", createdAt: now.toISOString(), active: true, }, ], text: "this is a long test.", evaluation: "25.23", @@ -103,8 +106,8 @@ it("deserialize then save", () => { id: 1, title: "this is a test", authors: [ - { name: "DOE", firstName: "John", email: "test@test.test", createdAt: new Date(), }, - { name: "TEST", firstName: "Another", email: "another@test.test", createdAt: new Date(), }, + { name: "DOE", firstName: "John", email: "test@test.test", createdAt: new Date(), active: true, }, + { name: "TEST", firstName: "Another", email: "another@test.test", createdAt: new Date(), active: false, }, ], text: "this is a long test.", evaluation: "25.23", @@ -129,8 +132,8 @@ it("save with modified submodels", () => { id: 1, title: "this is a test", authors: [ - { name: "DOE", firstName: "John", email: "test@test.test", createdAt: new Date(), }, - { name: "TEST", firstName: "Another", email: "another@test.test", createdAt: new Date(), }, + { name: "DOE", firstName: "John", email: "test@test.test", createdAt: new Date(), active: true, }, + { name: "TEST", firstName: "Another", email: "another@test.test", createdAt: new Date(), active: false, }, ], text: "this is a long test.", evaluation: "25.23",