# @casbin/expression-eval

> JavaScript expression parsing and evaluation.

Latest version **5.3.0** (published 2024-11-19) · MIT license · 0 weekly downloads

## Install

```sh
npm install @casbin/expression-eval
pnpm add @casbin/expression-eval
yarn add @casbin/expression-eval
bun add @casbin/expression-eval
```

## Health

**Score 50/100 (C)** — status: stable.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

Negative: stale.

## Facts

| | |
|---|---|
| Version | 5.3.0 |
| Published | 2024-11-19 |
| First published | 2024-10-24 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 1 |
| Unpacked size | 91.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 5 |
| Author | Node-Casbin |
| Maintainers | hsluoyz |
| Keywords | expression, eval, evaluation, parser, ast, abstract, syntax, tree, math |

## Links

- npm: https://www.npmjs.com/package/@casbin/expression-eval
- Repository: https://github.com/node-casbin/expression-eval
- Homepage: https://github.com/node-casbin/expression-eval#readme
- Issues: https://github.com/node-casbin/expression-eval/issues
- npm.io page: https://npm.io/package/@casbin/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.3.0 (latest) — 2024-11-19
- 5.2.0 — 2024-10-27
- 5.1.0 — 2024-10-24
- 5.0.2 — 2024-10-24

## README

# expression-eval

[![NPM version][npm-image]][npm-url]
[![NPM download][download-image]][download-url]
[![ci](https://github.com/node-casbin/expression-eval/actions/workflows/ci.yml/badge.svg)](https://github.com/node-casbin/expression-eval/actions/workflows/ci.yml)
[![Coverage Status](https://coveralls.io/repos/github/node-casbin/expression-eval/badge.svg?branch=master)](https://coveralls.io/github/node-casbin/expression-eval?branch=master)
[![Discord](https://img.shields.io/discord/1022748306096537660?logo=discord&label=discord&color=5865F2)](https://discord.gg/S5UjpzGZjN)

[npm-image]: https://img.shields.io/npm/v/@casbin/expression-eval.svg?style=flat-square
[npm-url]: https://npmjs.org/package/@casbin/expression-eval
[download-image]: https://img.shields.io/npm/dm/@casbin/expression-eval.svg?style=flat-square
[download-url]: https://npmjs.org/package/@casbin/expression-eval

JavaScript expression parsing and evaluation.

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

## Installation

Install:

```
npm install --save @casbin/expression-eval
```

Import:

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

## API

### Parsing

```javascript
import { parse } from '@casbin/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 '@casbin/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 '@casbin/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

Apache 2.0 License.

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