Utility to evaluate BODMAS arithmetic formulas. It is an implementation of the shunting yard algorithm.
With yarn
:
yarn add @beyondessential/arithmetic
With npm
:
npm add @beyondessential/arithmetic
formulaText
must be in BODMAS format.
Usage example:
import { runArithmetic } from '@beyondessential/arithmetic';
const value = runArithmetic('(-1 + 2.5) / 3');
console.log(value); // 0.5
const valueWithVariable = runArithmetic('2 * four', {
four: 4,
});
console.log(valueWithVariable); // 8
Usage example:
import { getVariables } from '@beyondessential/arithmetic';
const variables = getVariables('(-a * b - 1) / (c + 3)');
console.log(variables); // ['a', 'b', 'c']
Note: All operators are case insensitive.
Operator | Example | Description |
---|---|---|
+ |
1 + 1 |
Addition |
- |
1 - 1 |
Subtraction |
* or x |
1 * 1 or 1 x 1 |
Multiplication |
/ |
1 / 1 |
Division |
() |
1 / (1 + 1) |
Brackets |
- |
-1 |
Unary minus |
max |
max(1, 2, 3) |
Takes the maximum value of it's arguments. -Infinity if given none |