From c89848294f232b4ccd8f423ed64a6e44da35401c Mon Sep 17 00:00:00 2001 From: arodidev <51090527+arodidev@users.noreply.github.com> Date: Tue, 11 Jun 2024 23:53:14 +0300 Subject: [PATCH] MVP --- src/utils/expression-runner.ts | 62 ++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) 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