1.0.14 • Published 5 months ago

awesome-logic-parser v1.0.14

Weekly downloads
-
License
ISC
Repository
github
Last release
5 months ago

Awesome Logic Parser

The Awesome Logic Parser is a JavaScript library for evaluating logic rules based on a set of rules and a data context. This library provides the ability to define complex rules and evaluate them against data.

Installation

You can install the Logic Parser library using npm:

npm install awesome-logic-parser

Usage

// Import the LogicParser library
const { LogicParser } = require('logic-parser');

// Sample data
const data = {
  age: 25,
  name: 'John',
  hobbies: ['reading', 'swimming'],
  city: 'New York',
  text: 'Hello, World'
};

// Define variables for testing
const minAge = 18;
const maxAge = 30;
const targetCity = 'New York';
const startsWith = 'Jo';
const hobby = 'swimming';
const endsWith = 'World';

// Sample rules
const logicGroup = {
  type: 'group',
  rules: [
    // Relational operators
    {
      type: 'expr',
      left: '{age}',
      operator: 'GTE',
      right: minAge
    },
    {
      type: 'logic',
      operator: 'AND'
    },
    {
      type: 'expr',
      left: '{city}',
      operator: 'EQ',
      right: targetCity
    },
    {
      type: 'logic',
      operator: 'AND'
    },
    {
      type: 'expr',
      left: '{name}',
      operator: 'STARTSWITH',
      right: startsWith
    },
    {
      type: 'logic',
      operator: 'AND'
    },
    {
      type: 'expr',
      left: '{hobbies}',
      operator: 'INCLUDES',
      right: hobby
    },
    {
      type: 'logic',
      operator: 'AND'
    },
    {
      type: 'expr',
      left: '{text}',
      operator: 'ENDSWITH',
      right: endsWith
    },
    {
      type: 'logic',
      operator: 'AND'
    },
    {
      type: 'expr',
      left: '{age}',
      operator: 'BETWEEN',
      right: {
        start: minAge,
        end: maxAge
      }
    }
  ]
};

// Set up the LogicParser
const logicParser = new LogicParser({
  resultWhenEmpty: false,
  returnFalseWhenError: true
});

// Evaluate the rules
const result = logicParser.parse(logicGroup, data);

License

This library is released under the MIT License.

1.0.14

5 months ago

1.0.13

5 months ago

1.0.11

5 months ago

1.0.10

5 months ago

1.0.12

5 months ago

1.0.9

6 months ago

1.0.8

6 months ago

1.0.7

6 months ago

1.0.6

6 months ago

1.0.5

6 months ago

1.0.4

6 months ago

1.0.3

6 months ago

1.0.2

6 months ago

1.0.1

6 months ago

1.0.0

6 months ago