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/README.md CHANGED
@@ -1,135 +1,270 @@
1
- # Exprify — Math Expression Parser & Evaluator
2
-
3
- [![Exprify Social Banner](https://raw.githubusercontent.com/code-hemu/Exprify/refs/heads/main/src/assets/capture.jpg)](https://github.com/code-hemu/Exprify)
4
-
5
- Exprify is a JavaScript expression parser and evaluator supporting math operations, variables, and custom functions.
6
-
7
- ## 🔧 Manual Build
8
- 1. Clone the repository:
9
- ```bash
10
- git clone https://github.com/code-hemu/Exprify.git
11
- cd Exprify
12
-
13
- 2. Install dependencies and build:
14
- ```bash
15
- npm install
16
- npm run build
17
-
18
- The output will be generated in a dist/ folder.
19
-
20
- ## 🚀 Quick Start
21
- ### Node.js / ES Modules
22
- ```Javascript
23
- import Exprify from "exprify";
24
- const expr = new Exprify();
25
- console.log(expr.evaluate("5 + 7 * 2"));
26
- // → 19
27
- ```
28
- ### Browser (UMD)
29
- ```html
30
- <script src="exprify.js"></script>
31
- <script>
32
- const expr = new Exprify();
33
- console.log(expr.evaluate("10 + 5 * 2"));
34
- </script>
35
- ```
36
- ## 🧠 Examples
37
- ### ➕ Basic Math
38
- ```Javascript
39
- expr.evaluate("10 + 5 * 2");
40
- // → 20
41
- ```
42
- ### 🧮 Parentheses
43
- ```Javascript
44
- expr.evaluate("(10 + 5) * 2");
45
- // → 30
46
- ```
47
- ### 🔢 Variables
48
- ```Javascript
49
- expr.setVariable("x", 10);
50
- expr.setVariable("y", 5);
51
-
52
- expr.evaluate("x + y * 2");
53
- // 20
54
- ```
55
- ### 🔧 Custom Functions
56
- ```Javascript
57
- expr.addFunction("double", (x) => x * 2);
58
-
59
- expr.evaluate("#double(5) + 3");
60
- // → 13
61
- ```
62
-
63
- ### 📊 Built-in Functions
64
- ```Javascript
65
- expr.evaluate("#max(10, 25, 7)");
66
- // → 25
67
-
68
- expr.evaluate("#min(10, 25, 7)");
69
- // → 7
70
- ```
71
- ## 🤿 Other Examples
72
- ```Javascript
73
- //Create a new Exprify object with Exprify class
74
- const expr = new Exprify();
75
-
76
- //Simple Expression evaluation
77
- console.log(expr.evaluate(`25+5*2`)); // → 35
78
-
79
- //Nested expression evaluation
80
- console.log(expr.evaluate(`((52/8+2)+56*((25/2)*4+(8-2)))*2`)); // → 6289
81
-
82
- //BigInt Expression evaluation
83
- console.log(expr.evaluate(`11n ^2n`)); // 121n
84
-
85
- //String concatenation: '+' Operator behaves as concatenation operator
86
- console.log(expr.evaluate(`"Hello " + "World"`)); // → "Hello World"
87
-
88
- //Invalid Expression: One operand is a string and another one is number
89
- console.log(expr.evaluate(`"45" + 5`)); // → datatype error
90
-
91
- //Invalid Expression: One operand is a number and another one is boolean
92
- console.log(expr.evaluate(`45 * true`)); // → datatype error
93
-
94
- //Invalid Expression: unclosed quoted text
95
- console.log(expr.evaluate(`"Hello World `)); // → unclosed error
96
- ```
97
-
98
- ## 🧩 Built-in Functions
99
- Exprify has some built-in functions, here is a complete list
100
- | Function | Details | Example |
101
- | - | - |- |
102
- |**#max(...)**| #max() function returns the largest number of the provided numerical arguments | ```"#max(45,50,20, 4+9*(6+4))" //returns 94 ```|
103
- |**#min(...)**| #min() function returns the smallest number of the provided numerical arguments | ```"#min(45,50,20, 4+9*(6+4))" //returns 20 ```|
104
- |**#and(...)** <br> or <br> **#&&**| **#and()** or **#&&** tests each of its arguments , if all are true then it will return true | ```"#and(true , true, false)" //returns false ``` <br> <br> ```"#&&(true , true, false)" //returns false ```|
105
- |**#or(...)** <br> or <br> **#\|\|** | **#or()** or **#\|\|()** tests each of its arguments , if any of its arguments is true then it will return true | ```"#or(true , true, false)" //returns true ``` <br> <br> ```"#\|\|(true , true, false)" //returns true```|
106
- |**#not(x)** <br> or <br> **#!**| **#not()** or **#!()** changes 'true' value to a 'false' value and 'false' value to a 'true' value | ```"#not(true)" //returns false ``` <br> <br> ```"#!(true)" //returns false ``` |
107
- |**#greaterThan(...)** <br> or <br> **#>**| **#greaterThan()** or **#>()** takes 2 parameters and compare if that the 1st parameter is greater than the second parameter or not | ```"#greaterThan(67 , 5)" //returns true ``` <br> <br>```"#>(67 , 5)" //returns true ``` |
108
- |**#lessThan(...)** <br> or <br> **#<**| **#lessThan()** or **#<()** takes 2 parameters and compare if that the 1st parameter is less than the second parameter or not | ```"#lessThan(67 , 5)" //returns false ``` <br> <br>```"#<(67 , 5)" //returns false ``` |
109
- |**#isEqual(...)** <br> or <br> **#==**| **#isEqual()** or **#==()** takes 2 parameters and compare if that the both parameter is same numerical value or not | ```"#isEqual(60 , 50+10)" //returns true ``` <br> <br>```"#==(60 , 50+10)" //returns true ``` |
110
- |**#if(...)**| **#if()** takes 3 parameters. 1st parameter is a condition parameter, if the condition is true then it returns 2nd parameter otherwise it returns 3rd parameter (if the 3rd parameter is not specified then its default value false will be return) | ```"#if(true , 5, 80)" //returns 80 ``` <br> <br> ```"#if(#<(50,100) , '50 is less than 100', '100 is less than 50')" //returns '50 is less than 100' ```|
111
-
112
-
113
- ## 📜 License
114
- Exprify is freely distributable under the terms of the GPL-3.0 License. Copyright (c) [Nirmal Paul](https://github.com/nirmalpaul383/) (N Paul).
115
-
116
-
117
- ## 🤝 Contributing
118
-
119
- Contributions are welcome!
120
-
121
- 1. Fork the repo
122
-
123
- 2. Create your branch:
124
- ```bash
125
- git checkout -b feature/your-feature
126
-
127
- 3. Commit changes:
128
- ```bash
129
- git commit -m "Add your feature"
130
-
131
- 4. Push and open a PR 🚀
132
-
133
- ## Support
134
- If you like [this project](https://github.com/code-hemu/Exprify), give it a ⭐ on GitHub! This project is originally made by [Nirmal Paul](https://github.com/nirmalpaul383/) (N Paul) and replicate by [ViewPoint](https://github.com/nirmalpaul383/ViewPoint). The Main Developer's (N Paul) [youtube page](https://www.youtube.com/channel/UCY6JY8bTlR7hZEvhy6Pldxg/), [facebook page](https://facebook.com/a.new.way.Technical/).
135
-
1
+ # Exprify
2
+
3
+ [![Exprify Social Banner](https://raw.githubusercontent.com/code-hemu/Exprify/refs/heads/main/src/assets/capture.jpg)](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.