https://github.com/shepherdwind/simple-evaluate
A safe parse for simple js expression
https://github.com/shepherdwind/simple-evaluate
evaluate-expressions js-safe-eval
Last synced: about 1 month ago
JSON representation
A safe parse for simple js expression
- Host: GitHub
- URL: https://github.com/shepherdwind/simple-evaluate
- Owner: shepherdwind
- License: mit
- Created: 2017-10-04T10:34:30.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2022-10-19T10:19:38.000Z (over 2 years ago)
- Last Synced: 2025-04-15T21:08:01.403Z (about 1 month ago)
- Topics: evaluate-expressions, js-safe-eval
- Language: TypeScript
- Homepage: https://www.yuque.com/eward/bt8ny4/bgc0gx
- Size: 201 KB
- Stars: 61
- Watchers: 1
- Forks: 5
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: history.md
- License: LICENSE
Awesome Lists containing this project
README
# Simple Evaluate
[![NPM version][npm-image]][npm-url]
[![build status][travis-image]][travis-url]
[![Test coverage][coveralls-image]][coveralls-url]
[![npm download][download-image]][download-url][npm-image]: http://img.shields.io/npm/v/simple-evaluate.svg?style=flat-square
[npm-url]: http://npmjs.org/package/simple-evaluate
[download-image]: https://img.shields.io/npm/dm/simple-evaluate.svg?style=flat-square
[download-url]: https://npmjs.org/package/simple-evaluate
[travis-image]: https://img.shields.io/travis/shepherdwind/simple-evaluate.svg?style=flat-square
[travis-url]: https://travis-ci.org/shepherdwind/simple-evaluate
[coveralls-image]: https://img.shields.io/coveralls/shepherdwind/simple-evaluate.svg?style=flat-square
[coveralls-url]: https://coveralls.io/r/shepherdwind/simple-evaluate?branch=masterA safe parse for simple js expression.
### Usage
```js
import evaluate from 'simple-evaluate';
evaluate(null, '12 + 1 > 14');
evaluate({ a }, 'a + 1 > 14');
```### Support operation include
- Math operator `+ - * / %`
- ternary expression `a ? b : c`
- Comparison `> < >= <= == != === !==`
- Logical `&& || !`
- NegationYou can run those expression, for example:
```js
evaluate({}, '!a > 0');
evaluate({}, 'a > 0 || a < -12 || 12 + 2*(4 + 4) < 12');
evaluate({ a: 1 }, '-a * 2');
```### context find
`$.` stand for the root value all `context`, value path only support '.', and not support function call.
> since 1.2.0, `$.` is optional. That mean `$.a.b` is equal to `a.b`
### string and boolean
String and boolean support, string start with `' | "`, just the same as javascript expression.
Boolean use two key words, `true | false`.
### template string
You can use template string, just like the javascript syntax, for example.
```js
evaluate({ a: 22 }, "`I am ${ a >= 18 ? 'adult' : 'child' }`").should.equal('I am adult');
```### Operation no support
- Funcion call
So, you can not run those expression
```js
evaluation({}, 'a(1) > 0');
```