2.1.1 • Published 5 years ago
@beyondessential/arithmetic v2.1.1
@beyondessential/arithmetic
Utility to evaluate BODMAS arithmetic formulas. It is an implementation of the shunting yard algorithm.
Installation
With yarn:
yarn add @beyondessential/arithmeticWith npm:
npm add @beyondessential/arithmeticAPI
formulaText must be in BODMAS format.
runArithmetic(formulaText: string, values?: Record<string, string | number>): number
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); // 8getVariables(formulaText: string): string[]
Usage example:
import { getVariables } from '@beyondessential/arithmetic';
const variables = getVariables('(-a * b - 1) / (c + 3)');
console.log(variables); // ['a', 'b', 'c']formulaText operators
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