MCPcopy
hub / github.com/GitbookIO/gitbook / evaluate

Method evaluate

packages/expr/src/runtime.ts:43–61  ·  view source on GitHub ↗

* Evaluates an expression based on the given inputs/context.

(expr: string, inputs: object)

Source from the content-addressed store, hash-verified

41 * Evaluates an expression based on the given inputs/context.
42 */
43 public evaluate(expr: string, inputs: object): unknown {
44 try {
45 const parsed = this.parse(expr);
46
47 if (parsed.invalidNodes.length > 0) {
48 throw new ExpressionError('Invalid nodes found when parsing');
49 }
50
51 return evaluate.sync<Expression>(parsed.result, inputs, {
52 functions: true,
53 withMembers: true,
54 generate: escodegen.generate,
55 });
56 } catch (error) {
57 throw error instanceof Error
58 ? new ExpressionError(error.message)
59 : new ExpressionError('Unexpected error');
60 }
61 }
62
63 /**
64 * Evaluates an expression safely by returning the error instead of throwing when invalid.

Callers 6

safeEvaluateMethod · 0.95
evaluateTemplateMethod · 0.95
runtime.test.tsFile · 0.80
waitForTOCScrollingFunction · 0.80
internal.spec.tsFile · 0.80

Calls 1

parseMethod · 0.95

Tested by

no test coverage detected