(expression: string, context: IFormulaEvaluateContext, shouldThrow?: boolean, forceThrow?: boolean)
| 175 | * @returns {any} |
| 176 | */ |
| 177 | export function evaluate(expression: string, context: IFormulaEvaluateContext, shouldThrow?: boolean, forceThrow?: boolean): any { |
| 178 | if (!expression) { |
| 179 | return null; |
| 180 | } |
| 181 | const state = context.state; |
| 182 | const fieldMap = getFieldMap(state, context.field.property.datasheetId)!; |
| 183 | const fExpr = parse(expression, { field: context.field, fieldMap, state }); |
| 184 | if ('error' in fExpr) { |
| 185 | if (forceThrow) { |
| 186 | throw fExpr.error; |
| 187 | } |
| 188 | return null; |
| 189 | } |
| 190 | |
| 191 | // console.log(util.inspect(fExpr, { showHidden: false, depth: null })); |
| 192 | |
| 193 | try { |
| 194 | const resolverFn = resolverWrapper(context); |
| 195 | const interpreter = new Interpreter(resolverFn, context); |
| 196 | const result = interpreter.visit(fExpr.ast); |
| 197 | |
| 198 | // Error for NaN/Infinite/-Infinite values |
| 199 | if (isNumber(result) && !isFinite(result)) { |
| 200 | throw new FormulaBaseError('NaN'); |
| 201 | } |
| 202 | return result; |
| 203 | } catch (e) { |
| 204 | if (shouldThrow) { |
| 205 | throw e; |
| 206 | } |
| 207 | return e; |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | // Convert an expression with fieldName as variable name to an expression with fieldId as variable name |
| 212 | export function expressionTransform( |
no test coverage detected