@arunkumar_h/rule-engine
Version:
A lightweight and extensible rule engine built with TypeScript and Node.js. Define complex business rules and evaluate conditions easily using a simple JSON structure.
69 lines (64 loc) • 3.4 kB
TypeScript
import { LRUCache } from 'lru-cache';
declare namespace N_Engine {
type Operator = "===" | "!==" | ">" | ">=" | "<" | "<=" | "==" | "!=" | "like%" | "%like" | "%like%" | "in" | "!in" | "includes" | "!includes" | string;
type RuleCallback = string | number | boolean | object | string[] | number[] | boolean[] | object[] | ((fact: object, name: string) => RuleCallback);
export type OperatorCallback = (a: any, b: any) => Promise<boolean> | boolean;
export type ConditionOperation = {
path: string;
operator: Operator;
value: any;
};
export type ConditionType = ConditionOperation | Condition | string;
export type ConditionAnd = {
and: ConditionType[];
};
export type ConditionOr = {
or: ConditionType[];
};
export type Condition = ConditionAnd | ConditionOr;
export type Rule = {
condition: Condition | string;
onSuccess: RuleCallback;
onFail: RuleCallback;
cache?: boolean;
};
export type NamedRules = Record<string, Rule>;
export type NamedConditions = Record<string, Condition>;
export type NamedOperators = Record<string, OperatorCallback>;
export { };
}
declare function typeGuardCondition(condition: object): condition is N_Engine.ConditionAnd | N_Engine.ConditionOr;
declare function memoize(cache: LRUCache<string, any>, fn: (...args: any[]) => any, resolver?: (...args: any[]) => string): (...args: any[]) => any;
declare function includes(collection: unknown, value: any): boolean;
declare function endsWith(str: string, target: string, position?: number): boolean;
declare function startsWith(str: string, target: string, position?: number): boolean;
declare function get(fact: object, path: string): any;
declare class Engine {
protected namedRules: Map<string, N_Engine.Rule>;
protected namedConditions: Map<string, N_Engine.Condition>;
protected namedOperators: Map<string, N_Engine.OperatorCallback>;
protected cache: LRUCache<string, boolean> | null;
protected cacheOption: LRUCache.Options<string, boolean, unknown>;
constructor(cacheOptions?: Partial<LRUCache.Options<string, boolean, unknown>>);
get rule(): {
[k: string]: N_Engine.Rule;
};
get condition(): {
[k: string]: N_Engine.Condition;
};
get operator(): {
[k: string]: N_Engine.OperatorCallback;
};
private add;
private addLoop;
protected executeOperation(fact: object, { path, operator, value }: N_Engine.ConditionOperation): Promise<boolean>;
protected executeConditionOperation(fact: object, cond: N_Engine.ConditionType): Promise<boolean | undefined>;
protected evaluateRule(fact: object, condition: N_Engine.Condition | string): Promise<boolean>;
protected memoize(resolver: (...args: any[]) => string): Promise<(...args: any[]) => Promise<boolean | undefined>>;
protected cachedEvaluateRule(ruleName: string, rule: N_Engine.Rule): Promise<((fact: object, condition: N_Engine.Condition | string) => Promise<boolean>) | ((...args: any[]) => Promise<boolean | undefined>)>;
addRule(list: N_Engine.NamedRules): this;
addCondition(list: N_Engine.NamedConditions): this;
addOperator(list: N_Engine.NamedOperators): this;
run(fact: object, ruleName: string): Promise<any>;
}
export { Engine, endsWith, get, includes, memoize, startsWith, typeGuardCondition };