imicros-rules-compiler

0.0.15 • Public • Published

imicros-rules-compiler

Build Status Coverage Status

Installation

$ npm install imicros-rules-compiler --save

Usage

Usage Compiler

const { Compiler } = require("imicros-rules-compiler");
 
let exp;
// Define rule set
exp = "@@ "
exp += "~F user.groups.name[..string]; > result.acl[string]:= 'decline'; > result.rule[number]:= 0"
exp += "@ user.groups.name :: 'admin','guests' => result.acl := 'allow'; result.rule := 1"
exp += "@ user.groups.name :: 'others','members' => result.acl := 'allow'; result.rule := 2"
exp += "@@"
 
Compiler.compile(exp).then(strFunction => {
    // The compiled function can be stored somewhere as a string
    
    // For execution create a function from the string...
    let f = new Function(strFunction)();    
                                           
    // ...and execute the ruleset function with parameters to check against the rules
    let response = f({user: { groups: { name: ["users"] } }});
    console.log(JSON.stringify(response))   // {"result":{"acl":"decline","rule":0}}
    response = f({user: { groups: { name: ["guests"] } }});                                       
    console.log(JSON.stringify(response))   // {"result":{"acl":"allow","rule":1}}
    response = f({user: { groups: { name: ["members"] } }});                                       
    console.log(JSON.stringify(response))   // {"result":{"acl":"allow","rule":2}}                                     
});
 

Rules Language

  • @@ starts and ends the ruleset.
  • @ starts a new rule.
  • => starts the defintion the resulting output.
  • All conditions are defined left of =>. Multiple conditions are separated by ;. Each condition has exactly one parameter on the left side of :: the required values to check on the right side.
  • The expression on the right side of an condition can be a single value, a list of values, a comparison like > 5 or a range [2018-1-21..2018-2-23].
  • After the initial @@ can follow the hit policy ~F and type defintions of the used parameters in the ruleset. Definitions of output parameter are noted with a leading >. Parameters can be initialized with default values - e.g. result.acl[string]:= 'decline'.

Type definitions

Valid types are

  • [string]
  • [..string] array of strings
  • [number] - as decimal point, only . is allowed. The regex for numbers is /(0[xX][ \d a-f A-F ]+|0[oO][0-7]+|0[bB][01]+|(?:\d+(?!(?:.\d|\d)))|(?:\d+.\d+)(?!(?:.\d|\d))(?: [eE][+-]?\d+ )?)/
  • [..number] array of numbers
  • [date]
  • [..date] array of dates
  • [time]
  • [..time] array of times
  • [boolean]

Examples for valid conditions

  • user.age :: >= 16 & <= +35 Age is between 16 and 35
  • environment.date :: [2018-1-21..2018-2-23],>=2018-05-07 Date is between 2018-1-21 and 2018-2-23 or greater equal 2018-05-07
  • environment.time :: [6:00..08:00:00],>=18:00 Time is between 6 and 8 am or after 6pm
  • age :: ]12..16[,>65 Age is between 13 and 15 (the interval values 12 and 16 are excluded).

/imicros-rules-compiler/

    Package Sidebar

    Install

    npm i imicros-rules-compiler

    Weekly Downloads

    6

    Version

    0.0.15

    License

    MIT

    Unpacked Size

    174 kB

    Total Files

    31

    Last publish

    Collaborators

    • al66