UNPKG

@adguard/agtree

Version:
46 lines (45 loc) 1.43 kB
/** * @file Utility functions for logical expression node. */ import { type AnyExpressionNode, type ExpressionVariableNode } from '../nodes/index.js'; /** * Variable table. Key is variable name, value is boolean. */ export type VariableTable = { [key: string]: boolean; }; /** * Utility functions for logical expression node. */ export declare class LogicalExpressionUtils { /** * Get all variables in the expression. * * @param node Logical expression node * @returns List of variables in the expression (nodes) * @example * If the expression is `a && b || c`, the returned list will be * nodes for `a`, `b`, and `c`. */ static getVariables(node: AnyExpressionNode): ExpressionVariableNode[]; /** * Evaluate the parsed logical expression. You'll need to provide a * variable table. * * @param node Logical expression node * @param table Variable table (key: variable name, value: boolean) * @returns Evaluation result * @example * If the expression is `a && b`, and the variable table is * `{ a: true, b: false }`, the result will be `false`. * * Example code: * ```js * LogicalExpressionUtils.evaluate( * LogicalExpressionParser.parse('a && b'), * { a: true, b: false } * ); * ``` */ static evaluate(node: AnyExpressionNode, table: VariableTable): boolean; }