Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/sidhantpanda/json-rule-evaluator


https://github.com/sidhantpanda/json-rule-evaluator

Last synced: 23 days ago
JSON representation

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