@burdaforward/composable-rules
Version:
A library for composable rule logic. Easily combine simple rules into more complex ones in a maintainable way using your custom logic and data.
52 lines (50 loc) • 2.42 kB
TypeScript
type Rule<Facts = any, Result = any, Input = any> = PlainRule<Facts, Result, Input> | InjectedRule<Facts, Result, Input> | TransformedRule<Facts, Result, Input> | FirstRule<Facts, Result, Input> | AllRule<Facts, Result, Input> | ChainRule<Facts, Result, Input> | IfRule<Facts, Result, Input>;
type Matcher<Facts = any, Input = any> = (facts?: Facts, input?: Input) => boolean;
type Mapper<Facts = any> = (facts?: Facts) => Facts;
type Action<Facts, Result, Input> = (facts?: Facts, input?: Input) => Result;
type Transformer = Function;
type PlainRule<Facts = any, Result = any, Input = any> = {
type?: "plain";
matcher: Matcher<Facts, Input>;
action: Action<Facts, Result, Input>;
};
type InjectedRule<Facts = any, Result = any, Input = any> = {
type: "injected";
mapper: Mapper<Facts>;
childRule: Rule<Facts, Result, Input>;
};
type TransformedRule<Facts = any, Result = any, Input = any> = {
type: "transformed";
transformer: Transformer;
rule: Rule<Facts, Result, Input>;
};
type IfRule<Facts = any, Result = any, Input = any> = {
type: "if";
matcher: Matcher;
rule: Rule<Facts, Result, Input>;
};
type AllRule<Facts = any, Result = any, Input = any> = {
type: "all";
rules: Rule<Facts, Result, Input>[];
};
type FirstRule<Facts = any, Result = any, Input = any> = {
type: "first";
rules: Rule<Facts, Result, Input>[];
};
type ChainRule<Facts = any, Result = any, Input = any> = {
type: "chain";
rules: Rule<Facts, Result, Input>[];
};
declare const always: (...args: any[]) => boolean;
declare const not: (matcher: Matcher) => (facts?: any, value?: any) => boolean;
declare const one: (matchers: Matcher[]) => (facts?: any, value?: any) => boolean;
declare const all: (matchers: Matcher[]) => (facts?: any, value?: any) => boolean;
declare const injectFacts: (mapper: Mapper, rule: Rule) => InjectedRule;
declare const transformOutput: (fn: Transformer, rule: Rule) => TransformedRule;
declare const applyIf: (matcher: Matcher, rule: Rule) => IfRule;
declare const applyAll: (rules: Rule[]) => AllRule;
declare const applyFirst: (rules: Rule[]) => FirstRule;
declare const applyChain: (rules: Rule[]) => ChainRule;
declare const detailedRun: (...innerArgs: any[]) => any;
declare const run: (...innerArgs: any[]) => any;
export { all, always, applyAll, applyChain, applyFirst, applyIf, detailedRun, injectFacts, not, one, run, transformOutput };