-
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
9 changed files
with
303 additions
and
41 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 |
---|---|---|
|
@@ -92,6 +92,17 @@ const expression: IExampleExpression = { | |
{ | ||
userId: '[email protected]', | ||
}, | ||
{ | ||
times: { | ||
lte: { | ||
op: '+', | ||
lhs: { | ||
ref: 'nested.value4' | ||
}, | ||
rhs: 2, | ||
}, | ||
}, | ||
}, | ||
{ | ||
and: [ | ||
{ | ||
|
@@ -129,7 +140,7 @@ There are 4 types of operators you can use (evaluated in that order of precedenc | |
- `and` - accepts a non-empty list of expressions | ||
- `or` - accepts a non-empty list of expressions | ||
- `not` - accepts another expressions | ||
- `<user defined funcs>` - accepts any type of argument and evaluated by the user defined functions and the given context. Can be async. | ||
- `<user defined funcs>` - accepts any type of argument and evaluated by the user defined functions, and the given context (can be async). | ||
- `<compare funcs>` - operates on one of the context properties and compares it to a given value. | ||
- `{property: {op: value}}` | ||
- available ops: | ||
|
@@ -145,13 +156,36 @@ There are 4 types of operators you can use (evaluated in that order of precedenc | |
- `inq: any[]` - True if in an array of values. Comparison is done using the `===` operator | ||
- `between: readonly [number, number] (as const)` - True if the value is between the two specified values: greater than or equal to first value and less than or equal to second value. | ||
- `{property: value}` | ||
- compares the property to that value (shorthand to the `eq` op) | ||
- compares the property to that value (shorthand to the `eq` op, without the option to user math or refs to other properties) | ||
|
||
> Nested properties in the context can also be accessed using a dot notation (see example above) | ||
> In each expression level, you can only define 1 operator, and 1 only | ||
> You can reference values (and nested values) from the context using the {"ref":"<dot notation path>"} | ||
> (see example above) on the right-hand side of expressions (not in parameters to user defined functions though) | ||
The right-hand side of compare (not user defined) functions can be a: | ||
- literal - number/string/boolean (depending on the left-hand side of the function) | ||
- reference to a property (or nested property) in the context. | ||
This can be achieved by using `{"ref":"<dot notation path>"}` | ||
- A math operation that can reference properties in the context. | ||
The valid operations are `+,-,*,/,%,pow`. | ||
This can be achieved by using | ||
```json | ||
{ | ||
"op": "<+,-,*,/,%,pow>", | ||
"lhs": {"ref": "<dot notation path>"}, // or a number literal | ||
"rhs": {"ref": "<dot notation path>"} // or a number literal | ||
} | ||
``` | ||
which will be computed as `<lhs> <op> <rhs>` where lhs is left-hand-side and rhs is right-hand-side. So for example | ||
```json | ||
{ | ||
"op": "/", | ||
"lhs": 10, | ||
"rhs": 2 | ||
} | ||
``` | ||
will equal `10 / 2 = 5` | ||
|
||
|
||
Example expressions, assuming we have the `user` and `maxCount` user defined functions in place can be: | ||
```json | ||
|
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
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 |
---|---|---|
|
@@ -50,6 +50,7 @@ rules = [ | |
{user: '[email protected]'}, | ||
{maxCount: 5}, | ||
{times: {eq:{ref:'nested.value'}}}, | ||
{times: {lte:{op:'+', lhs: {ref:'nested.value'}, rhs: 1}}}, | ||
], | ||
}, | ||
consequence: { | ||
|
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 |
---|---|---|
|
@@ -45,6 +45,15 @@ expression = { | |
|
||
run(expression, context); | ||
|
||
expression = { | ||
and: [ | ||
{user: '[email protected]'}, | ||
{times: {eq:{op:'+', lhs: {ref:'nested.value'}, rhs: 1}}}, | ||
], | ||
}; | ||
|
||
run(expression, context); | ||
|
||
expression = { | ||
and: [ | ||
{user: '[email protected]'}, | ||
|
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
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
Oops, something went wrong.