Embedding the engine
import { ExpressionEngine } from "solve-engine";
const engine = new ExpressionEngine("en");The first argument is the locale, which decides decimal and thousands separators and the ambiguous date order.
Configuration
Section titled “Configuration”The second and third arguments enable diagnostics and override configuration. Both are optional.
const engine = new ExpressionEngine("en", false, { validation: { maxExpressionLength: 1000, maxComplexity: 500, },});Safety limits exist because the engine is designed to run on untrusted input as
someone types. They bound expression length, parse complexity, instruction
count, stack depth, and how many elements a range or matrix may be expanded to
by map/reduce (vm.maxCollectionSize, 100000 by default, which is what
stops a typo like sum(x, 1:100000000) from allocating until the host runs out
of memory). Each produces a clear error rather than hanging.
Reading a result
Section titled “Reading a result”import { ValueType } from "solve-engine/vm";
const [value] = engine.evaluateExpression("2 + 2");
value.type; // ValueType.Numbervalue.toNumber(); // 4value.unit; // undefinedClearing state
Section titled “Clearing state”An engine accumulates variables and cached results. Call clear() to reset it
between documents rather than constructing a new one, which is cheaper.
engine.clear();