som-exp-sdk
Version:
Evaluate the User Expression
115 lines (114 loc) • 6.55 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.LoopExpression = void 0;
const base_expression_1 = require("./base-expression");
const constants_1 = require("./constants");
class LoopExpression {
constructor() {
this.loopCount = (inputExp) => {
// Ex: loopCount('total=total+5 UNTIL total>=20') // return 5
let output = this.const.INVALID_EXPRESSION;
try {
const regex = this.const.LOOP_COUNT_REGEX;
if (regex.test(inputExp)) {
let loopVariable = "0";
const exprVariables = regex.exec(inputExp);
if (typeof exprVariables != undefined && (exprVariables === null || exprVariables === void 0 ? void 0 : exprVariables.groups)) {
let grpValExp1 = this.baseExpression.removeSpaces(exprVariables.groups[this.const.REGEX_EXP1].trim());
let grpValExp2 = this.baseExpression.removeSpaces(exprVariables.groups[this.const.REGEX_EXP2].trim());
let loopType = exprVariables.groups[this.const.REGEX_LOOP_TYPE].trim();
let conditionString = exprVariables.groups[this.const.REGEX_CONDITION].trim();
let wrapVal = this.baseExpression.wrapInSpace(loopVariable);
let booleanLogicBuilder = (!!conditionString) ? wrapVal + conditionString : wrapVal;
let condition = false;
let isConditionBoolean = Boolean(this.baseExpression.evaluateExpression(booleanLogicBuilder));
let inputExpr = "";
if (typeof isConditionBoolean === this.const.TYPE_BOOLEAN && booleanLogicBuilder) {
let count = 0;
do {
inputExpr = inputExpr + grpValExp1 + wrapVal + grpValExp2;
loopVariable = this.baseExpression.evaluateExpression(inputExpr);
booleanLogicBuilder = "";
wrapVal = this.baseExpression.wrapInSpace(loopVariable);
booleanLogicBuilder = booleanLogicBuilder + wrapVal + conditionString;
condition = (!!booleanLogicBuilder)
? Boolean(this.baseExpression.evaluateExpression(booleanLogicBuilder))
: false;
if (count < this.const.MAX_LOOP_COUNT) {
count++;
inputExpr = "";
}
else {
console.log(`Maximum number of iterations reached ${this.const.MAX_LOOP_COUNT}`);
break;
}
if (!!loopType) {
if (loopType.toLowerCase() == this.const.WHILE) {
condition = Boolean(this.baseExpression.evaluateExpression(booleanLogicBuilder));
}
else if (loopType.toLowerCase() == this.const.UNTIL) {
condition = !(this.baseExpression.evaluateExpression(booleanLogicBuilder));
}
}
else {
condition = false;
}
} while (condition);
output = count.toString();
}
else {
console.log(`'${booleanLogicBuilder.toString()}' is not a valid Boolean expression.`);
}
}
else {
console.log(`No groups found for expression ${inputExp}`);
}
}
else {
console.log(`Input expression: ${inputExp} is not a valid expression.`);
}
}
catch (ex) {
console.log(`Error occurred when evaluating expression ${inputExp}. Error is ${ex}`);
}
return output;
};
this.forLoop = (inputExp) => {
// Ex: forLoop('60,total=2000.00+total') // return 120000
let output = this.const.INVALID_EXPRESSION;
try {
const regex = this.const.FOR_LOOP_REGEX;
if (inputExp && regex.test(inputExp)) {
let exprVariables = regex.exec(inputExp);
if (typeof exprVariables != undefined && (exprVariables === null || exprVariables === void 0 ? void 0 : exprVariables.groups)) {
let loopVariable = "0";
let count = parseInt(this.baseExpression.removeSpaces(exprVariables.groups[this.const.REGEX_COUNT].trim()));
let grpValExp1 = this.baseExpression.removeSpaces(exprVariables.groups[this.const.REGEX_EXP1].trim());
let grpValExp2 = this.baseExpression.removeSpaces(exprVariables.groups[this.const.REGEX_EXP2].trim());
let inputExpr = "";
for (let itr = 0; itr < count; ++itr) {
let wrapVal = this.baseExpression.wrapInSpace(loopVariable);
inputExpr = inputExpr + grpValExp1 + wrapVal + grpValExp2;
loopVariable = this.baseExpression.evaluateExpression(inputExpr);
inputExpr = "";
}
output = loopVariable;
}
else {
console.log(`No groups found for expression ${inputExp}`);
}
}
else {
console.log(`Input expression: ${inputExp} is not a valid expression.`);
}
}
catch (ex) {
console.log(`Error occurred when evaluating expression ${inputExp}. Error is ${ex}`);
}
return output;
};
this.const = new constants_1.Constants();
this.baseExpression = new base_expression_1.BaseExpression();
}
}
exports.LoopExpression = LoopExpression;