* Evaluates an expression based on the given inputs/context.
(expr: string, inputs: object)
| 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. |
no test coverage detected