Javascript Calc Interpreter
Util to parse expressions in string form and return the evaluated result. This util is case-insensitive.
How to Install
npm install javascript-calc-interpreter --save
Usage
const {Expression} = require('javascript-calc-interpreter')
Expression.evaluate("2+2")
Get Help
import {Expression} from "./Expression";
Expression.getHelp()
This returns an array with multiple objects that all have a command and description field.
Configuration
Setting the maximum amount of iteration before the parsing is cancelled
import {Expression} from "./Expression"
Expression.maxIterations = 30
Adding your own functions
You can add your own functions and constants.
Functions:
You can choose the amount of arguments the function can take.
If you set selfProcessing
to false
the input will be run through the interpreter before it is passed to the function, if not you will get the raw input.
import {Expression} from "./Expression";
Expression.addFunction({
name: "name",
selfProcessing: false,
function: (a, b) => a + b
})
Additional configurations for your custom functions
Set takesArray
to true
if your function can take a variable amount of arguments
If you set description
your function will be discoverable by the getHelp()
method
It takes an array of objects that have the format
{
command: "function(arguments)",
description: "Describe what your function does"
}
If you want to add a constant you can do it like this
import {Expression} from "./Expression";
Expression.addConstant("pi", Math.PI)