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/README.md CHANGED
@@ -1,135 +1,203 @@
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 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.