exprify 1.0.0 → 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.gitattributes +2 -0
- package/.github/workflows/ci.yml +40 -0
- package/.github/workflows/npm-publish.yml +38 -0
- package/.github/workflows/security-audit.yml +34 -0
- package/CHANGELOG.md +11 -0
- package/LICENSE +673 -673
- package/README.md +203 -135
- package/dist/exprify.cjs.js +2320 -503
- package/dist/exprify.cjs.js.map +1 -1
- package/dist/exprify.esm.js +2320 -497
- package/dist/exprify.esm.js.map +1 -1
- package/dist/exprify.js +2340 -523
- package/dist/exprify.js.map +1 -1
- package/dist/exprify.min.js +2 -2
- package/dist/exprify.min.js.map +1 -1
- package/doc/tokenType.txt +48 -0
- package/package.json +7 -3
- package/rollup.config.js +80 -0
- package/src/assets/capture.jpg +0 -0
- package/src/core/Exprify.js +140 -70
- package/src/core/context.js +30 -0
- package/src/function/executor.js +64 -0
- package/src/function/internal.js +270 -0
- package/src/function/registry.js +68 -0
- package/src/index.js +2 -38
- package/src/math/operations.js +37 -47
- package/src/parser/astBuild.js +508 -0
- package/src/parser/evaluator.js +430 -57
- package/src/parser/tokenizer.js +399 -145
- package/src/utils/globalUnits.js +217 -0
- package/src/utils/store.js +178 -0
- package/src/variables/store.js +75 -0
- package/test/browser.html +23 -0
- package/test/exprify.test.js +140 -0
- package/src/functions/externalFunctions.js +0 -19
- package/src/functions/internalFunctions.js +0 -53
- package/src/parser/infixToPostfix.js +0 -78
- package/src/utils/typeConverter.js +0 -63
- package/src/variables/variables.js +0 -28
package/README.md
CHANGED
|
@@ -1,135 +1,203 @@
|
|
|
1
|
-
# Exprify
|
|
2
|
-
|
|
3
|
-
[](https://github.com/code-hemu/Exprify)
|
|
4
|
-
|
|
5
|
-
Exprify is a JavaScript expression parser and evaluator
|
|
6
|
-
|
|
7
|
-
##
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
```
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
expr.
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
expr.
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
//
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
console.log(expr.evaluate(
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
1
|
+
# Exprify
|
|
2
|
+
|
|
3
|
+
[](https://github.com/code-hemu/Exprify)
|
|
4
|
+
|
|
5
|
+
Exprify is a JavaScript math expression parser and evaluator with runtime type checking. It supports arithmetic, variables, custom functions, unit conversion, matrices, complex numbers, and a small set of algebra helpers.
|
|
6
|
+
|
|
7
|
+
## Installation
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm install exprify
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Quick Start
|
|
14
|
+
|
|
15
|
+
```js
|
|
16
|
+
import Exprify from "exprify";
|
|
17
|
+
|
|
18
|
+
const expr = new Exprify();
|
|
19
|
+
|
|
20
|
+
console.log(expr.evaluate("5 + 7 * 2"));
|
|
21
|
+
// 19
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
## Browser Usage
|
|
25
|
+
|
|
26
|
+
```html
|
|
27
|
+
<script src="https://unpkg.com/exprify"></script>
|
|
28
|
+
<script>
|
|
29
|
+
const expr = new Exprify();
|
|
30
|
+
console.log(expr.evaluate("(10 + 5) * 2"));
|
|
31
|
+
</script>
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
## API
|
|
35
|
+
|
|
36
|
+
### `new Exprify()`
|
|
37
|
+
|
|
38
|
+
Creates a new evaluator instance with isolated variable, function, unit, and compile-cache state.
|
|
39
|
+
|
|
40
|
+
### `expr.evaluate(expression)`
|
|
41
|
+
|
|
42
|
+
Parses and evaluates an expression string.
|
|
43
|
+
|
|
44
|
+
```js
|
|
45
|
+
expr.evaluate("10 + 5 * 2");
|
|
46
|
+
// 20
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
### `expr.parse(expression)`
|
|
50
|
+
|
|
51
|
+
Returns the token list and AST for an expression.
|
|
52
|
+
|
|
53
|
+
```js
|
|
54
|
+
const parsed = expr.parse("2 inch to cm");
|
|
55
|
+
console.log(parsed.tokens);
|
|
56
|
+
console.log(parsed.ast);
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
### `expr.compile(expression)`
|
|
60
|
+
|
|
61
|
+
Compiles an expression once and returns a reusable function. You can pass a temporary scope object when calling it.
|
|
62
|
+
|
|
63
|
+
```js
|
|
64
|
+
const area = expr.compile("width * height");
|
|
65
|
+
|
|
66
|
+
console.log(area({ width: 6, height: 4 }));
|
|
67
|
+
// 24
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
### `expr.setVariable(name, value)` / `expr.getVariable(name)`
|
|
71
|
+
|
|
72
|
+
Stores values on the instance for reuse across evaluations.
|
|
73
|
+
|
|
74
|
+
```js
|
|
75
|
+
expr.setVariable("x", 10);
|
|
76
|
+
expr.setVariable("y", 5);
|
|
77
|
+
|
|
78
|
+
console.log(expr.evaluate("x + y * 2"));
|
|
79
|
+
// 20
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
### `expr.addFunction(name, fn)`
|
|
83
|
+
|
|
84
|
+
Registers a custom function.
|
|
85
|
+
|
|
86
|
+
```js
|
|
87
|
+
expr.addFunction("double", (n) => n * 2);
|
|
88
|
+
|
|
89
|
+
console.log(expr.evaluate("double(5) + 3"));
|
|
90
|
+
// 13
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
## Supported Features
|
|
94
|
+
|
|
95
|
+
### Arithmetic and precedence
|
|
96
|
+
|
|
97
|
+
```js
|
|
98
|
+
expr.evaluate("2 + 3 * 4");
|
|
99
|
+
// 14
|
|
100
|
+
|
|
101
|
+
expr.evaluate("(2 + 3) * 4");
|
|
102
|
+
// 20
|
|
103
|
+
|
|
104
|
+
expr.evaluate("11n ^ 2n");
|
|
105
|
+
// 121n
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
### Strings and booleans
|
|
109
|
+
|
|
110
|
+
```js
|
|
111
|
+
expr.evaluate('"Hello " + "World"');
|
|
112
|
+
// "Hello World"
|
|
113
|
+
|
|
114
|
+
expr.evaluate("true && false");
|
|
115
|
+
// false
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
### Built-in functions
|
|
119
|
+
|
|
120
|
+
```js
|
|
121
|
+
expr.evaluate("max(10, 25, 7)");
|
|
122
|
+
// 25
|
|
123
|
+
|
|
124
|
+
expr.evaluate("min(10, 25, 7)");
|
|
125
|
+
// 7
|
|
126
|
+
|
|
127
|
+
expr.evaluate("sqrt(81)");
|
|
128
|
+
// 9
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
Common built-ins include `max`, `min`, `abs`, `round`, `floor`, `ceil`, `sqrt`, `pow`, `sin`, `cos`, `tan`, `log`, `log10`, `exp`, `clamp`, `if`, `length`, `typeof`, `det`, `simplify`, and `derivative`.
|
|
132
|
+
|
|
133
|
+
### Unit conversion
|
|
134
|
+
|
|
135
|
+
```js
|
|
136
|
+
expr.evaluate("2 inch to cm");
|
|
137
|
+
// "5.08 cm"
|
|
138
|
+
|
|
139
|
+
expr.evaluate("5 cm + 2 inch");
|
|
140
|
+
// "10.08 cm"
|
|
141
|
+
|
|
142
|
+
expr.evaluate("5cm + 0.2 m in inch");
|
|
143
|
+
// "9.84251968503937 inch"
|
|
144
|
+
```
|
|
145
|
+
|
|
146
|
+
### Matrices
|
|
147
|
+
|
|
148
|
+
```js
|
|
149
|
+
expr.evaluate("det([-1, 2; 3, 1])");
|
|
150
|
+
// -7
|
|
151
|
+
|
|
152
|
+
expr.evaluate("a = [1, 2, 3; 4, 5, 6]");
|
|
153
|
+
expr.evaluate("a[2, 3]");
|
|
154
|
+
// 6
|
|
155
|
+
|
|
156
|
+
expr.evaluate("a[1:2, 2]");
|
|
157
|
+
// "2\n5"
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
### Complex numbers
|
|
161
|
+
|
|
162
|
+
```js
|
|
163
|
+
expr.evaluate("9 / 3 + 2i");
|
|
164
|
+
// "3 + 2i"
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
### Algebra helpers
|
|
168
|
+
|
|
169
|
+
```js
|
|
170
|
+
expr.evaluate('simplify("2x + x")');
|
|
171
|
+
// "3 * x"
|
|
172
|
+
|
|
173
|
+
expr.evaluate('derivative("2x^2 + 3x + 4", "x")');
|
|
174
|
+
// "4 * x + 3"
|
|
175
|
+
```
|
|
176
|
+
|
|
177
|
+
## Manual Build
|
|
178
|
+
|
|
179
|
+
```bash
|
|
180
|
+
git clone https://github.com/code-hemu/Exprify.git
|
|
181
|
+
cd Exprify
|
|
182
|
+
npm install
|
|
183
|
+
npm run build
|
|
184
|
+
```
|
|
185
|
+
|
|
186
|
+
Build output is generated in `dist/`.
|
|
187
|
+
|
|
188
|
+
## Testing
|
|
189
|
+
|
|
190
|
+
```bash
|
|
191
|
+
npm test
|
|
192
|
+
```
|
|
193
|
+
|
|
194
|
+
## License
|
|
195
|
+
|
|
196
|
+
Exprify is licensed under GPL-3.0. Copyright (c) [Nirmal Paul](https://github.com/nirmalpaul383/).
|
|
197
|
+
|
|
198
|
+
## Contributing
|
|
199
|
+
|
|
200
|
+
1. Fork the repository.
|
|
201
|
+
2. Create a branch: `git checkout -b feature/your-feature`
|
|
202
|
+
3. Commit your changes: `git commit -m "Add your feature"`
|
|
203
|
+
4. Push the branch and open a pull request.
|