Skip to content

Commit

Permalink
make compatible with eslint
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelcoxon committed Feb 12, 2024
1 parent f2137ca commit 2f1aec1
Show file tree
Hide file tree
Showing 15 changed files with 151 additions and 65 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
10 changes: 5 additions & 5 deletions src/Cache/AsyncCacheItem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import isFunction from '../TypeHelpers/isFunction';
import { Promises } from '..';


export default class AsyncCacheItem<T> implements IAsyncCacheItem<T>
export default class AsyncCacheItem<T> implements IAsyncCacheItem
{
readonly #promiseOrValue: Awaitable<T>;
readonly #expiryPolicy: IExpiryPolicyDelegate<T>;
Expand All @@ -22,9 +22,9 @@ export default class AsyncCacheItem<T> implements IAsyncCacheItem<T>
this.#expiryPolicy = expiryPolicy;
}

public getValueAsync(): Promise<T>
{
return new Promise<T>((resolve, reject) =>
public getValueAsync<T1 = T>(): Promise<T1>
{
return new Promise<T1>((resolve, reject) =>
{
(async () =>
{
Expand All @@ -37,7 +37,7 @@ export default class AsyncCacheItem<T> implements IAsyncCacheItem<T>
}
else
{
resolve(value);
resolve(value as T1);
}
}
catch (error)
Expand Down
12 changes: 6 additions & 6 deletions src/Cache/MemoryCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ import { isUndefinedOrNull } from '../TypeHelpers';
*/
export default class MemoryCache<TKey = string> implements ICache<TKey>
{
readonly #internalCache: Map<TKey, IAsyncCacheItem<unknown>>;
readonly #internalCache: Map<TKey, IAsyncCacheItem>;

constructor()
{
this.#internalCache = new Map<TKey, IAsyncCacheItem<unknown>>();
this.#internalCache = new Map<TKey, IAsyncCacheItem>();
}

add<T>(key: TKey, value: Awaitable<T>, expiryPolicy: IExpiryPolicyDelegate<T>): void
Expand All @@ -29,8 +29,8 @@ export default class MemoryCache<TKey = string> implements ICache<TKey>

async addOrGetAsync<T>(key: TKey, factory: (key: TKey) => Awaitable<T>, expiryPolicy: IExpiryPolicyDelegate<T>): Promise<T>
{
const cacheItem = this.#internalCache.get(key) as IAsyncCacheItem<T> | undefined;
const value = await cacheItem?.getValueAsync();
const cacheItem = this.#internalCache.get(key) as IAsyncCacheItem | undefined;
const value = await cacheItem?.getValueAsync<T>();

if (!isUndefinedOrNull(value))
{
Expand Down Expand Up @@ -72,7 +72,7 @@ export default class MemoryCache<TKey = string> implements ICache<TKey>
throw new KeyNotFoundException(key);
}

const value = await cacheItem.getValueAsync();
const value = await cacheItem.getValueAsync<T>();
if (isUndefinedOrNull(value))
{
this.#internalCache.delete(key);
Expand All @@ -91,7 +91,7 @@ export default class MemoryCache<TKey = string> implements ICache<TKey>
return { success: false };
}

const value = await cacheItem.getValueAsync();
const value = await cacheItem.getValueAsync<T>();
if (isUndefinedOrNull(value))
{
this.#internalCache.delete(key);
Expand Down
4 changes: 2 additions & 2 deletions src/Cache/_types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
export type IExpiryPolicyDelegate<T> = (value?: T) => boolean;


export interface IAsyncCacheItem<T>
export interface IAsyncCacheItem
{
getValueAsync(): Promise<T>;
getValueAsync<T>(): Promise<T>;
}

export interface ICache<TKey>
Expand Down
4 changes: 3 additions & 1 deletion src/Configuration/JsonConfiguration.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});
Expand Down
9 changes: 5 additions & 4 deletions src/Expressions/Instruction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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];
71 changes: 58 additions & 13 deletions src/Expressions/InstructionExpressionExecutor.spec.ts
Original file line number Diff line number Diff line change
@@ -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);
Expand All @@ -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);
});
});


