# expression-eval

> JavaScript expression parsing and evaluation.

Latest version **5.0.1** (published 2023-06-19) · MIT license · 0 weekly downloads

> **Deprecated.** This package is deprecated.

## Install

```sh
npm install expression-eval
pnpm add expression-eval
yarn add expression-eval
bun add expression-eval
```

## Health

**Score 10/100 (F)** — status: deprecated.

Negative: deprecated.

## Facts

| | |
|---|---|
| Version | 5.0.1 |
| Published | 2023-06-19 |
| First published | 2017-04-13 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 1 |
| Unpacked size | 76.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 200 |
| Author | Don McCurdy |
| Maintainers | donmccurdy |
| Keywords | expression, eval, evaluation, parser, ast, abstract, syntax, tree, math |

## Links

- npm: https://www.npmjs.com/package/expression-eval
- Repository: https://github.com/donmccurdy/expression-eval
- Homepage: https://github.com/donmccurdy/expression-eval#readme
- Issues: https://github.com/donmccurdy/expression-eval/issues
- npm.io page: https://npm.io/package/expression-eval

## Dependencies (1)

- [jsep](https://npm.io/package/jsep.md) ^0.3.0

## Alternatives

- [babylon](https://npm.io/package/babylon.md) — 5.1M weekly downloads
- [csscolorparser](https://npm.io/package/csscolorparser.md) — 3.7M weekly downloads
- [expr-eval-fork](https://npm.io/package/expr-eval-fork.md) — 1.5M weekly downloads
- [@leeoniya/ufuzzy](https://npm.io/package/@leeoniya/ufuzzy.md) — 247.7K weekly downloads
- [xml-parser](https://npm.io/package/xml-parser.md) — 78.4K weekly downloads

## Recent versions

- 5.0.1 (latest) — 2023-06-19
- 5.0.0 — 2021-06-30
- 4.0.0 — 2021-01-06
- 3.1.2 — 2020-07-16
- 3.1.1 — 2020-04-26
- 3.1.0 — 2020-04-04
- 3.0.4 — 2020-03-29
- 3.0.3 — 2020-03-29
- 3.0.2 — 2020-03-29
- 3.0.1 — 2020-03-29
- 3.0.0 — 2020-03-29
- 2.1.0 — 2019-12-31
- 2.0.0 — 2019-02-23
- 1.4.0 — 2018-09-22
- 1.3.1 — 2018-09-02
- … 7 more at https://npm.io/package/expression-eval/versions

## README

# expression-eval

[![Latest NPM release](https://img.shields.io/npm/v/expression-eval.svg)](https://www.npmjs.com/package/expression-eval)
[![Minzipped size](https://badgen.net/bundlephobia/minzip/expression-eval)](https://bundlephobia.com/result?p=expression-eval)
[![License](https://img.shields.io/badge/license-MIT-007ec6.svg)](https://github.com/donmccurdy/expression-eval/blob/master/LICENSE)
[![CI](https://github.com/donmccurdy/expression-eval/workflows/CI/badge.svg?branch=master&event=push)](https://github.com/donmccurdy/expression-eval/actions?query=workflow%3ACI)

JavaScript expression parsing and evaluation.

> ⚠️ **UNMAINTAINED:** The `expression-eval` npm package is no longer maintained. The package was originally published as part of a now-completed personal project, and I do not have incentives to continue maintenance. Please feel free to use the code, but be aware that support and updates will not be available.

> ⚠️ **SECURITY NOTICE:** As mentioned under [Security](#security) below, this library does not attempt to provide a secure sandbox for evaluation. Evaluation involving user inputs (expressions or values) may lead to unsafe behavior. If your project requires a secure sandbox, consider alternatives such as [vm2](https://www.npmjs.com/package/vm2).

Powered by [jsep](https://github.com/soney/jsep).

## Installation

Install:

```
npm install --save expression-eval
```

Import:

```js
// ES6
import { parse, eval } from 'expression-eval';
// CommonJS
const { parse, eval } = require('expression-eval');
// UMD / standalone script
const { parse, eval } = window.expressionEval;
```

## API

### Parsing

```javascript
import { parse } from 'expression-eval';
const ast = parse('1 + foo');
```

The result of the parse is an AST (abstract syntax tree), like:

```json
{
  "type": "BinaryExpression",
  "operator": "+",
  "left": {
    "type": "Literal",
    "value": 1,
    "raw": "1"
  },
  "right": {
    "type": "Identifier",
    "name": "foo"
  }
}
```

### Evaluation

```javascript
import { parse, eval } from 'expression-eval';
const ast = parse('a + b / c'); // abstract syntax tree (AST)
const value = eval(ast, {a: 2, b: 2, c: 5}); // 2.4
```

Alternatively, use `evalAsync` for asynchronous evaluation.

### Compilation

```javascript
import { compile } from 'expression-eval';
const fn = compile('foo.bar + 10');
fn({foo: {bar: 'baz'}}); // 'baz10'
```

Alternatively, use `compileAsync` for asynchronous compilation.

## Security

Although this package does [avoid the use of `eval()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/eval#Do_not_ever_use_eval!), it _cannot guarantee that user-provided expressions, or user-provided inputs to evaluation, will not modify the state or behavior of your application_. This library does not attempt to provide a secure sandbox for evaluation. Evaluation of arbitrary user inputs (expressions or values) may lead to unsafe behavior. If your project requires a secure sandbox, consider alternatives such as [vm2](https://www.npmjs.com/package/vm2).

## License

MIT License.

---
_Source: https://npm.io/package/expression-eval · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
