Skip to content

Expressions can be parsed and executed in separately with the parse and execute functions. This is more efficient that calling evaluate multiple times with the same expression because the parse step only needs to be done once per expression.

Example

tsx
import { ESCalcLite } from "escalc-lite"

const logicalExpression = ESCalcLite.parse('1 + 2')
console.log(logicalExpression)
/*
{
  type: 'binary',
  operator: 'addition',
  left: { type: 'value', value: { type: 'constant', value: 1 } },
  right: { type: 'value', value: { type: 'constant', value: 2 } }
}
*/

const result = ESCalcLite.execute(logicalExpression)
console.log(result)
//3

Options

Parse

ts
export type ESCalcLiteParseOptions = Partial<{
  // Factory that creates literals values such as numbers and booleans
  literalFactory: ESCalcLiteLiteralFactory
}>

Execute

ts
export type ESCalcLiteExecuteOptions = Partial<{
  // Map of parameters that are used when executing the expression
  params: Record<string, ESCalcLiteParameter>
  // Map of parameters that are evaluated on demand
  lazyParams: Record<string, ESCalcLiteLazyParameter>
  // Map of functions that are used when executing the expression
  functions: Record<string, ESCalcLiteExpressionFunction>
  // A set of operators that can be overridden to change how they behave when execution an expression
  valueCalculator: ESCalcLiteValueCalculator
}>