{"ScriptPreparationCode":"const supportedOperators = {\r\n \u0027-\u0027: {\r\n precedence: 1,\r\n operate: (lhs, rhs) =\u003E lhs - rhs,\r\n },\r\n \u0027\u002B\u0027: {\r\n precedence: 1,\r\n operate: (lhs, rhs) =\u003E lhs \u002B rhs,\r\n },\r\n \u0027/\u0027: {\r\n precedence: 3,\r\n operate: (lhs, rhs) =\u003E lhs / rhs,\r\n },\r\n \u0027*\u0027: {\r\n precedence: 4,\r\n operate: (lhs, rhs) =\u003E lhs * rhs,\r\n },\r\n \u0027^\u0027: {\r\n precedence: 5,\r\n operate: (lhs, rhs) =\u003E lhs ** rhs,\r\n },\r\n};\r\n\r\nfunction isOperator(value) {\r\n return !!supportedOperators[value];\r\n}\r\n\r\nfunction isOperand(value) {\r\n return !Number.isNaN(\u002Bvalue);\r\n}\r\n\r\nfunction getPrecedence(operator) {\r\n const supportedOperator = supportedOperators[operator];\r\n if (!supportedOperator) {\r\n return -1;\r\n }\r\n return supportedOperator.precedence;\r\n}\r\n\r\nfunction evaluateLastOperation(operators, operands) {\r\n const operator = supportedOperators[operators.pop()];\r\n const rhs = operands.pop();\r\n const lhs = operands.pop();\r\n return operator.operate(\u002Blhs, \u002Brhs);\r\n}\r\n\r\nfunction tokenizeExpression(expr) {\r\n const currentTokens = [];\r\n let currentValue = \u0027\u0027;\r\n\r\n for (let i = 0; i \u003C expr.length; i \u002B= 1) {\r\n if (expr[i] === \u0027 \u0027) {\r\n if (currentValue) {\r\n currentTokens.push(currentValue);\r\n currentValue = \u0027\u0027;\r\n }\r\n } else if (expr[i] === \u0027)\u0027) {\r\n if (currentValue) {\r\n currentTokens.push(currentValue);\r\n currentValue = \u0027\u0027;\r\n }\r\n currentTokens.push(\u0027)\u0027);\r\n } else if (isOperand(expr[i]) ||\r\n (expr[i] === \u0027.\u0027 \u0026\u0026 currentValue.indexOf(\u0027.\u0027) === -1 \u0026\u0026 expr[i \u002B 1]) ||\r\n (expr[i] === \u0027-\u0027 \u0026\u0026 currentValue.indexOf(\u0027-\u0027) === -1 \u0026\u0026 isOperand(expr[i \u002B 1]))) {\r\n currentValue \u002B= expr[i];\r\n } else if (expr[i] === \u0027(\u0027 || expr[i] === \u0027)\u0027 || isOperator(expr[i])) {\r\n currentTokens.push(expr[i]);\r\n }\r\n }\r\n if (currentValue) {\r\n currentTokens.push(currentValue);\r\n }\r\n return currentTokens;\r\n}\r\n\r\n\r\nfunction evaluateExpression(expr) {\r\n const tokens = tokenizeExpression(expr);\r\n const operands = [];\r\n const operators = [];\r\n\r\n tokens.forEach((token) =\u003E {\r\n if (isOperand(token)) {\r\n operands.push(token);\r\n } else if (token === \u0027(\u0027) {\r\n operators.push(token);\r\n } else if (token === \u0027)\u0027) {\r\n let operatorTop = operators[operators.length - 1];\r\n while (operators.length \u0026\u0026 operatorTop !== \u0027(\u0027) {\r\n operands.push(evaluateLastOperation(operators, operands));\r\n operatorTop = operators[operators.length - 1];\r\n }\r\n operators.pop();\r\n } else if (isOperator(token)) {\r\n let operatorTop = operators[operators.length - 1];\r\n while (operators.length \u0026\u0026 operatorTop !== \u0027(\u0027 \u0026\u0026 getPrecedence(token) \u003C= getPrecedence(operatorTop)) {\r\n operands.push(evaluateLastOperation(operators, operands));\r\n operatorTop = operators[operators.length - 1];\r\n }\r\n operators.push(token);\r\n }\r\n });\r\n\r\n while (operators.length) {\r\n operands.push(evaluateLastOperation(operators, operands));\r\n }\r\n\r\n return operands[0];\r\n}\r\n\r\nvar testMathExpression = \u0060142 * ( 2 / 0.9 ) ^ -1.2 * 0.9938 ^ 50\u0060;\r\n\r\nvar func = new Function(\u0060return ${testMathExpression}\u0060);","TestCases":[{"Name":"eval","Code":"eval(testMathExpression);","IsDeferred":false},{"Name":"new Function","Code":"func();","IsDeferred":false},{"Name":"mathjs","Code":"math.evaluate(testMathExpression);","IsDeferred":false},{"Name":"evaluateExpression","Code":"evaluateExpression(testMathExpression);","IsDeferred":false}]}