compileSafeExpression

Compiles an expression's JavaScript to a function of the given formals, or returns null where the formals or the body fail the checks or JavaScript refuses the result.

@internote/canvas-sdkFunction
On this page
import { compileSafeExpression } from '@internote/canvas-sdk';

The one place a document's js becomes code; every evaluator goes through here. The body may hold only numbers, the formals, Math.<member>, the members of any namespace given, the arithmetic operators, brackets and commas. For a value read once, evaluateExpression wraps this.

#Signature

<F extends (
    ...args: never[],
) => unknown>(js: unknown, formals: readonly string[], namespaces?: ExpressionNamespaces): F | null

#Parameters

js
The expression's JavaScript, as a refined expression's js.
formals
The names the function declares, in order; the only bare names the body may use.
namespaces
The namespaces reachable as ns.member. Math alone by default.

#Example

const f = compileSafeExpression<(x: number, a: number) => number>(curve.js, ['x', 'a']);
const ys = f ? xs.map((x) => f(x, params.a)) : [];

Declared in packages/canvas-sdk/src/maths/safe-function.ts:144.

Last updated