codeflows / ParsaJS

Library to parse and evaluate arithmetic expressions without eval()

Home Page:https://parsajs-demo.netlify.app/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Library to parse and evaluate arithmetic expressions without eval()

Supports basic operators [ ** * / % + - ( ) ] and variables.

Order of operations: BEMDAS

Usage

npm i @jaakkohurtta/parsajs
import Parsa from "@jaakkohurtta/parsajs"
const myParser = new Parsa()

let parse = await myParser.parse('a*(20/8)*b,{"a":3,"b":2}')
let evaluation = await myParser.evaluateAll(parse.items)

console.log(evaluation)
	
// Log:
//	{
//	  complete: true,
//	  eval: 15, 
//	  items: [{ "15", "number" }]
//	  blocks: [{ id: 1, prio: 0, startIndex: 0, endIndex: 0 }],
//	}

Methods

parse(string)

Parses an input string for evaluation

Params: string
Returns a promise: 
	{ 
	  msg: string, 
	  items: [{ value, type}, ...],
  	  blocks: [{ id, prio, startIndex, endIndex }, ...]
	}

evaluateAll(items)

Evaluates the expression string

Params: array of expression items
Returns a promise: 
	{ 
	  blocks: [{ ... }], 
	  complete: boolean (true), 
	  eval: number, 
	  items: [{ ... }] 
	}

evaluateNext(items)

Evaluates the next block according to order of operations

Params: array of expression items
Returns a promise: 
	{ 
	  blocks: [{ ... }], 
	  complete: boolean (false), 
	  eval: number, 
	  items: [{ ... }] 
	}

getSourceString()

Returns the original input string

getSourceItems()

Returns an array of parsed expression items

License: MIT

About

Library to parse and evaluate arithmetic expressions without eval()

https://parsajs-demo.netlify.app/


Languages

Language:JavaScript 100.0%