ViewPoint is a math expression parser and evaluator that supports runtime data-type checking written in native JS. It allows you to evaluate mathematical expressions with ease while ensuring the correctness of the input data.
- Expression Evaluation:
- Parses and evaluates mathematical expressions like '5*52+285/2'
- Supports basic arithmetic operators (+, -, *, /,^, %). [Note: here % is implemented as modules / reminder operator]
- Supports nested expressions with parentheses (e.g. (36/2)-4)
- Allows complex expressions with multiple levels of nesting (e.g (52/8+2)+56*((25/2)*4+(8-2))
- Supports multiple datatypes e.g expressions with decimal / floating points numbers, BigInt, String, and Boolean datatype
- Data Type Validation:
- Validates data types during expression evaluation, throwing errors for invalid operations (e.g., attempting sum or multiplication between strings and numbers).
- Expression Tokenization:
- It can tokenize a mathematical expression into individual tokens and can return tokenized expression as an array (e.g expression = "5 + 72+(85^2+1)", token = [5,'+',7,'',2,'+','(',85,'^',2,'+',1,')'])
- Infix to Postfix Conversion:
- It can convert infix expressions to postfix notation and return as an array
- JavaScript Variable Support:
- ViewPoint supports using of JavaScript variables in an expressions using
${}
and backticks (`
)
- ViewPoint supports using of JavaScript variables in an expressions using
- External Variable Support:
- Allows users to define external variables using the
.var()
method
- Allows users to define external variables using the
- Operator Order Control:
- Follows PEMDAS (Parentheses, Exponents, Multiplication and Division, and Addition and Subtraction) order of operations by default
- Allows users to define their own custom order of operations (if needed)
- Customizable Operator Behavior:
- Allows users to re-define the math operator (e.g. '*' or '%') behavior to suit specific requirements (if needed)
To use this evaluator/library, firstly, you need to link ViewPoint.js to your project. You can do that by using two ways:
- Download/Clone and Use: Download or clone the repository and use the ViewPoint.js file in your project.
- Use via CDN:
- Use the library directly via CDN using the following script tag:
https://cdn.jsdelivr.net/gh/nirmalpaul383/ViewPoint/ViewPoint.js
- For using it in browser/webpage you can use
<script src="https://cdn.jsdelivr.net/gh/nirmalpaul383/ViewPoint/ViewPoint.js"></script>
- Use the library directly via CDN using the following script tag:
- create a ViewPoint object using
new ViewPoint()
keywords:
//Create a new ViewPoint object with ViewPoint class
let ViewPoint_obj = new ViewPoint();
- Store your expression in string format:
//Sample Expression
const expresion = `((52/8+2)+56*((25/2)*4+(8-2)))*2`;
- For evaluating an expression use
.evaluate()
method:
//Expression evaluating
let output = ViewPoint_obj.evaluate(expresion);
//For output
console.log(output); //Output 6289 to the console
//Create a new ViewPoint object with ViewPoint class
let ViewPoint_obj = new ViewPoint();
//Simple Expression evaluation
console.log(ViewPoint_obj.evaluate(`25+5*2`)); //Output 35 to the console
//Nested expression evaluation
console.log(ViewPoint_obj.evaluate(`((52/8+2)+56*((25/2)*4+(8-2)))*2`)); //Output 6289 to the console
//BigInt Expression evaluation
console.log(ViewPoint_obj.evaluate(`11n ^2n`)); //Output 121n to the console
//String concatenation: '+' Operator behaves as concatenation operator if all operands are string
console.log(ViewPoint_obj.evaluate(`"Hello " + "World"`)); //Output "Hello World" to the console
//Invalid Expression: One operand is a string and another one is number
console.log(ViewPoint_obj.evaluate(`"45" + 5`)); //Throws a datatype error
//Invalid Expression: One operand is a number and another one is boolean
console.log(ViewPoint_obj.evaluate(`45 * true`)); //Throws a datatype error
//Invalid Expression: unclosed quoted text
console.log(ViewPoint_obj.evaluate(`"Hello World `)); //Throws a error message for unclosed quoted text
//Create a new ViewPoint object with the viewpoint class
let ViewPoint_obj = new ViewPoint();
//Sample expression with number, operators, BigInt, parentheses, floating point number, quoted text, and white space
const expresion = `2 / 5.04 +5n +"Sample quoted text" + (40*(45-2))`;
//For tokenization of expression and storing into the token array
let token_array = ViewPoint_obj.tokenize(expresion)
//For output the token array into the console
console.log(token_array)
/*
[ 2, '/', 5.04, '+', 5n, '+', 'Sample quoted text', '+', '(', 40, '\*', '(', 45, '-', 2, ')', ')' ]
*/
//Create a new ViewPoint object with viewpoint class
let VP = new ViewPoint();
//Sample expression
const expresion = `4+8*(5-1)`;
//For tokenization of expression and storing into the token array
let token_array = VP.tokenize(expresion)
//For converting an infix expression array to its equivalent postfix expression array and storing into the postfix array
let postFix_Array = VP.infix_to_postfix(...token_array)
//For output the postfix expression array into the console
console.log(postFix_Array) //Output: [ 4, 8, 5, 1, '-', '*', '+' ]
//Create a new ViewPoint object with the ViewPoint class
let VP = new ViewPoint();
//Sample JavaScript variable
let a = 5;
//Sample expression with JavaScript variable
const expresion = `35 +(${a}*9)*2`;
//Expression evaluating and storing the result into the output variable
let output = VP.evaluate(expresion);
//For output the result
console.log(output); //Output: 125
//Create a new ViewPoint object with viewpoint class
let ViewPoint_obj = new ViewPoint();
//Defining external variable with the name of "myVar"
ViewPoint_obj.var("myVar", 400);
//Sample expression with an external variable
const expresion = `35 *100 - myVar`;
//Expression evaluating and storing the result into the output variable
let output = ViewPoint_obj.evaluate(expresion);
//For output the result
console.log(output); //Output: 3100
//Create a new ViewPoint object with viewpoint class
let ViewPoint_obj = new ViewPoint();
//Sample expression
const expresion = `4*2+1`;
//Expression evaluating with default operator precedence
let output = ViewPoint_obj.evaluate(expresion);
//Changing the default operator_precedence in the ViewPoint Obejct
//promote '+' and '-' operator to highest precedence
ViewPoint_obj.ViewPoint_math_def.operator_precedence = {
'^': 1,
'*': 1,
'/': 1,
'%': 1,
'+': 2,
'-': 2,
}
//Expression re-evaluating
let output2 = ViewPoint_obj.evaluate(expresion);
//For output the actual result
console.log(output); //Output: 9 (First * then +)
//For output the re-evaluating result (after changing the default operator_precedence)
console.log(output2); //Output: 12 (First + then *)
//Create a new ViewPoint object with viewpoint class
let ViewPoint_obj = new ViewPoint();
//Sample expression
const expresion = `4*2`;
//Expression evaluating with default operator behavior
let output = ViewPoint_obj.evaluate(expresion);
//Override default behavior of multiplication ('*') operator in the ViewPoint object and redefine * operator to behave like '+' (addition) operator
ViewPoint_obj.ViewPoint_math_def.multiplication = function (val1, val2) {
return val1 + val2;
}
//Expression re-evaluating
let output2 = ViewPoint_obj.evaluate(expresion);
//For output the actual result
console.log(output); //Output: 8 (multiplication ('*') operator works as multiplication)
//For output the re-evaluating result (multiplication ('*') operator works as addition ('+'))
console.log(output2); //Output: 6
This object is used for defining of some basic math logic and math operations for ViewPoint
Name | Type | Details |
---|---|---|
operator_precedence | Object | This object contains the operator's precedence value according to the BODMAS or PEMDAS rule Default operator's precedence values: operator_precedence: {'^': 4, '*': 3, '/': 2, '%': 2,'+': 1,'-': 1} |
exponents(value1, value2) | Method | Definition of Power(^) operator Behavior |
multiplication(value1, value2) | Method | Definition of Multiplication(*) operator Behavior |
division(value1, value2) | Method | Definition of Division(/) operator Behavior |
modulus(value1, value2) | Method | Definition of Modulus(%) operator Behavior |
addition(value1, value2) | Method | Definition of Addition(+) and concatenation(+) operator Behavior |
subtraction(value1, value2) | Method | Definition of Subtraction(-) operator Behavior |
If you like this project please give a star to this project . This project is originally made by me(N Paul). My github profile , My youtube page , facebook page
This is an open-source program. You are welcome to modify it...
Thank you for trying it out!