diff --git a/package.json b/package.json index 5d7ffb9..39bc35e 100644 --- a/package.json +++ b/package.json @@ -18,8 +18,8 @@ "build:docs:typedoc": "typedoc --logLevel Verbose --out docs/api src", "build:lib": "npm-run-all -s clean:lib build:lib:typescript", "build:lib:debug": "npm-run-all -s clean:lib build:lib:typescript:debug", - "build:lib:typescript": "tsc", - "build:lib:typescript:debug": "tsc --sourceMap", + "build:lib:typescript": "tsc -p ./tsconfig.build.json", + "build:lib:typescript:debug": "tsc --sourceMap -p ./tsconfig.build.json", "build": "npm-run-all -s build:lib -p build:dist", "clean:lib": "rimraf lib", "clean:dist": "rimraf dist", diff --git a/src/Cache/AsyncCacheItem.ts b/src/Cache/AsyncCacheItem.ts index 495c636..d29e5ca 100644 --- a/src/Cache/AsyncCacheItem.ts +++ b/src/Cache/AsyncCacheItem.ts @@ -4,7 +4,7 @@ import isFunction from '../TypeHelpers/isFunction'; import { Promises } from '..'; -export default class AsyncCacheItem implements IAsyncCacheItem +export default class AsyncCacheItem implements IAsyncCacheItem { readonly #promiseOrValue: Awaitable; readonly #expiryPolicy: IExpiryPolicyDelegate; @@ -22,9 +22,9 @@ export default class AsyncCacheItem implements IAsyncCacheItem this.#expiryPolicy = expiryPolicy; } - public getValueAsync(): Promise - { - return new Promise((resolve, reject) => + public getValueAsync(): Promise + { + return new Promise((resolve, reject) => { (async () => { @@ -37,7 +37,7 @@ export default class AsyncCacheItem implements IAsyncCacheItem } else { - resolve(value); + resolve(value as T1); } } catch (error) diff --git a/src/Cache/MemoryCache.ts b/src/Cache/MemoryCache.ts index 35e1ae0..db62036 100644 --- a/src/Cache/MemoryCache.ts +++ b/src/Cache/MemoryCache.ts @@ -10,11 +10,11 @@ import { isUndefinedOrNull } from '../TypeHelpers'; */ export default class MemoryCache implements ICache { - readonly #internalCache: Map>; + readonly #internalCache: Map; constructor() { - this.#internalCache = new Map>(); + this.#internalCache = new Map(); } add(key: TKey, value: Awaitable, expiryPolicy: IExpiryPolicyDelegate): void @@ -29,8 +29,8 @@ export default class MemoryCache implements ICache async addOrGetAsync(key: TKey, factory: (key: TKey) => Awaitable, expiryPolicy: IExpiryPolicyDelegate): Promise { - const cacheItem = this.#internalCache.get(key) as IAsyncCacheItem | undefined; - const value = await cacheItem?.getValueAsync(); + const cacheItem = this.#internalCache.get(key) as IAsyncCacheItem | undefined; + const value = await cacheItem?.getValueAsync(); if (!isUndefinedOrNull(value)) { @@ -72,7 +72,7 @@ export default class MemoryCache implements ICache throw new KeyNotFoundException(key); } - const value = await cacheItem.getValueAsync(); + const value = await cacheItem.getValueAsync(); if (isUndefinedOrNull(value)) { this.#internalCache.delete(key); @@ -91,7 +91,7 @@ export default class MemoryCache implements ICache return { success: false }; } - const value = await cacheItem.getValueAsync(); + const value = await cacheItem.getValueAsync(); if (isUndefinedOrNull(value)) { this.#internalCache.delete(key); diff --git a/src/Cache/_types.ts b/src/Cache/_types.ts index deab7ac..f7b5c5b 100644 --- a/src/Cache/_types.ts +++ b/src/Cache/_types.ts @@ -7,9 +7,9 @@ export type IExpiryPolicyDelegate = (value?: T) => boolean; -export interface IAsyncCacheItem +export interface IAsyncCacheItem { - getValueAsync(): Promise; + getValueAsync(): Promise; } export interface ICache diff --git a/src/Configuration/JsonConfiguration.spec.ts b/src/Configuration/JsonConfiguration.spec.ts index e621a70..9f82011 100644 --- a/src/Configuration/JsonConfiguration.spec.ts +++ b/src/Configuration/JsonConfiguration.spec.ts @@ -118,7 +118,9 @@ describe("JsonConfigurationBuilder.build", () => age: 35, }]); - expect((actual as unknown[]).find(i => i.name === 'mike')).toEqual({ + + // eslint-disable-next-line @typescript-eslint/no-explicit-any + expect((actual as any[]).find(i => i.name === 'mike')).toEqual({ name: 'mike', age: 35, }); diff --git a/src/Expressions/Instruction.ts b/src/Expressions/Instruction.ts index 99ce0e5..689124b 100644 --- a/src/Expressions/Instruction.ts +++ b/src/Expressions/Instruction.ts @@ -14,7 +14,8 @@ const instruction = */ export type ValueType = number | string | boolean; -export type Instruction = [ExpressionType, ...((Instruction | ValueType)[])]; -export type AddInstruction = [ExpressionType.Add, ...(Instruction[])]; -export type AndAlsoInstruction = [ExpressionType.AndAlso, ...(Instruction[])]; -export type ConstantInstruction = [ExpressionType.Constant, ValueType]; +export type InstructionParameterType = Instruction | ValueType; +export type Instruction = [ExpressionType, ...InstructionParameterType[]]; +export type AddInstruction = [ExpressionType.Add, ...InstructionParameterType[]]; +export type AndAlsoInstruction = [ExpressionType.AndAlso, ...InstructionParameterType[]]; +export type ConstantInstruction = [ExpressionType.Constant, InstructionParameterType]; diff --git a/src/Expressions/InstructionExpressionExecutor.spec.ts b/src/Expressions/InstructionExpressionExecutor.spec.ts index f95c651..46e72f4 100644 --- a/src/Expressions/InstructionExpressionExecutor.spec.ts +++ b/src/Expressions/InstructionExpressionExecutor.spec.ts @@ -1,28 +1,33 @@ import { ExpressionType } from './ExpressionType'; import InstructionExpressionExecutor from './InstructionExpressionExecutor'; -import { Instruction } from './Instruction'; - - +import { Instruction } from './Instruction'; +import createConstant from './utils/createConstant'; +import { ConstantExpressionType } from './ConstantExpression'; +import { Expression } from './Expression'; +import InstructionExpressionVisitor from './InstructionExpressionVisitor'; describe("InstructionExpressionExecutor", () => { it("should execute the Instructions", () => { + // 30 + 10 + 11 + 21 + 41 const program: Instruction = [ ExpressionType.Add, [ - ExpressionType.Add, [ - ExpressionType.Constant, 30], [ - ExpressionType.Add, [ - ExpressionType.Add, - [ExpressionType.Constant, 10], - [ExpressionType.Constant, 11] - ], - [ExpressionType.Constant, 21] + ExpressionType.Add, + [ExpressionType.Constant, 30], + [ExpressionType.Add, [ + ExpressionType.Add, + [ExpressionType.Constant, 10], + [ExpressionType.Constant, 11] + ], + [ExpressionType.Constant, 21] ] ], [ExpressionType.Constant, 41] ]; - const expected = 113; + console.log(program); + + const expected = 113;// 30 + 10 + 11 + 21 + 41 const actual = InstructionExpressionExecutor(program); expect(actual).toEqual(expected); @@ -31,10 +36,50 @@ describe("InstructionExpressionExecutor", () => it("should execute the Instructions as values", () => { const program: Instruction = [1, [1, [5, 30], [1, [1, [5, 10], [5, 11]], [5, 21]]], [5, 41]]; - const expected = 113; + const expected = 113;// 30 + 10 + 11 + 21 + 41 const actual = InstructionExpressionExecutor(program); expect(actual).toEqual(expected); }); + + it("should execute the Instructions from Expressions", () => + { + // 30 + 10 + 11 + 21 + 41 + + const c30 = createConstant(30, ConstantExpressionType.Number); + const c10 = createConstant(10, ConstantExpressionType.Number); + const c11 = createConstant(11, ConstantExpressionType.Number); + const c21 = createConstant(21, ConstantExpressionType.Number); + const c41 = createConstant(41, ConstantExpressionType.Number); + + function toInstruction(expr: Expression): Instruction + { + const visitor = new InstructionExpressionVisitor(); + visitor.visit(expr); + return visitor.value; + } + + const program: Instruction = [ + ExpressionType.Add, [ + ExpressionType.Add, + toInstruction(c30), + [ExpressionType.Add, [ + ExpressionType.Add, + toInstruction(c10), + toInstruction(c11) + ], + toInstruction(c21) + ] + ], + toInstruction(c41) + ]; + + console.log(program); + + const expected = 113;// 30 + 10 + 11 + 21 + 41 + const actual = InstructionExpressionExecutor(program); + + expect(actual).toEqual(expected); + }); }); diff --git a/src/Expressions/InstructionExpressionVisitor.ts b/src/Expressions/InstructionExpressionVisitor.ts index 3b87899..d1ec8b6 100644 --- a/src/Expressions/InstructionExpressionVisitor.ts +++ b/src/Expressions/InstructionExpressionVisitor.ts @@ -2,6 +2,7 @@ import { BinaryExpression } from './BinaryExpression'; import { ConstantExpression } from './ConstantExpression'; import { UnaryExpression } from './UnaryExpression'; import ExpressionVisitor from './ExpressionVisitor'; +import { Instruction } from './Instruction'; @@ -11,9 +12,9 @@ export default class InstructionExpressionVisitor extends ExpressionVisitor { #stack: unknown[] = []; - get value(): unknown[] + get value(): Instruction { - return this.#stack; + return this.#stack as Instruction; } public visitAddExpression(node: BinaryExpression): void diff --git a/src/Numbers/enum.spec.ts b/src/Numbers/enum.spec.ts index 42fa623..caa0e79 100644 --- a/src/Numbers/enum.spec.ts +++ b/src/Numbers/enum.spec.ts @@ -1,25 +1,25 @@ -//import Int32 from './Integers/Int32'; +import Int32 from './Integers/Int32'; -// class Enum extends Int32 -// { +class Enum extends Int32 +{ -// } +} -// class Status extends Enum -// { -// public static readonly Active : Status = new Int32(0); -// public static readonly Inactive: Status = new Int32(1); -// } +class Status extends Enum +{ + public static readonly Active: Status = new Int32(0); + public static readonly Inactive: Status = new Int32(1); +} -// describe("enum", () => -// { -// it("easy", () => -// { -// const value = Status.Active; +describe("enum", () => +{ + it("easy", () => + { + const value = Status.Active; + console.trace(value); - -// }); -// }); \ No newline at end of file + }); +}); diff --git a/src/Pipeline/Pipeline.spec.ts b/src/Pipeline/Pipeline.spec.ts index f889aa1..16af756 100644 --- a/src/Pipeline/Pipeline.spec.ts +++ b/src/Pipeline/Pipeline.spec.ts @@ -31,7 +31,7 @@ describe("Pipeline", () => { const subject = new Pipeline(new ConsoleLogger(console)); let outsideValue = "not changed"; - subject.start(async (ctx) => + subject.start(async (ctx) => { outsideValue = ctx.data; }); diff --git a/src/Pipeline/Pipeline.ts b/src/Pipeline/Pipeline.ts index 9600427..eaf213a 100644 --- a/src/Pipeline/Pipeline.ts +++ b/src/Pipeline/Pipeline.ts @@ -14,19 +14,51 @@ export default class Pipeline implements IPipelineTaskQueue this.#queue = []; } - public start(task: IPipelineTask | ((context: IContext) => Promise)): this + public start(task: IPipelineTask | ((context: IContext) => Promise)): this { - this.#queue.push(isFunction(task) ? { name: task.toString(), executeAsync: task } : task); + if (isFunction(task)) + { + const pTask: IPipelineTask = { + name: task.toString(), + async executeAsync(context: IContext) + { + await task(context); + } + }; + + this.#queue.push(pTask); + } + else + { + this.#queue.push(task as IPipelineTask); + } + return this; } - public then(task: IPipelineTask | ((context: IContext) => Promise)): this + public then(task: IPipelineTask | ((context: IContext) => Promise)): this { - this.#queue.push(isFunction(task) ? { name: task.toString(), executeAsync: task } : task); + if (isFunction(task)) + { + const pTask: IPipelineTask = { + name: task.toString(), + async executeAsync(context: IContext) + { + await task(context); + } + }; + + this.#queue.push(pTask); + } + else + { + this.#queue.push(task as IPipelineTask); + } + return this; } - public async executeAsync(context: IContext): Promise + public async executeAsync(context: IContext): Promise { const queue = [...this.#queue]; const logger = this.#logger.scope('Execute'); diff --git a/src/Pipeline/_types.ts b/src/Pipeline/_types.ts index ec4c960..38771f0 100644 --- a/src/Pipeline/_types.ts +++ b/src/Pipeline/_types.ts @@ -1,4 +1,4 @@ -export interface IContext +export interface IContext { readonly data: T; } @@ -6,10 +6,10 @@ export interface IContext export interface IPipelineTask { readonly name: string; - executeAsync(context: IContext): Promise; + executeAsync(context: IContext): Promise; } export interface IPipelineTaskQueue { - then(task: IPipelineTask | ((context: IContext) => Promise)): IPipelineTaskQueue; + then(task: IPipelineTask | ((context: IContext) => Promise)): IPipelineTaskQueue; } diff --git a/src/Types.ts b/src/Types.ts index faccc12..9e59f4e 100644 --- a/src/Types.ts +++ b/src/Types.ts @@ -14,7 +14,8 @@ export type Undefinable = T | undefined; export type Awaitable = T | PromiseLike; /** Helper type for representing constructors */ -export type ConstructorFor = { new(...args: unknown[]): T; }; +// eslint-disable-next-line @typescript-eslint/no-explicit-any +export type ConstructorFor = { new(...args: any[]): T; }; /** Shortcut for () => T */ export type Action = () => T; diff --git a/tsconfig.build.json b/tsconfig.build.json new file mode 100644 index 0000000..483a81e --- /dev/null +++ b/tsconfig.build.json @@ -0,0 +1,7 @@ +{ + "extends":"./tsconfig.json", + + "exclude": [ + "./src/**/*.spec.*" + ], +} \ No newline at end of file diff --git a/tsconfig.json b/tsconfig.json index a650eff..afd5190 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -33,7 +33,4 @@ "include": [ "./src/**/*" ], - "exclude": [ - "./src/**/*.spec.*" - ], } \ No newline at end of file