Expand Down
5 changes: 3 additions & 2 deletions src/Expressions/InstructionExpressionVisitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { BinaryExpression } from './BinaryExpression';
import { ConstantExpression } from './ConstantExpression';
import { UnaryExpression } from './UnaryExpression';
import ExpressionVisitor from './ExpressionVisitor';
import { Instruction } from './Instruction';



Expand All @@ -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
Expand Down
34 changes: 17 additions & 17 deletions src/Numbers/enum.spec.ts
Original file line number Diff line number Diff line change
@@ -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);


// });
// });
});
});
2 changes: 1 addition & 1 deletion src/Pipeline/Pipeline.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ describe("Pipeline", () =>
{
const subject = new Pipeline(new ConsoleLogger(console));
let outsideValue = "not changed";
subject.start(async (ctx) =>
subject.start<string>(async (ctx) =>
{
outsideValue = ctx.data;
});
Expand Down
42 changes: 37 additions & 5 deletions src/Pipeline/Pipeline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,51 @@ export default class Pipeline implements IPipelineTaskQueue
this.#queue = [];
}

public start(task: IPipelineTask | ((context: IContext) => Promise<void>)): this
public start<T>(task: IPipelineTask | ((context: IContext<T>) => Promise<void>)): this
{
this.#queue.push(isFunction(task) ? { name: task.toString(), executeAsync: task } : task);
if (isFunction(task))
{
const pTask: IPipelineTask = {
name: task.toString(),
async executeAsync<T1 = T>(context: IContext<T1>)
{
await task(context);
}
};

this.#queue.push(pTask);
}
else
{
this.#queue.push(task as IPipelineTask);
}

return this;
}

public then(task: IPipelineTask | ((context: IContext) => Promise<void>)): this
public then<T>(task: IPipelineTask | ((context: IContext<T>) => Promise<void>)): this
{
this.#queue.push(isFunction(task) ? { name: task.toString(), executeAsync: task } : task);
if (isFunction(task))
{
const pTask: IPipelineTask = {
name: task.toString(),
async executeAsync<T1 = T>(context: IContext<T1>)
{
await task(context);
}
};

this.#queue.push(pTask);
}
else
{
this.#queue.push(task as IPipelineTask);
}

return this;
}

public async executeAsync(context: IContext): Promise<void>
public async executeAsync<T>(context: IContext<T>): Promise<void>
{
const queue = [...this.#queue];
const logger = this.#logger.scope('Execute');
Expand Down
6 changes: 3 additions & 3 deletions src/Pipeline/_types.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
export interface IContext<T = unknown>
export interface IContext<T>
{
readonly data: T;
}

export interface IPipelineTask
{
readonly name: string;
executeAsync(context: IContext): Promise<void>;
executeAsync<T>(context: IContext<T>): Promise<void>;
}

export interface IPipelineTaskQueue
{
then(task: IPipelineTask | ((context: IContext) => Promise<void>)): IPipelineTaskQueue;
then(task: IPipelineTask | (<T>(context: IContext<T>) => Promise<void>)): IPipelineTaskQueue;
}
3 changes: 2 additions & 1 deletion src/Types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ export type Undefinable<T> = T | undefined;
export type Awaitable<T> = T | PromiseLike<T>;

/** Helper type for representing constructors */
export type ConstructorFor<T> = { new(...args: unknown[]): T; };
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export type ConstructorFor<T> = { new(...args: any[]): T; };

/** Shortcut for () => T */
export type Action<T> = () => T;
Expand Down
7 changes: 7 additions & 0 deletions tsconfig.build.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"extends":"./tsconfig.json",

"exclude": [
"./src/**/*.spec.*"
],
}
3 changes: 0 additions & 3 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,4 @@
"include": [
"./src/**/*"
],
"exclude": [
"./src/**/*.spec.*"
],
}

0 comments on commit 2f1aec1

Please sign in to comment.