Skip to content

Commit

Permalink
MVP
Browse files Browse the repository at this point in the history
  • Loading branch information
arodidev committed Jun 11, 2024
1 parent cb898c4 commit c898482
Showing 1 changed file with 62 additions and 0 deletions.
62 changes: 62 additions & 0 deletions src/utils/expression-runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit c898482

Please sign in to comment.