javascript-calc-interpreter
TypeScript icon, indicating that this package has built-in type declarations

2.2.0 • Public • Published

Javascript Calc Interpreter

pipeline-badge pipeline-badge

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)

Readme

Keywords

none

Package Sidebar

Install

npm i javascript-calc-interpreter

Weekly Downloads

36

Version

2.2.0

License

ISC

Unpacked Size

167 kB

Total Files

49

Last publish

Collaborators

  • mcslinplay