Skip to content

Latest commit

 

History

History
60 lines (42 loc) · 1.44 KB

README.md

File metadata and controls

60 lines (42 loc) · 1.44 KB

@beyondessential/arithmetic

Utility to evaluate BODMAS arithmetic formulas. It is an implementation of the shunting yard algorithm.

Installation

With yarn:

yarn add @beyondessential/arithmetic

With npm:

npm add @beyondessential/arithmetic

API

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); // 8

getVariables(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