Skip to content

Commit

Permalink
fix readme
Browse files Browse the repository at this point in the history
  • Loading branch information
regevbr committed Jun 9, 2018
1 parent ca69d87 commit 1fcd919
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 5 deletions.
50 changes: 45 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,62 @@ npm install json-expression-eval --save
yarn add json-expression-eval
```
## Usage
- Please see example dir for extended usage and opinionated usage
### 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:
- and - accepts a non empty list of operators
- or - accepts a non empty list of operators
- not - accepts another operator
- \<user defined funcs\> - accepts any type of argument and evaluated by the user defined functions and given context.

Example expressions, assuming we have the user and maxCount user defined functions in place can be:
```json
{
"or":[
{
"not":{
"user":"[email protected]"
}
},
{
"maxCount":1
}
]
}
```

### Javascript
```javascript
var evaluator = require('json-expression-eval');
var result = evaluator.evaluate({and:[{user: '[email protected]'}]},{userId:'[email protected]'});
const evaluator = require('json-expression-eval');
const functionsTable = {
user: (user, context) => {
return context.userId === user;
},
maxCount: (maxCount, context) => {
return context.times < maxCount;
}
};
const result = evaluator.evaluate({or:[{user: '[email protected]'},{not: {and: [{maxCount: 1},{user: '[email protected]'}]}}]},{userId:'[email protected]', times: 1},functionsTable);
```
```sh
Output should be 'true'
```
### TypeScript
```typescript
import { evaluate } from 'json-expression-eval';
console.log(evaluate({and:[{user: '[email protected]'}]},{userId:'[email protected]'});)

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 'true'
Output should be 'false'
```
## Test
```sh
Expand Down
7 changes: 7 additions & 0 deletions example/example.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,10 @@ expression = {
]
};
run(expression, context);
expression = {
or: [
{ not: { user: '[email protected]' } },
{ maxCount: 1 },
]
};
run(expression, context);
9 changes: 9 additions & 0 deletions example/example.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,14 @@ expression = {

run(expression, context);

expression = {
or: [
{not: { user: '[email protected]'}},
{ maxCount: 1},
]
};

run(expression, context);



0 comments on commit 1fcd919

Please sign in to comment.