|
1 | 1 | # math-expression-evaluator
|
2 |
| -An extremely efficient, flexible and amazing evaluator for Math expression in Javascript.([Documentation](http://bugwheels94.github.io/math-expression-evaluator/)) |
| 2 | +An extremely efficient, flexible and amazing evaluator for Math expression in Javascript. |
3 | 3 |
|
4 | 4 | ## Use cases
|
5 | 5 | |Input|Result|Explanation|
|
@@ -27,7 +27,58 @@ An extremely efficient, flexible and amazing evaluator for Math expression in Ja
|
27 | 27 |
|
28 | 28 | bower install math-expression-evaluator
|
29 | 29 |
|
30 |
| -### How to run test |
| 30 | +## Usage |
| 31 | + |
| 32 | +### Using eval method of mexp object |
| 33 | + |
| 34 | +const mexp = new Mexp() |
| 35 | +var value = mexp.eval(exp); // 2 + 2 |
| 36 | + |
| 37 | +### Using constituents of eval methods of mexp object |
| 38 | + |
| 39 | +1. Create mexp object |
| 40 | + |
| 41 | + const mexp = new Mexp |
| 42 | + |
| 43 | +2. Parse an expression and then add additional detail to the tokens using |
| 44 | + |
| 45 | + var lexed = mexp.lex("expression"); |
| 46 | + which returns an array of token which will be further processed by methods toPostfix and postfixEval |
| 47 | + |
| 48 | +3. Now, that array is needed to be converted to postfix notation using |
| 49 | + |
| 50 | + var postfixed = mexp.toPostfix(lexed); |
| 51 | + which converts the array to postfix notation and return new array |
| 52 | + |
| 53 | +4. Now to get the value of expression use postfixEval |
| 54 | + |
| 55 | + var result = mexp.postfixEval(postfixed); |
| 56 | + where result contains the result. |
| 57 | + |
| 58 | + |
| 59 | +### Extending tokens |
| 60 | + |
| 61 | +1. Defining a token |
| 62 | + |
| 63 | + A token is defined similar way as 1.x version. You may refer to test file on examples on how to add tokens. Since this package is TS compatible, you will get autocomplete on `mexp.addToken` |
| 64 | + |
| 65 | + |
| 66 | +2. Adding tokens using addToken method of mexp object |
| 67 | + |
| 68 | + const mexp = new Mexp() |
| 69 | + mexp.addToken([token1, token2]) // tokens once added will be preserved in later evaluations |
| 70 | + |
| 71 | +3. Adding tokens using eval method of mexp object |
| 72 | + |
| 73 | + const mexp = new Mexp() |
| 74 | + mexp.eval("expression", [token1, token2]) // tokens once added will be preserved in later evaluations |
| 75 | + |
| 76 | +4. Adding token using constituents of eval method of mexp object |
| 77 | + |
| 78 | + const mexp = new Mexp() |
| 79 | + const answer = mexp.postfixEval(mexp.toPostfix(mexp.lexed("expression", [token1, token2]))) // tokens once added will be preserved in later evaluations |
| 80 | + console.log(answer) |
| 81 | +## How to run test |
31 | 82 |
|
32 | 83 | npm test
|
33 | 84 |
|
|
0 commit comments