MCPcopy Create free account
hub / github.com/apitable/apitable / evaluate

Function evaluate

packages/core/src/formula_parser/evaluate.ts:177–209  ·  view source on GitHub ↗
(expression: string, context: IFormulaEvaluateContext, shouldThrow?: boolean, forceThrow?: boolean)

Source from the content-addressed store, hash-verified

175 * @returns {any}
176 */
177export 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
212export function expressionTransform(

Callers 5

getFormulaCellValueFunction · 0.90
getCellValueMethod · 0.90
numeric.test.tsFile · 0.90
getVisibleRowsFunction · 0.90
runAutomationUrlFunction · 0.90

Calls 3

visitMethod · 0.95
parseFunction · 0.70
resolverWrapperFunction · 0.70

Tested by

no test coverage detected