@math-x-ts/expressions 0.0.0-stage → 0.1.0

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 ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2022-2026 dopey2
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,146 @@
1
- # Temporary Holding Version
1
+ # @math-x-ts/expressions
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ <p>
4
+ <a href="https://www.npmjs.com/package/@math-x-ts/expressions"><img src="https://img.shields.io/npm/v/@math-x-ts/expressions.svg?&color=default" alt="npm version"></a>
5
+ <a href="https://opensource.org/licenses/MIT"><img src="https://img.shields.io/badge/License-MIT-blue.svg" alt="MIT license"></a>
6
+ </p>
7
+
8
+ Parse mathematical expressions, evaluate them, and get a **step by step solution** that shows every intermediate step, the way a student would write it.
9
+
10
+ ## Install
11
+
12
+ ```bash
13
+ npm install @math-x-ts/expressions
14
+ ```
15
+
16
+ The package ships both ES modules and CommonJS, with TypeScript types. It targets ES2020.
17
+
18
+ ## Usage
19
+
20
+ ### Evaluate an expression
21
+
22
+ ```ts
23
+ import { evaluate } from '@math-x-ts/expressions';
24
+
25
+ console.log(evaluate('4 * (1 + 2)')); // 12
26
+ ```
27
+
28
+ ### Get a step by step solution
29
+
30
+ ```ts
31
+ import { parse } from '@math-x-ts/expressions';
32
+
33
+ const mathNode = parse('4 * (1 + 2)');
34
+ const steps = mathNode.solveAll();
35
+
36
+ console.log(steps[0].toString()); // '4 * (1 + 2)'
37
+ console.log(steps[1].toString()); // '4 * 3'
38
+ console.log(steps[2].toString()); // '12'
39
+ ```
40
+
41
+ ### Fractions
42
+
43
+ Fractions are solved step by step: first a common denominator, then the addition, then the result is reduced to lowest terms, with the division shown as its own step.
44
+
45
+ ```ts
46
+ import { parse } from '@math-x-ts/expressions';
47
+
48
+ const mathNode = parse('2 / 6 + 2 / 2');
49
+ const steps = mathNode.solveAll();
50
+
51
+ console.log(steps[0].toString()); // '{2} / {6} + {2} / {2}'
52
+ console.log(steps[1].toString()); // '{2} / {6} + {2 * 3} / {2 * 3}'
53
+ console.log(steps[2].toString()); // '{2} / {6} + {6} / {6}'
54
+ console.log(steps[3].toString()); // '{2 + 6} / {6}'
55
+ console.log(steps[4].toString()); // '{8} / {6}'
56
+ console.log(steps[5].toString()); // '{8 : 2} / {6 : 2}'
57
+ console.log(steps[6].toString()); // '{4} / {3}'
58
+ ```
59
+
60
+ A fraction result has no single number value, so `evaluate` returns `NaN` for it. Use `solve` to get the final step instead:
61
+
62
+ ```ts
63
+ import { solve, evaluate } from '@math-x-ts/expressions';
64
+
65
+ const result = solve('2 / 6 + 2 / 2');
66
+
67
+ console.log(result.toString()); // '{4} / {3}'
68
+ console.log(evaluate('2 / 6 + 2 / 2')); // NaN
69
+ ```
70
+
71
+ ### LaTeX output
72
+
73
+ Every step can be rendered as LaTeX, e.g. with KaTeX or MathJax:
74
+
75
+ ```ts
76
+ import { parse } from '@math-x-ts/expressions';
77
+
78
+ const mathNode = parse('4 * (1 + 2)');
79
+ const steps = mathNode.solveAll();
80
+
81
+ console.log(steps[0].toTex()); // '4 \times (1 + 2)'
82
+ console.log(steps[1].toTex()); // '4 \times 3'
83
+ console.log(steps[2].toTex()); // '12'
84
+ ```
85
+
86
+ ## Syntax
87
+
88
+ | Symbol | Meaning | Example | Result |
89
+ |-------------|------------------------------------------|----------------------|----------------------|
90
+ | `+` `-` | Addition, subtraction | `4 - 6` | `-2` |
91
+ | `*` | Multiplication | `4 * 3` | `12` |
92
+ | `:` | Division, gives a number | `10 : 4` | `2.5` |
93
+ | `/` | Fraction, reduced to lowest terms | `10 / 4` | `{5} / {2}` |
94
+ | `^` | Exponent | `2 ^ 3 + 1` | `9` |
95
+ | `-` | Negative | `-(3 + 2) * 2` | `-10` |
96
+ | `( )` | Parentheses | `4 * (1 + 2)` | `12` |
97
+ | `{ }` | Priority like a parenthesis, not rendered | `2 ^ {1 + 2}`<br>`{5 + 10} / {2 + 2}` | `8`<br>`{15} / {4}` |
98
+
99
+ Variables (`x`) and equations (`=`) can be parsed and printed, but not solved yet.
100
+
101
+ ## API
102
+
103
+ ### Functions
104
+
105
+ | Function | Returns | Description |
106
+ |------------------------|------------|----------------------------------------------------------|
107
+ | `parse(expression)` | `MathNode` | Parse an expression into a tree of math nodes |
108
+ | `solve(expression)` | `MathNode` | The last step of the solution |
109
+ | `evaluate(expression)` | `number` | The numeric result, or `NaN` when the result isn't a number (e.g. a fraction) |
110
+
111
+ ### `MathNode` methods
112
+
113
+ | Method | Returns | Description |
114
+ |-----------------------|--------------|-------------------------------------------------|
115
+ | `solveAll()` | `MathNode[]` | Every step, from the expression to the result |
116
+ | `solveAllToString()` | `string[]` | Every step as a string |
117
+ | `solveAllToTex()` | `string[]` | Every step as LaTeX |
118
+ | `solveAllToJson()` | `object[]` | Every step as JSON |
119
+ | `next()` | `MathNode` | The next step only |
120
+ | `solve()` | `MathNode` | The last step |
121
+ | `evaluate()` | `number` | The numeric result, or `NaN` |
122
+ | `toString()` | `string` | The expression as a string |
123
+ | `toTex()` | `string` | The expression as LaTeX |
124
+ | `toJson()` | `object` | The expression tree as JSON |
125
+ | `isEqual(node)` | `boolean` | Whether two trees are the same |
126
+ | `isAtomic` | `boolean` | `true` when the node can't be simplified further |
127
+
128
+ The node classes are exported too, to build or inspect trees directly: `Add`, `Constant`, `Divide`, `Equal`, `Exponent`, `Fraction`, `Multiply`, `Negative`, `Parenthesis`, `Subtract`, `Variable`, plus `MathNodeType`.
129
+
130
+ ```ts
131
+ import { Add, Constant } from '@math-x-ts/expressions';
132
+
133
+ const mathNode = new Add(new Constant(1), new Constant(2));
134
+
135
+ console.log(mathNode.toString()); // '1 + 2'
136
+ console.log(mathNode.toJson());
137
+ // {
138
+ // type: 'Add',
139
+ // left: { type: 'Constant', value: 1 },
140
+ // right: { type: 'Constant', value: 2 }
141
+ // }
142
+ ```
143
+
144
+ ## License
145
+
146
+ MIT