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

plain-rules-engine

v1.1.0

Published

A simple rules engine written in Typescript

Downloads

51

Readme

Plain Rules Engine

License TypeScript

Introduction

Plain Rules Engine is a TypeScript library for creating and evaluating simple business rules in Node.js applications. It provides a flexible way to define and apply rules to your data.

Features

  • Easy rule creation and evaluation. Defining a rule in JSON is as simple as:
{
    "trackHasStrongLanguage": {
        "conditions": [
            ["$.user.age", "lessThan", 16],
            ["$.tags", "contains", "strong language"]
        ],
        "effect": {
            "action": "omit"
        }
    },
  ... other rules
}

You can also define rules in TypeScript:

export const musicSearchRules: Rule = {
  trackHasStrongLanguage: {
    conditions: [
      ['$.user.age', Operator.LESS_THAN, 16],
      ['$.tags', Operator.CONTAINS, 'strong language'],
    ],
    effect: {
      action: Action.OMIT,
    },
  },
  // Add more rules here
};

In this case, the effect defined for "trackHasStrongLanguage" will only be applied to objects that meet all conditions in conditions array

  • Customizable rule actions and conditions.
  • Written in TypeScript for type safety.

Installation

You can install Plain Rules Engine via npm:

npm install plain-rules-engine

Usage

Here's an example of how to use Plain Rules Engine:

import {
  RuleEngine,
  type Rule,
  type ApplyRulesResponse,
} from 'plain-rules-engine';

// Define your rules (could be in a JSON file, TS file, an object store, in-memory database etc.). See example rule definition above

// Fetch your rules (you'll have to create the function that does that)
const musicSearchRules: Rule = fetchRulesFromDataSource();

// Instantiate the rule engine
const engine = new RuleEngine(musicSearchRules);

// Define your data
const tracks = [
  {
    title: 'Heal the World',
    artist: 'Michael Jackson',
    year: 1991,
    album: 'Dangerous',
    tags: ['pop', 'society'],
  },
  {
    title: 'The Girl From Last Night',
    artist: 'Jackson Pepper',
    year: 2017,
    album: 'Slot Machine',
    tags: ['rap', 'strong language'],
  },
];

const user = {
  user: {
    age: 14,
    lastLogin: '2023-08-26T16:00:45Z',
  },
};

// Apply the rules
const evaluation = engine.applyRules(tracks, user);

// Handle the results
console.log('RESULT: ', evaluation);

For more detailed documentation and examples, please refer to Building a Rule Engine With TypeScript — the article that inspired Plain Rules Engine.

Contribution

We welcome contributions from the community. If you'd like to contribute to this project or have any questions, issues, or suggestions, please feel free to open an issue.

License

This project is licensed under the Apache-2.0 License.