# aexpr

> Aexpr is safe javascript expression interpreter, support operators / context accessing / property accessing / lodash functions.

Latest version **1.0.2** (published 2022-10-28) · MIT license · 0 weekly downloads

## Install

```sh
npm install aexpr
pnpm add aexpr
yarn add aexpr
bun add aexpr
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.2 |
| Published | 2022-10-28 |
| First published | 2022-05-16 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 40.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 22 |
| Maintainers | korbinzhao |
| Keywords | expression, interpret, interpreter, expression-engine, expression-interpreter, lodash, compiler, operators, context, property |

## Links

- npm: https://www.npmjs.com/package/aexpr
- Repository: https://github.com/korbinzhao/aexpr
- Homepage: https://github.com/korbinzhao/aexpr#readme
- Issues: https://github.com/korbinzhao/aexpr/issues
- npm.io page: https://npm.io/package/aexpr

## Dependencies (1)

- [lodash](https://npm.io/package/lodash.md) ^4.17.21

## Alternatives

- [@openai/codex-sdk](https://npm.io/package/@openai/codex-sdk.md) — 731.4K weekly downloads
- [babel-plugin-transform-react-jsx](https://npm.io/package/babel-plugin-transform-react-jsx.md) — 565.0K weekly downloads
- [babel-helper-remove-or-void](https://npm.io/package/babel-helper-remove-or-void.md) — 508.5K weekly downloads
- [@pnpm/store-controller-types](https://npm.io/package/@pnpm/store-controller-types.md) — 186.9K weekly downloads
- [react-native-signature-canvas](https://npm.io/package/react-native-signature-canvas.md) — 155.6K weekly downloads

## Recent versions

- 1.0.2 (latest) — 2022-10-28
- 1.0.1 — 2022-05-17
- 1.0.0 — 2022-05-16

## README

# Aexpr

Aexpr is safe javascript expression interpreter, support operators / context accessing / property accessing / lodash functions.

# Support
* Data type: number/boolean/string/object/array
* Operators: 
    * Mathematical operators: + - * / 
    * Logic operators: && || > >= < <= === !== 
    * Tenary operator: a ? b : c
* Context access
* Property access
* Functions: 
    * Support all [Lodash](https://lodash.com/docs/4.17.15) functions, but prohibit function type input params to avoid prototype access

# Advantage
* Security assurance
    * Sandbox isolation to avoid prototype accessing using AST
    * Avoid memory leak by prohibit statements like 'while'
* [Lodash](https://lodash.com/docs/4.17.15) functions support

# Usage
```
const interpret = require('aexpr');

// invoke the function
// interpret(codeStr: string, context?: object);

// calculate
interpret('1 + 2 / 4 * 6');
interpret('1 + 2 / (4 * 6)');

// compare
interpret('1 > 2');
interpret('1 < 2');
interpret('1 <= 2');
interpret('2 <= 2');
interpret('2 === 2');

// logic
interpret('1 && 2');
interpret('1 || 0');

// context accessing && property accessing
interpret('a + b / c.d.e', { a: 2, b: 3, c: { d: { e: 5 } } });

// lodash functions
interpret(`_.has(obj, 'a.b')`, { obj: { a: { b: 1, c: 2 } } });
interpret('_.indexOf(arr, 1)', { arr: [1, 2, 3, 4] });

const arr = [
    { 'user': 'barney', 'active': false },
    { 'user': 'fred', 'active': false },
    { 'user': 'pebbles', 'active': true }
];

// not support function type input params to avoid prototype access
expect(interpret.bind(null, `_.findIndex(users, function(o) { return o.user == 'barney'; })`)).toThrow('Parse error');
// support ordinary input params
expect(interpret(`_.findIndex(users, { 'user': 'fred', 'active': false })`, {users: arr})).toBe(1);

```

# license
MIT

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