-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
61 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -35,3 +35,10 @@ expression = { | |
] | ||
}; | ||
run(expression, context); | ||
expression = { | ||
or: [ | ||
{ not: { user: '[email protected]' } }, | ||
{ maxCount: 1 }, | ||
] | ||
}; | ||
run(expression, context); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -47,5 +47,14 @@ expression = { | |
|
||
run(expression, context); | ||
|
||
expression = { | ||
or: [ | ||
{not: { user: '[email protected]'}}, | ||
{ maxCount: 1}, | ||
] | ||
}; | ||
|
||
run(expression, context); | ||
|
||
|
||
|