Parses, simplifies, and optionally evaluates a boolean expression. When called with values, it evaluates the expression with the provided variable values, and otherwise, it simplifies the expression to its minimal form. Supports variables (a-z, A-Z, 0-9, _), operators (!, &, |), and parentheses.

// Simplify expression
const p = Chalkboard.bool.parse("x & !x | y & x | y & !x"); // Returns "y"
const q = Chalkboard.bool.parse("(x & y) | (x & z)"); // Returns "x & (y | z)"

// Evaluate expression with values
const r = Chalkboard.bool.parse("x & y | z", { x: true, y: false, z: true }); // Returns true
const s = Chalkboard.bool.parse("a & !b", { a: 1, b: 0 }); // Returns true

// Get AST representation
const t = Chalkboard.bool.parse("x & y", {}, true); // Returns AST object
  • Parameters

    • input: string

      The boolean expression to parse

    • Optionalvalues: Record<string, boolean | 0 | 1>

      Optional object mapping variable names to values

    • OptionalreturnAST: boolean = false

      If true, returns an abstract syntax tree (AST) instead of a string

    Returns string | boolean | 0 | 1 | { type: string; [key: string]: any }