@_solaris/expression-parser
v0.1.3
Published
Type-safe consumers for ww-expression-editor output — AST evaluation, template rendering, and filter evaluation across the three editor presets.
Downloads
471
Readme
@_solaris/expression-parser
Type-safe consumers for ww-expression-editor output. Pure TypeScript, zero runtime dependencies, works in Node, browser, and Deno.
The editor has three presets, each with a different output shape. This package provides one consumer per preset, plus a standalone parser if you only have the raw expression string.
| Preset | Output | Consumer |
|---|---|---|
| condition | { expression, ast, valid, errors } | evaluate(ast, ctx) → boolean |
| variableInsertion | { expression, ast: null, valid, errors, variables } | render(expression, ctx) → string |
| filterBuilding | { expression, ast: null, filters, valid, errors } | evaluateFilters(filters, item) → boolean |
Install
npm install @_solaris/expression-parserDeno:
import { evaluate } from 'npm:@_solaris/expression-parser';Usage by preset
condition — evaluate a boolean expression against a context
import { evaluate, type ExpressionOutput } from '@_solaris/expression-parser';
const output: ExpressionOutput = /* from the editor */;
const passes = evaluate(output.ast, { user: { age: 21, country: 'FR' } });
// → true / falseFor non-boolean expressions (text, numbers, dates), use evaluateValue instead — it returns the raw value with no boolean assertion.
If you only have the raw expression string (e.g. read from a DB):
import { parse, evaluate } from '@_solaris/expression-parser';
const { ast, errors } = parse("user.age > 18 AND user.country in ['FR','US']");
if (errors.length === 0) evaluate(ast, ctx);variableInsertion — render a template string
import { render, type TemplateOutput } from '@_solaris/expression-parser';
const output: TemplateOutput = /* from the editor */;
const text = render(output.expression, { user: { name: 'Alice', age: 30 } });
// "Bonjour Alice, tu as 30 ans"For HTML/SQL/shell injection contexts, pass a format option to escape substituted values:
render(expression, ctx, { format: v => escapeHtml(String(v)) });filterBuilding — pass through to a filter consumer, or evaluate locally
import { evaluateFilters, type FilterOutput } from '@_solaris/expression-parser';
const output: FilterOutput = /* from the editor */;
// Bind directly into ww-virtualized-list:
// <ww-virtualized-list :filters="output.filters" filter-mode="and" />
// Or evaluate locally:
const items = data.filter(item => evaluateFilters(output.filters, item));The local evaluator mirrors ww-virtualized-list's semantics (case-insensitive contains / startsWith / endsWith, numeric coercion for ordering, etc.). For other runtimes (e.g. PostgREST) you'll likely translate output.filters into the target query language rather than using evaluateFilters.
Sub-path imports (tree-shaking)
import { evaluate } from '@_solaris/expression-parser/expression';
import { parse } from '@_solaris/expression-parser/parser';
import { render } from '@_solaris/expression-parser/template';
import { evaluateFilters } from '@_solaris/expression-parser/filter';Or grab everything namespaced:
import { expression, template, filter, parser } from '@_solaris/expression-parser';
expression.evaluate(...);
template.render(...);
filter.evaluateFilters(...);
parser.parse(...);License
MIT
