Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/sidhantpanda/json-rule-evaluator
https://github.com/sidhantpanda/json-rule-evaluator
Last synced: 23 days ago
JSON representation
- Host: GitHub
- URL: https://github.com/sidhantpanda/json-rule-evaluator
- Owner: sidhantpanda
- Created: 2020-11-20T15:16:48.000Z (almost 4 years ago)
- Default Branch: main
- Last Pushed: 2024-09-11T10:08:15.000Z (2 months ago)
- Last Synced: 2024-10-03T22:41:10.663Z (about 1 month ago)
- Language: TypeScript
- Size: 18.6 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# json-rule-evaluator
---Demo at: https://gifted-mcclintock-348e6f.netlify.app/
#### Installation
```
npm i -S json-rule-evaluator
```#### Usage
```typescript
import RuleEvaluator, { Rule } from 'json-rule-evaluator';const sampleData = {
some: {
boolean: true,
number: 12,
string: 'abc',
},
other: {
boolean: false,
number: 2,
string: 'xyz',
},
};const testRule: Rule = {
$and: [
{
path: 'some.number',
value: 12,
operator: 'gt',
},
{
path: 'some.boolean',
value: true,
operator: '==',
},
{
$or: [
{
path: 'some.string',
ref: 'other.string',
operator: '!=',
options: {
caseSensitive: false,
},
},
{
path: 'some.boolean',
ref: 'other.boolean',
operator: '==',
},
],
},
],
};const evaluator = new RuleEvaluator(testRule);
const processed = evaluator.test(sampleData);console.log(processed);
// Output:
// {
// result: false,
// details: {
// operator: "$and",
// status: "false",
// children: [{
// rule: {
// path: "some.number",
// value: 12,
// operator: "gt"
// },
// status: "false"
// }, {
// rule: {
// path: "some.boolean",
// value: true,
// operator: "=="
// },
// status: "not_evaluated"
// }, {
// operator: "$or",
// status: "not_evaluated",
// children: [{
// rule: {
// path: "some.string",
// ref: "other.string",
// operator: "!=",
// options: {
// caseSensitive: false
// }
// },
// status: "true"
// },
// {
// rule: {
// path: "some.boolean",
// ref: "other.boolean",
// operator: "=="
// },
// status: "not_evaluated"
// }]
// }]
// }
// }
```#### Available Operators
##### Boolean
- ==
- !=
- and
- or##### Number
- ==
- !=
- lt (lesser than)
- gt (greater than)
- lte (lesser than or equal)
- gte (greater than or equal)##### String
- ==
- !=
- contains
- startsWith
- endsWith
- doesntContain
- doesntStartWith
- doesntEndWith