parsemath
TypeScript icon, indicating that this package has built-in type declarations

0.5.1 • Public • Published

parsemath

Simple parser for arithmetic expressions.

  • Written in typescript.
  • No dependencies.
  • Designed to safely parse untrusted expression strings. Identifiers, operations, and functions can be restricted.
  • Supports the following operations: + (unary and binary), - (unary and binary), *, /, ^, **, and = (equality, not assignment).
  • Supports variable substitution (see below).
  • Whitespace insensitive.

Installation

npm install parsemath

Use

import {parse} from "parsemath" // or let {parse} = require("parsemath")

let value = parse("x^2 + x + 1", {x: 1e-1})
console.log(value) // 1.11

Some useful error messages:

parse("(x-y)(x+y)", { x: 1, y: 2 });
// Error: Unexpected '(' at position 5.

// (x-y)(x+y)
//      ▲
// ─────╯

Throws when it encounters an unspecified variable

parse("x^2 + y + 1" {x: 1e-1})
// Error: Unknown identifier 'y' at position 6.
//
// x^2 + y + 1
//       ▲
// ──────╯

Versions

Current Tags

  • Version
    Downloads (Last 7 Days)
    • Tag
  • 0.5.1
    0
    • latest

Version History

Package Sidebar

Install

npm i parsemath

Weekly Downloads

6

Version

0.5.1

License

ISC

Unpacked Size

39.2 kB

Total Files

21

Last publish

Collaborators

  • mgreenbe