0.5.1 • Published 4 years ago

parsemath v0.5.1

Weekly downloads
22
License
ISC
Repository
github
Last release
4 years ago

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
//       ▲
// ──────╯
0.5.1

4 years ago

0.5.0

4 years ago

0.4.0

4 years ago

0.3.0

4 years ago

0.2.0

4 years ago

0.1.0

4 years ago

1.0.0

4 years ago