Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/korbinzhao/aexpr
A safe javascript expression interpreter, support operators / context accessing / property accessing / lodash functions.
https://github.com/korbinzhao/aexpr
expression expression-interpreter expressionengine interpreter javascript nodejs
Last synced: about 1 month ago
JSON representation
A safe javascript expression interpreter, support operators / context accessing / property accessing / lodash functions.
- Host: GitHub
- URL: https://github.com/korbinzhao/aexpr
- Owner: korbinzhao
- Created: 2022-05-13T08:56:24.000Z (over 2 years ago)
- Default Branch: master
- Last Pushed: 2022-10-28T09:47:17.000Z (about 2 years ago)
- Last Synced: 2024-10-28T12:16:47.671Z (about 2 months ago)
- Topics: expression, expression-interpreter, expressionengine, interpreter, javascript, nodejs
- Language: JavaScript
- Homepage:
- Size: 51.8 KB
- Stars: 20
- Watchers: 3
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: readme.md
Awesome Lists containing this project
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