Skip to content

Commit

Permalink
update readme
Browse files Browse the repository at this point in the history
  • Loading branch information
regevbr committed Aug 11, 2019
1 parent 49edead commit 8086845
Showing 1 changed file with 56 additions and 52 deletions.
108 changes: 56 additions & 52 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,67 @@
[![devDependencies Status](https://david-dm.org/regevbr/json-expression-eval/dev-status.svg)](https://david-dm.org/regevbr/json-expression-eval?type=dev)

# json-expression-eval
A Node.js module that evaluates a json described boolean expression using dynamic functions
A Node.js module that evaluates a json described boolean expressions using dynamic functions and a given context.

The module is strictly typed, ensuring that passed expressions are 100% valid at compile time.

This module is especially useful if you need to serialize complex expressions (to be saved in DB for example)

## Installation
```sh
npm install json-expression-eval --save
npm install json-expression-eval
```
Or
```sh
yarn add json-expression-eval
```

## Usage
*Please see tests for more usages and example dir for extended and opinionated usage. (under /src)*
### API
This module enables you to evaluate boolean expressions which are described using a JSON structure and can utilize user defined 'functions'.
There are 4 types op operators you can use (evaluated in that order of precedence):

*Please see tests and examples dir for more usages and example (under /src)*

```typescript
import { Expression, evaluate } from 'json-expression-eval';

interface IExampleContext {
userId: string;
times: number;
}

const exampleContext: IExampleContext = {
userId: '[email protected]',
times: 5,
};

const functionsTable = {
countRange: ([min, max]: [number, number], context: IExampleContext): boolean => {
return context.times >= min && context.times < max;
},
};

const expression: Expression<IExampleContext, typeof functionsTable> = {
or: [
{
userId: '[email protected]',
},
{
and: [
{
countRange: [2, 6],
},
{
userId: '[email protected]',
},
],
},
],
};
console.log(evaluate(expression, exampleContext, functionsTable)); // true
```

### Expression

There are 4 types of operators you can use (evaluated in that order of precedence):
- `and` - accepts a non empty list of operators
- `or` - accepts a non empty list of operators
- `not` - accepts another operator
Expand Down Expand Up @@ -55,49 +105,3 @@ Example expressions, assuming we have the `user` and `maxCount` user defined fun
]
}
```

### TypeScript
```typescript
import {evaluate} from 'json-expression-eval';

const functionsTable: FunctionsTable = {
user: (user: string, context: { userId: string }): boolean => {
return context.userId === user;
},
maxCount: (maxCount: number, context: { times: number }): boolean => {
return context.times < maxCount;
}
};
const result = evaluate(
{
or: [
{
user: '[email protected]'
},
{
not: {
and: [
{
maxCount: 5
},
{
user: '[email protected]'
}
]
}
}
]
},
{
userId: '[email protected]',
times: 1
},
functionsTable);
```
```sh
Output should be 'false'
```
## Test
```sh
npm run test
```

0 comments on commit 8086845

Please sign in to comment.