diff --git a/src/utils/expression-runner.ts b/src/utils/expression-runner.ts index d64fd0147..6b5ce27c3 100644 --- a/src/utils/expression-runner.ts +++ b/src/utils/expression-runner.ts @@ -32,6 +32,68 @@ export function evaluateExpression( return null; } + const operators = [ + '+', + '*', + '/', + '%', + '==', + '!=', + '===', + '!==', + '>', + '<', + '>=', + '<=', + '&&', + '||', + '&', + '|', + '^', + '~', + '<<', + '>>', + '>>>', + '+=', + '-=', + '*=', + '/=', + '%=', + '?:', + '?', + ':', + '=>', + ]; + + function splitter(delimiter: string, expression: string) { + return expression + .split(delimiter) + .map((item) => item.trim()) + .join(` ${delimiter} `); + } + + const expressionFormatter = (expression: string): string => { + let sanitizedExpression; + + operators.forEach((operator) => { + const expressionParts = expression.split(operator).map((eachItem) => eachItem.trim()); + if (expressionParts.length > 1) { + const [firstPart, lastPart] = expressionParts; + + if (!/[a-zA-Z0-9']/.test(firstPart.charAt(firstPart.length - 1)) || !/[a-zA-Z0-9']/.test(lastPart?.charAt(0))) { + // these have been split by an invalid operator + console.log('split by invalid delimiter'); + } else { + sanitizedExpression = splitter(operator, expression); + } + } + }); + + return sanitizedExpression; + }; + + expressionFormatter(expression); + const allFieldsKeys = fields.map((f) => f.id); const parts = parseExpression(expression.trim()); // register dependencies