npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

imicros-rules-compiler

v0.0.15

Published

rules interpreter/compiler for rules engine

Downloads

27

Readme

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).