conditional-love
Version:
Programmatically construct conditional expressions
39 lines (38 loc) • 1.9 kB
TypeScript
import ES6Error from 'es6-error';
export { Resolver, ResolverResolver, CompositeResolver, DepthFirstResolver, } from './resolver';
export interface Conditional<INPUT extends any[]> {
(...args: INPUT): boolean;
}
export interface Handler<INPUT extends any[], OUTPUT> {
(...args: INPUT): OUTPUT;
}
export interface Rule<INPUT extends any[], OUTPUT> {
(...args: INPUT): (Handler<INPUT, OUTPUT> | undefined | void);
}
export interface DispatcherInstance<INPUT extends any[], OUTPUT> {
(...args: INPUT): OUTPUT;
rules: Rule<INPUT, OUTPUT>[];
onMatchFailure: Handler<INPUT, OUTPUT>;
use(rule: Rule<INPUT, OUTPUT>): DispatcherInstance<INPUT, OUTPUT>;
otherwise(handler: Handler<INPUT, OUTPUT>): DispatcherInstance<INPUT, OUTPUT>;
}
export declare class UnhandledArgumentsError extends ES6Error {
}
/**
* A factory for creating "Command Dispatchers". The general
* concept is outlined here:
*
* https://olvlvl.com/2018-04-command-dispatcher-pattern
*
* Think of it as a mechanism for programmatically constructing
* conditional logic. Thus, you can easily plug-in new behaviors
* without actually needing to change the lower-level code.
*/
export declare function Dispatcher<INPUT extends any[], OUTPUT>(): DispatcherInstance<INPUT, OUTPUT>;
export declare function IF<INPUT extends any[], OUTPUT>(condition: Conditional<INPUT>, handler: Handler<INPUT, OUTPUT>): Rule<INPUT, OUTPUT>;
export declare function RETURN<OUTPUT>(value: OUTPUT): () => OUTPUT;
export declare function DO_NOTHING(): void;
export declare function THROW_UNHANDLED_ARGUMENTS_ERROR(): never;
export declare function AND<ARGS extends any[]>(expressions: Conditional<ARGS>[]): Conditional<ARGS>;
export declare function OR<ARGS extends any[]>(expressions: Conditional<ARGS>[]): Conditional<ARGS>;
export declare function NOT<ARGS extends any[]>(expr: Conditional<ARGS>): Conditional<ARGS>;