Add bool type and README improvements.

This commit is contained in:
Madeorsk 2022-09-18 19:47:38 +02:00
parent d25585cc89
commit 6f62b2d053
4 changed files with 42 additions and 10 deletions

View file

@ -47,6 +47,9 @@ class Person extends Model
@Property(SDate) @Property(SDate)
createdAt: Date = undefined; 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: 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. - `StringType`: string in the model, string in the serialized object.
- `NumericType`: number in the model, number 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. - `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. - `ArrayType`: array in the model, array in the serialized object.
- `ModelType`: instance of a specific class in the model, object 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, 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}". following a certain naming convention: "S{type_name}".
- `BoolType` => `SBool`
- `StringType` => `SString` - `StringType` => `SString`
- `NumericType` => `SNumeric` - `NumericType` => `SNumeric`
- `DecimalType` => `SDecimal` - `DecimalType` => `SDecimal`

View file

@ -0,0 +1,22 @@
import {Type} from "./Type";
/**
* Type of any boolean value.
*/
export class BoolType extends Type<boolean, boolean>
{
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();

View file

@ -4,6 +4,7 @@ export * from "./Model/Model";
export * from "./Model/Types/Type"; export * from "./Model/Types/Type";
export * from "./Model/Types/ArrayType"; export * from "./Model/Types/ArrayType";
export * from "./Model/Types/BoolType";
export * from "./Model/Types/DateType"; export * from "./Model/Types/DateType";
export * from "./Model/Types/DecimalType"; export * from "./Model/Types/DecimalType";
export * from "./Model/Types/ModelType"; export * from "./Model/Types/ModelType";

View file

@ -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. * Another test model.
@ -17,6 +17,9 @@ class Author extends Model
@Property(SDate) @Property(SDate)
createdAt: Date = undefined; createdAt: Date = undefined;
@Property(SBool)
active: boolean = true;
constructor(name: string = undefined, firstName: string = undefined, email: string = undefined, createdAt: Date = undefined) constructor(name: string = undefined, firstName: string = undefined, email: string = undefined, createdAt: Date = undefined)
{ {
super(); super();
@ -55,8 +58,8 @@ it("deserialize", () => {
id: 1, id: 1,
title: "this is a test", title: "this is a test",
authors: [ authors: [
{ name: "DOE", firstName: "John", email: "test@test.test", createdAt: "2022-08-07T08:47:01.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", }, { name: "TEST", firstName: "Another", email: "another@test.test", createdAt: "2022-09-07T18:32:55.000Z", active: false, },
], ],
text: "this is a long test.", text: "this is a long test.",
evaluation: "25.23", evaluation: "25.23",
@ -64,8 +67,8 @@ it("deserialize", () => {
id: 1, id: 1,
title: "this is a test", title: "this is a test",
authors: [ authors: [
{ name: "DOE", firstName: "John", email: "test@test.test", createdAt: "2022-08-07T08:47:01.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", }, { name: "TEST", firstName: "Another", email: "another@test.test", createdAt: "2022-09-07T18:32:55.000Z", active: false, },
], ],
text: "this is a long test.", text: "this is a long test.",
evaluation: "25.23", evaluation: "25.23",
@ -90,7 +93,7 @@ it("create and check state then serialize", () => {
id: 1, id: 1,
title: "this is a test", title: "this is a test",
authors: [ 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.", text: "this is a long test.",
evaluation: "25.23", evaluation: "25.23",
@ -103,8 +106,8 @@ it("deserialize then save", () => {
id: 1, id: 1,
title: "this is a test", title: "this is a test",
authors: [ authors: [
{ name: "DOE", firstName: "John", email: "test@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(), }, { name: "TEST", firstName: "Another", email: "another@test.test", createdAt: new Date(), active: false, },
], ],
text: "this is a long test.", text: "this is a long test.",
evaluation: "25.23", evaluation: "25.23",
@ -129,8 +132,8 @@ it("save with modified submodels", () => {
id: 1, id: 1,
title: "this is a test", title: "this is a test",
authors: [ authors: [
{ name: "DOE", firstName: "John", email: "test@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(), }, { name: "TEST", firstName: "Another", email: "another@test.test", createdAt: new Date(), active: false, },
], ],
text: "this is a long test.", text: "this is a long test.",
evaluation: "25.23", evaluation: "25.23",