Skip to content

Commit d653e02

Browse files
committed
feat(typescript): add TS
1 parent bcab658 commit d653e02

22 files changed

+23291
-31144
lines changed

.gitignore

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
node_modules
2-
.commit_msg
2+
.commit_msg
3+
dist/es/
4+
dist/types/

README.md

+53-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# 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.
33

44
## Use cases
55
|Input|Result|Explanation|
@@ -27,7 +27,58 @@ An extremely efficient, flexible and amazing evaluator for Math expression in Ja
2727

2828
bower install math-expression-evaluator
2929

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
3182

3283
npm test
3384

babel.config.js

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
const { NODE_ENV, BABEL_ENV } = process.env
2+
const cjs = NODE_ENV === 'test' || BABEL_ENV === 'commonjs'
3+
const loose = true
4+
5+
module.exports = {
6+
presets: [
7+
[
8+
'@babel/preset-env',
9+
{
10+
loose,
11+
12+
modules: false,
13+
exclude: ['@babel/plugin-transform-regenerator'],
14+
},
15+
],
16+
'@babel/preset-typescript',
17+
],
18+
plugins: [
19+
cjs && ['@babel/transform-modules-commonjs', { loose }],
20+
// [
21+
// '@babel/transform-runtime',
22+
// {
23+
// // useESModules: !cjs,
24+
// version: require('./package.json').dependencies['@babel/runtime'].replace(/^[^0-9]*/, ''),
25+
// },
26+
// ],
27+
].filter(Boolean),
28+
}

0 commit comments

Comments
 (0)