exprify 1.0.1 → 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/README.md +100 -33
- package/dist/exprify.cjs.js +671 -4
- package/dist/exprify.cjs.js.map +1 -1
- package/dist/exprify.esm.js +671 -4
- package/dist/exprify.esm.js.map +1 -1
- package/dist/exprify.js +672 -5
- 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 +1 -1
- package/.gitattributes +0 -2
- package/.github/workflows/ci.yml +0 -40
- package/.github/workflows/npm-publish.yml +0 -38
- package/.github/workflows/security-audit.yml +0 -34
- package/CHANGELOG.md +0 -11
- package/doc/tokenType.txt +0 -48
- package/rollup.config.js +0 -80
- package/src/assets/capture.jpg +0 -0
- package/src/core/Exprify.js +0 -140
- package/src/core/context.js +0 -30
- package/src/function/executor.js +0 -64
- package/src/function/internal.js +0 -270
- package/src/function/registry.js +0 -68
- package/src/index.js +0 -2
- package/src/math/operations.js +0 -38
- package/src/parser/astBuild.js +0 -508
- package/src/parser/evaluator.js +0 -430
- package/src/parser/tokenizer.js +0 -399
- package/src/utils/globalUnits.js +0 -217
- package/src/utils/store.js +0 -178
- package/src/variables/store.js +0 -75
- package/test/browser.html +0 -23
- package/test/exprify.test.js +0 -140
package/README.md
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
[](https://github.com/code-hemu/Exprify)
|
|
4
4
|
|
|
5
|
-
Exprify is a JavaScript
|
|
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
6
|
|
|
7
7
|
## Installation
|
|
8
8
|
|
|
@@ -19,6 +19,10 @@ const expr = new Exprify();
|
|
|
19
19
|
|
|
20
20
|
console.log(expr.evaluate("5 + 7 * 2"));
|
|
21
21
|
// 19
|
|
22
|
+
|
|
23
|
+
expr.setVariable("x", 10);
|
|
24
|
+
console.log(expr.evaluate("x + 5"));
|
|
25
|
+
// 15
|
|
22
26
|
```
|
|
23
27
|
|
|
24
28
|
## Browser Usage
|
|
@@ -31,11 +35,18 @@ console.log(expr.evaluate("5 + 7 * 2"));
|
|
|
31
35
|
</script>
|
|
32
36
|
```
|
|
33
37
|
|
|
38
|
+
`unpkg` resolves to the browser bundle from `dist/exprify.min.js`.
|
|
39
|
+
|
|
34
40
|
## API
|
|
35
41
|
|
|
36
42
|
### `new Exprify()`
|
|
37
43
|
|
|
38
|
-
Creates a new evaluator instance with isolated
|
|
44
|
+
Creates a new evaluator instance with isolated state for:
|
|
45
|
+
|
|
46
|
+
- variables
|
|
47
|
+
- functions
|
|
48
|
+
- units
|
|
49
|
+
- compiled-expression cache
|
|
39
50
|
|
|
40
51
|
### `expr.evaluate(expression)`
|
|
41
52
|
|
|
@@ -48,7 +59,7 @@ expr.evaluate("10 + 5 * 2");
|
|
|
48
59
|
|
|
49
60
|
### `expr.parse(expression)`
|
|
50
61
|
|
|
51
|
-
Returns
|
|
62
|
+
Returns `{ tokens, ast }`.
|
|
52
63
|
|
|
53
64
|
```js
|
|
54
65
|
const parsed = expr.parse("2 inch to cm");
|
|
@@ -58,7 +69,7 @@ console.log(parsed.ast);
|
|
|
58
69
|
|
|
59
70
|
### `expr.compile(expression)`
|
|
60
71
|
|
|
61
|
-
Compiles an expression once and returns a reusable function.
|
|
72
|
+
Compiles an expression once and returns a reusable function.
|
|
62
73
|
|
|
63
74
|
```js
|
|
64
75
|
const area = expr.compile("width * height");
|
|
@@ -69,7 +80,7 @@ console.log(area({ width: 6, height: 4 }));
|
|
|
69
80
|
|
|
70
81
|
### `expr.setVariable(name, value)` / `expr.getVariable(name)`
|
|
71
82
|
|
|
72
|
-
Stores
|
|
83
|
+
Stores and reuses values across evaluations.
|
|
73
84
|
|
|
74
85
|
```js
|
|
75
86
|
expr.setVariable("x", 10);
|
|
@@ -81,7 +92,7 @@ console.log(expr.evaluate("x + y * 2"));
|
|
|
81
92
|
|
|
82
93
|
### `expr.addFunction(name, fn)`
|
|
83
94
|
|
|
84
|
-
Registers a custom function.
|
|
95
|
+
Registers a custom JavaScript function.
|
|
85
96
|
|
|
86
97
|
```js
|
|
87
98
|
expr.addFunction("double", (n) => n * 2);
|
|
@@ -90,9 +101,20 @@ console.log(expr.evaluate("double(5) + 3"));
|
|
|
90
101
|
// 13
|
|
91
102
|
```
|
|
92
103
|
|
|
93
|
-
|
|
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
|
|
94
116
|
|
|
95
|
-
### Arithmetic
|
|
117
|
+
### Arithmetic
|
|
96
118
|
|
|
97
119
|
```js
|
|
98
120
|
expr.evaluate("2 + 3 * 4");
|
|
@@ -105,7 +127,7 @@ expr.evaluate("11n ^ 2n");
|
|
|
105
127
|
// 121n
|
|
106
128
|
```
|
|
107
129
|
|
|
108
|
-
### Strings
|
|
130
|
+
### Strings, Booleans, Complex Numbers
|
|
109
131
|
|
|
110
132
|
```js
|
|
111
133
|
expr.evaluate('"Hello " + "World"');
|
|
@@ -113,24 +135,12 @@ expr.evaluate('"Hello " + "World"');
|
|
|
113
135
|
|
|
114
136
|
expr.evaluate("true && false");
|
|
115
137
|
// false
|
|
116
|
-
```
|
|
117
|
-
|
|
118
|
-
### Built-in functions
|
|
119
138
|
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
// 25
|
|
123
|
-
|
|
124
|
-
expr.evaluate("min(10, 25, 7)");
|
|
125
|
-
// 7
|
|
126
|
-
|
|
127
|
-
expr.evaluate("sqrt(81)");
|
|
128
|
-
// 9
|
|
139
|
+
expr.evaluate("9 / 3 + 2i");
|
|
140
|
+
// "3 + 2i"
|
|
129
141
|
```
|
|
130
142
|
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
### Unit conversion
|
|
143
|
+
### Unit Conversion
|
|
134
144
|
|
|
135
145
|
```js
|
|
136
146
|
expr.evaluate("2 inch to cm");
|
|
@@ -141,30 +151,52 @@ expr.evaluate("5 cm + 2 inch");
|
|
|
141
151
|
|
|
142
152
|
expr.evaluate("5cm + 0.2 m in inch");
|
|
143
153
|
// "9.84251968503937 inch"
|
|
154
|
+
|
|
155
|
+
expr.evaluate("a = 5.08 cm + 2 inch");
|
|
156
|
+
expr.evaluate("a to inch");
|
|
157
|
+
// "4 inch"
|
|
144
158
|
```
|
|
145
159
|
|
|
146
160
|
### Matrices
|
|
147
161
|
|
|
148
|
-
|
|
149
|
-
expr.evaluate("det([-1, 2; 3, 1])");
|
|
150
|
-
// -7
|
|
162
|
+
Exprify supports matrix literals with `;` as row separators.
|
|
151
163
|
|
|
164
|
+
```js
|
|
152
165
|
expr.evaluate("a = [1, 2, 3; 4, 5, 6]");
|
|
166
|
+
// {"exprify":"DenseMatrix","data":[[1,2,3],[4,5,6]],"size":[2,3]}
|
|
167
|
+
|
|
153
168
|
expr.evaluate("a[2, 3]");
|
|
154
169
|
// 6
|
|
155
170
|
|
|
156
171
|
expr.evaluate("a[1:2, 2]");
|
|
157
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
|
|
158
179
|
```
|
|
159
180
|
|
|
160
|
-
###
|
|
181
|
+
### Linear Algebra Helpers
|
|
161
182
|
|
|
162
183
|
```js
|
|
163
|
-
expr.evaluate("
|
|
164
|
-
// "
|
|
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]
|
|
165
195
|
```
|
|
166
196
|
|
|
167
|
-
|
|
197
|
+
Available helpers currently include `det`, `lsolve`, `lup`, `lyap`, `qr`, and `polynomialRoot`.
|
|
198
|
+
|
|
199
|
+
### Algebra Helpers
|
|
168
200
|
|
|
169
201
|
```js
|
|
170
202
|
expr.evaluate('simplify("2x + x")');
|
|
@@ -172,8 +204,43 @@ expr.evaluate('simplify("2x + x")');
|
|
|
172
204
|
|
|
173
205
|
expr.evaluate('derivative("2x^2 + 3x + 4", "x")');
|
|
174
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)"}
|
|
175
210
|
```
|
|
176
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
|
+
|
|
177
244
|
## Manual Build
|
|
178
245
|
|
|
179
246
|
```bash
|
|
@@ -183,7 +250,7 @@ npm install
|
|
|
183
250
|
npm run build
|
|
184
251
|
```
|
|
185
252
|
|
|
186
|
-
Build output is
|
|
253
|
+
Build output is written to `dist/`.
|
|
187
254
|
|
|
188
255
|
## Testing
|
|
189
256
|
|
|
@@ -200,4 +267,4 @@ Exprify is licensed under GPL-3.0. Copyright (c) [Nirmal Paul](https://github.co
|
|
|
200
267
|
1. Fork the repository.
|
|
201
268
|
2. Create a branch: `git checkout -b feature/your-feature`
|
|
202
269
|
3. Commit your changes: `git commit -m "Add your feature"`
|
|
203
|
-
4. Push
|
|
270
|
+
4. Push your branch and open a pull request.
|