The node to render.
A parenthesized boolean expression.
renderAst({ type: "and",
left: { type: "term", value: "sodium", phrase: false },
right: { type: "not", operand: { type: "term", value: "borohydride", phrase: false } } });
// => '(sodium AND NOT borohydride)'
export function renderAst(ast: SearchAst): string {
switch (ast.type) {
case 'term':
return ast.phrase ? `"${ast.value}"` : ast.value;
case 'and':
return `(${renderAst(ast.left)} AND ${renderAst(ast.right)})`;
case 'or':
return `(${renderAst(ast.left)} OR ${renderAst(ast.right)})`;
case 'not':
return `NOT ${renderAst(ast.operand)}`;
default:
return '<unknown>';
}
}
Renders a parsed search AST as a readable one-line expression, so a printed probe shows how the query was actually parsed rather than just echoing the raw input.