UNPKG

@player-ui/player

Version:

52 lines 2.68 kB
import { SyncWaterfallHook, SyncBailHook } from "tapable-ts"; import type { ExpressionNode, BinaryOperator, UnaryOperator, ExpressionType, ExpressionContext, ExpressionHandler } from "./types"; export interface HookOptions extends ExpressionContext { /** Given an expression node */ resolveNode: (node: ExpressionNode) => any; /** Enabling this flag skips calling the onError hook, and just throws errors back to the caller. * The caller is responsible for handling the error. */ throwErrors?: boolean; /** Whether expressions should be parsed strictly or not */ strict?: boolean; } export type ExpressionEvaluatorOptions = Omit<HookOptions, "resolveNode" | "evaluate">; export type ExpressionEvaluatorFunction = (exp: ExpressionType, options?: ExpressionEvaluatorOptions) => any; /** * The expression evaluator is responsible for parsing and executing anything in the custom expression language * */ export declare class ExpressionEvaluator { private readonly vars; readonly hooks: { /** Resolve an AST node for an expression to a value */ resolve: SyncWaterfallHook<[any, ExpressionNode, HookOptions], Record<string, any>>; /** Gets the options that will be passed in calls to the resolve hook */ resolveOptions: SyncWaterfallHook<[HookOptions], Record<string, any>>; /** Allows users to change the expression to be evaluated before processing */ beforeEvaluate: SyncWaterfallHook<[ExpressionType, HookOptions], Record<string, any>>; /** * An optional means of handling an error in the expression execution * Return true if handled, to stop propagation of the error */ onError: SyncBailHook<[Error], true, Record<string, any>>; }; private readonly expressionsCache; private readonly defaultHookOptions; readonly operators: { binary: Map<string, BinaryOperator>; unary: Map<string, UnaryOperator>; expressions: Map<string, ExpressionHandler<any, any>>; }; reset(): void; constructor(defaultOptions: ExpressionEvaluatorOptions); evaluate(expr: ExpressionType, options?: ExpressionEvaluatorOptions): any; addExpressionFunction<T extends readonly unknown[], R>(name: string, handler: ExpressionHandler<T, R>): void; addBinaryOperator(operator: string, handler: BinaryOperator): void; addUnaryOperator(operator: string, handler: UnaryOperator): void; setExpressionVariable(name: string, value: unknown): void; getExpressionVariable(name: string): unknown; private _execAST; private _execString; private _resolveNode; } //# sourceMappingURL=evaluator.d.ts.map