exprify 1.0.0 → 1.0.2
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/LICENSE +673 -673
- package/README.md +270 -135
- package/dist/exprify.cjs.js +2987 -503
- package/dist/exprify.cjs.js.map +1 -1
- package/dist/exprify.esm.js +2987 -497
- package/dist/exprify.esm.js.map +1 -1
- package/dist/exprify.js +3007 -523
- package/dist/exprify.js.map +1 -1
- package/dist/exprify.min.js +2 -2
- package/dist/exprify.min.js.map +1 -1
- package/docs/README.md +34 -0
- package/docs/assets/css/style.scss +4 -0
- package/docs/tokenType.txt +21 -0
- package/package.json +7 -3
- package/src/core/Exprify.js +0 -70
- package/src/functions/externalFunctions.js +0 -19
- package/src/functions/internalFunctions.js +0 -53
- package/src/index.js +0 -38
- package/src/math/operations.js +0 -48
- package/src/parser/evaluator.js +0 -57
- package/src/parser/infixToPostfix.js +0 -78
- package/src/parser/tokenizer.js +0 -145
- package/src/utils/typeConverter.js +0 -63
- package/src/variables/variables.js +0 -28
package/README.md
CHANGED
|
@@ -1,135 +1,270 @@
|
|
|
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
|
-
<script>
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
###
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
```
|
|
65
|
-
expr.
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
console.log(
|
|
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
|
-
2
|
|
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 expression parser and evaluator for math-heavy apps. It supports arithmetic, variables, custom functions, unit conversion, matrices, complex numbers, symbolic helpers, and a growing set of linear algebra utilities.
|
|
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
|
+
expr.setVariable("x", 10);
|
|
24
|
+
console.log(expr.evaluate("x + 5"));
|
|
25
|
+
// 15
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
## Browser Usage
|
|
29
|
+
|
|
30
|
+
```html
|
|
31
|
+
<script src="https://unpkg.com/exprify"></script>
|
|
32
|
+
<script>
|
|
33
|
+
const expr = new Exprify();
|
|
34
|
+
console.log(expr.evaluate("(10 + 5) * 2"));
|
|
35
|
+
</script>
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
`unpkg` resolves to the browser bundle from `dist/exprify.min.js`.
|
|
39
|
+
|
|
40
|
+
## API
|
|
41
|
+
|
|
42
|
+
### `new Exprify()`
|
|
43
|
+
|
|
44
|
+
Creates a new evaluator instance with isolated state for:
|
|
45
|
+
|
|
46
|
+
- variables
|
|
47
|
+
- functions
|
|
48
|
+
- units
|
|
49
|
+
- compiled-expression cache
|
|
50
|
+
|
|
51
|
+
### `expr.evaluate(expression)`
|
|
52
|
+
|
|
53
|
+
Parses and evaluates an expression string.
|
|
54
|
+
|
|
55
|
+
```js
|
|
56
|
+
expr.evaluate("10 + 5 * 2");
|
|
57
|
+
// 20
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
### `expr.parse(expression)`
|
|
61
|
+
|
|
62
|
+
Returns `{ tokens, ast }`.
|
|
63
|
+
|
|
64
|
+
```js
|
|
65
|
+
const parsed = expr.parse("2 inch to cm");
|
|
66
|
+
console.log(parsed.tokens);
|
|
67
|
+
console.log(parsed.ast);
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
### `expr.compile(expression)`
|
|
71
|
+
|
|
72
|
+
Compiles an expression once and returns a reusable function.
|
|
73
|
+
|
|
74
|
+
```js
|
|
75
|
+
const area = expr.compile("width * height");
|
|
76
|
+
|
|
77
|
+
console.log(area({ width: 6, height: 4 }));
|
|
78
|
+
// 24
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
### `expr.setVariable(name, value)` / `expr.getVariable(name)`
|
|
82
|
+
|
|
83
|
+
Stores and reuses values across evaluations.
|
|
84
|
+
|
|
85
|
+
```js
|
|
86
|
+
expr.setVariable("x", 10);
|
|
87
|
+
expr.setVariable("y", 5);
|
|
88
|
+
|
|
89
|
+
console.log(expr.evaluate("x + y * 2"));
|
|
90
|
+
// 20
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
### `expr.addFunction(name, fn)`
|
|
94
|
+
|
|
95
|
+
Registers a custom JavaScript function.
|
|
96
|
+
|
|
97
|
+
```js
|
|
98
|
+
expr.addFunction("double", (n) => n * 2);
|
|
99
|
+
|
|
100
|
+
console.log(expr.evaluate("double(5) + 3"));
|
|
101
|
+
// 13
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
### Inline Function Definitions
|
|
105
|
+
|
|
106
|
+
You can define functions inside expressions.
|
|
107
|
+
|
|
108
|
+
```js
|
|
109
|
+
expr.evaluate("hyp(a, b) = sqrt(a ^ 2 + b ^ 2)");
|
|
110
|
+
|
|
111
|
+
console.log(expr.evaluate("hyp(3, 4)"));
|
|
112
|
+
// 5
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
## Features
|
|
116
|
+
|
|
117
|
+
### Arithmetic
|
|
118
|
+
|
|
119
|
+
```js
|
|
120
|
+
expr.evaluate("2 + 3 * 4");
|
|
121
|
+
// 14
|
|
122
|
+
|
|
123
|
+
expr.evaluate("(2 + 3) * 4");
|
|
124
|
+
// 20
|
|
125
|
+
|
|
126
|
+
expr.evaluate("11n ^ 2n");
|
|
127
|
+
// 121n
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
### Strings, Booleans, Complex Numbers
|
|
131
|
+
|
|
132
|
+
```js
|
|
133
|
+
expr.evaluate('"Hello " + "World"');
|
|
134
|
+
// "Hello World"
|
|
135
|
+
|
|
136
|
+
expr.evaluate("true && false");
|
|
137
|
+
// false
|
|
138
|
+
|
|
139
|
+
expr.evaluate("9 / 3 + 2i");
|
|
140
|
+
// "3 + 2i"
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
### Unit Conversion
|
|
144
|
+
|
|
145
|
+
```js
|
|
146
|
+
expr.evaluate("2 inch to cm");
|
|
147
|
+
// "5.08 cm"
|
|
148
|
+
|
|
149
|
+
expr.evaluate("5 cm + 2 inch");
|
|
150
|
+
// "10.08 cm"
|
|
151
|
+
|
|
152
|
+
expr.evaluate("5cm + 0.2 m in inch");
|
|
153
|
+
// "9.84251968503937 inch"
|
|
154
|
+
|
|
155
|
+
expr.evaluate("a = 5.08 cm + 2 inch");
|
|
156
|
+
expr.evaluate("a to inch");
|
|
157
|
+
// "4 inch"
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
### Matrices
|
|
161
|
+
|
|
162
|
+
Exprify supports matrix literals with `;` as row separators.
|
|
163
|
+
|
|
164
|
+
```js
|
|
165
|
+
expr.evaluate("a = [1, 2, 3; 4, 5, 6]");
|
|
166
|
+
// {"exprify":"DenseMatrix","data":[[1,2,3],[4,5,6]],"size":[2,3]}
|
|
167
|
+
|
|
168
|
+
expr.evaluate("a[2, 3]");
|
|
169
|
+
// 6
|
|
170
|
+
|
|
171
|
+
expr.evaluate("a[1:2, 2]");
|
|
172
|
+
// "2\n5"
|
|
173
|
+
|
|
174
|
+
expr.evaluate("a[3, 1:3] = [7, 8, 9]");
|
|
175
|
+
// "7\t8\t9"
|
|
176
|
+
|
|
177
|
+
expr.evaluate("det([-1, 2; 3, 1])");
|
|
178
|
+
// -7
|
|
179
|
+
```
|
|
180
|
+
|
|
181
|
+
### Linear Algebra Helpers
|
|
182
|
+
|
|
183
|
+
```js
|
|
184
|
+
expr.evaluate("lup([[2, 1], [1, 4]])");
|
|
185
|
+
// {"L":{"exprify":"DenseMatrix",...},"U":{"exprify":"DenseMatrix",...},"p":[0,1]}
|
|
186
|
+
|
|
187
|
+
expr.evaluate("lyap([[-2, 0], [1, -4]], [[3, 1], [1, 3]])");
|
|
188
|
+
// {"exprify":"DenseMatrix","data":[[0.75,0.2916666666666667],[0.2916666666666667,0.44791666666666663]],"size":[2,2]}
|
|
189
|
+
|
|
190
|
+
expr.evaluate("qr([[1, -1, 4], [1, 4, -2], [1, 4, 2], [1, -1, 0]])");
|
|
191
|
+
// {"Q":{"exprify":"DenseMatrix",...},"R":{"exprify":"DenseMatrix",...}}
|
|
192
|
+
|
|
193
|
+
expr.evaluate("polynomialRoot(-6, 11, -6, 1)");
|
|
194
|
+
// [1,3,2]
|
|
195
|
+
```
|
|
196
|
+
|
|
197
|
+
Available helpers currently include `det`, `lsolve`, `lup`, `lyap`, `qr`, and `polynomialRoot`.
|
|
198
|
+
|
|
199
|
+
### Algebra Helpers
|
|
200
|
+
|
|
201
|
+
```js
|
|
202
|
+
expr.evaluate('simplify("2x + x")');
|
|
203
|
+
// "3 * x"
|
|
204
|
+
|
|
205
|
+
expr.evaluate('derivative("2x^2 + 3x + 4", "x")');
|
|
206
|
+
// "4 * x + 3"
|
|
207
|
+
|
|
208
|
+
expr.evaluate('rationalize("2x/y - y/(x+1)", true)');
|
|
209
|
+
// {"numerator":"2 * x ^ 2 + 2 * x - y ^ 2","denominator":"x * y + y","coefficients":[],"variables":["x","y"],"expression":"(2 * x ^ 2 + 2 * x - y ^ 2) / (x * y + y)"}
|
|
210
|
+
```
|
|
211
|
+
|
|
212
|
+
### Parse and AST Utilities
|
|
213
|
+
|
|
214
|
+
```js
|
|
215
|
+
expr.evaluate('leafCount("e^(i*pi)-1")');
|
|
216
|
+
// 4
|
|
217
|
+
|
|
218
|
+
expr.evaluate('leafCount(parse("{a: 22/7, b: 10^(1/2)}"))');
|
|
219
|
+
// 5
|
|
220
|
+
```
|
|
221
|
+
|
|
222
|
+
### Built-in Functions
|
|
223
|
+
|
|
224
|
+
Common built-ins include:
|
|
225
|
+
|
|
226
|
+
- `max`, `min`, `abs`, `round`, `floor`, `ceil`, `sqrt`, `pow`
|
|
227
|
+
- `sin`, `cos`, `tan`, `asin`, `acos`, `atan`
|
|
228
|
+
- `log`, `log10`, `exp`, `random`
|
|
229
|
+
- `clamp`, `if`, `length`, `typeof`
|
|
230
|
+
- `det`, `lsolve`, `lup`, `lyap`, `qr`, `polynomialRoot`
|
|
231
|
+
- `simplify`, `derivative`, `rationalize`, `leafCount`, `parse`
|
|
232
|
+
|
|
233
|
+
## Return Types
|
|
234
|
+
|
|
235
|
+
Depending on the expression, `evaluate()` may return:
|
|
236
|
+
|
|
237
|
+
- numbers / bigint / booleans
|
|
238
|
+
- strings
|
|
239
|
+
- formatted unit strings like `"5.08 cm"`
|
|
240
|
+
- formatted complex strings like `"3 + 2i"`
|
|
241
|
+
- matrix wrapper JSON strings such as `{"exprify":"DenseMatrix",...}`
|
|
242
|
+
- JSON strings for structured helper outputs like `lup()` or `rationalize(..., true)`
|
|
243
|
+
|
|
244
|
+
## Manual Build
|
|
245
|
+
|
|
246
|
+
```bash
|
|
247
|
+
git clone https://github.com/code-hemu/Exprify.git
|
|
248
|
+
cd Exprify
|
|
249
|
+
npm install
|
|
250
|
+
npm run build
|
|
251
|
+
```
|
|
252
|
+
|
|
253
|
+
Build output is written to `dist/`.
|
|
254
|
+
|
|
255
|
+
## Testing
|
|
256
|
+
|
|
257
|
+
```bash
|
|
258
|
+
npm test
|
|
259
|
+
```
|
|
260
|
+
|
|
261
|
+
## License
|
|
262
|
+
|
|
263
|
+
Exprify is licensed under GPL-3.0. Copyright (c) [Nirmal Paul](https://github.com/nirmalpaul383/).
|
|
264
|
+
|
|
265
|
+
## Contributing
|
|
266
|
+
|
|
267
|
+
1. Fork the repository.
|
|
268
|
+
2. Create a branch: `git checkout -b feature/your-feature`
|
|
269
|
+
3. Commit your changes: `git commit -m "Add your feature"`
|
|
270
|
+
4. Push your branch and open a pull request.
|