You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: CHANGELOG.md
+4Lines changed: 4 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,6 +2,10 @@
2
2
3
3
All notable changes to **antlr-calculator** are documented here.
4
4
5
+
## v2.1.0:
6
+
- The calculator now supports trailing comments in a formula, separated by a semicolon `;` at the end of the actual formula input. For example, `1 + 2; Hello World!` now just evaluates `1 + 2` and ignores everything after the semicolon `;`
7
+
- Add support for substitutions in formulas
8
+
5
9
## v2.0.4:
6
10
- The internal check for null or empty formulas was changed for better compatibility with Node
This calculator is using the [ANTLR4 TypeScript target](https://github.com/tunnelvisionlabs/antlr4ts)
@@ -17,6 +17,8 @@ Whenever a calculation is performed, a `CalculationResult` is returned with the
17
17
18
18
[You can check out the live demo here!](https://antlr-calculator.dangl.me)
19
19
20
+
You can find the .NET version here: https://github.com/GeorgDangl/Dangl.Calculator
21
+
20
22
## Installation
21
23
22
24
Clone this repository or just go with `npm install antlr-calculator`.
@@ -118,6 +120,36 @@ Comments in Formulas are supported by encapsulating them either in `/*...*/`, `'
118
120
119
121
`4"Length"*3"Width"` resolves to `12`
120
122
123
+
## Substitutions
124
+
125
+
The calculator can be called with an overload that accepts a callback function for substitution values. For example, take the following formula:
126
+
`1,2*#Z4+3`
127
+
Here, `#Z4` is a _substitution_, which is a placeholder that can be externally supplied. Let's say you want to resolve `#Z4` to the value three, you could make this simple call:
128
+
129
+
```typescript
130
+
const formula ="1,2*#Z4+3";
131
+
const result =Calculator.calculate(formula, substitution=>
132
+
{
133
+
if (substitution==="#Z4")
134
+
{
135
+
return3;
136
+
}
137
+
138
+
returnnull;
139
+
});
140
+
```
141
+
142
+
The callback is in the form of a `(substitution: string) => number`, and it will be called for every substitution found in the formula. Multiple substitutions are supported. If duplicates in substitutions are present, the calculator will request each one individually. If a substitution resolves to `null`, the formula is considered invalid.
143
+
144
+
Substitutions must always start with the `#` character and can then have the following characters: `[a-z] | [A-Z] | [äÄöÖüÜ] | [0-9]`
145
+
146
+
## Trailing comments
147
+
148
+
Formulas may be terminated with a semicolon `;` at the end, followed by extra input that is not evaluated. This is useful when, instead of regular comments, you
149
+
just want to attach some trailing formation at the end of a formula. For example, the following formula:
150
+
`1 + 3; As per our counting`
151
+
Would just evaluate the `1 + 3` portion and return a valid result with the value `4`, ignoring the trailing semicolon and all input that follows.
0 commit comments