math-expression-parser
v1.0.1
Published
A lightweight math expression parser and evaluator.
Maintainers
Readme
math-expression-parser
A lightweight TypeScript math expression parser and evaluator.
Install
npm install math-expression-parserUsage
import {
calculateMathExpression,
canEvaluateMathExpression,
fixStringForCalculation,
} from "math-expression-parser";
const raw = "2 + 3 * sin(30)";
const safe = fixStringForCalculation(raw);
if (canEvaluateMathExpression(safe)) {
const result = calculateMathExpression(safe);
console.log(result); // 3.5
}Supported operators
+,-,*,/,^- Parentheses:
(,)
Supported constants
pi,PIe,Ephitauinf,Inf
Supported functions
Unary:
sin,cos,tanasin,acos,atansinh,cosh,tanhlog(base 10),ln,log2sqrt,cbrtabs,ceil,floor,round,signexp,deg,rad
Binary:
atan2(a, b)log_(base, value)min(a, b),max(a, b)pow(a, b)mod(a, b)hypot(a, b)
Development
npm install
npm test
npm run buildLicense
GPL-3.0-only. See LICENSE.
