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

typescript-business-rules-engine

v1.1.12

Published

A Typescript rule engine where rules are defined in JSON format.

Downloads

26

Readme

TypeScript Business Rules Engine

A Typescript rule engine where rules are defined in JSON format.

Facts are plain JavaScript or JSON objects or objects from ES6 classes with getters and setters. Rules are specified in pure JavaScript rather than in a separate, special-purpose language like DSL.

Install

npm install typescript-business-rules-engine

Usage

This is a basic example.

// import
import { Engine } from 'typescript-business-rules-engine';

const facts = {
	user: {
		name: 'frank',
		stars: 347,
	},
	weather: {
		temperature: 20,
		windy: true,
		rainy: false,
	},
};

const rules = [
	{
		// if data.user.stars >= 200 then data.user.mood = 'great'
		name: 'Mood Great',
		description: 'mood is great if 200 stars or more',
		priority: 0,
		final: false,
		activationGroup: '',
		condition: {
			'$op.greaterEqualThan': [
				{
					'$ctx.get': ['data.user.stars'],
				},
				200,
			],
		},
		preActions: [],
		postActions: [
			{
				'$ctx.set': ['data.user.mood', 'great'],
			},
		],
	},
	{
		// if data.user.mood === 'great && data.weather.temperature >= 20 && !data.weather.rainy then data.goWalking = true
		name: 'go for wak',
		description: 'go for a walk if mood is great and the weather is fine',
		priority: 0,
		final: false,
		activationGroup: '',
		condition: {
			'$op.and': [
				{
					'$op.equal': [
						{
							'$ctx.get': ['data.user.mood'],
						},
						'great',
					],
				},
				{
					'$op.greaterEqualThan': [
						{
							'$ctx.get': ['data.weather.temperature'],
						},
						20,
					],
				},
				{
					'$op.not': [
						{
							'$ctx.get': ['data.weather.rainy'],
						},
					],
				},
			],
		},
		preActions: [],
		postActions: [
			{
				'$ctx.set': ['data.goWalking', true],
			},
		],
	},
	{
		// if wheater.temperature < 20 then data.user.mood = 'bad'
		name: 'Mood Bad',
		description: 'mood is bad if temperature is below 20',
		priority: 0,
		final: false,
		activationGroup: '',
		condition: {
			'$op.lessThan': [
				{
					'$ctx.get': ['data.weather.temperature'],
				},
				20,
			],
		},
		preActions: [],
		postActions: [
			{
				'$ctx.set': ['data.user.mood', 'bad'],
			},
		],
	},
];

const main = async () => {
	const engine = new Engine('Test Engine', rules);

	console.log(await engine.evaluate(facts));
	/*{
    elapsed: 6,
    fired: [
        {
        name: 'Mood Great',
        fired: true,
        discarted: false,
        actions: [Array]
        }
    ],
    context: { data: { user: [Object], weather: [Object] }, extra: {} }
    } 
    */
};

main();

These are the resulting facts:

{
    elapsed: 6,
    fired: [
        {
        name: 'Mood Great',
        fired: true,
        discarted: false,
        actions: [Array]
        }
    ],
    context: { data: { user: [Object], weather: [Object] }, extra: {} }
    }

Features

Rule engine

The engine utilizes forward-chaining methodology and operates within a typical match-resolve-act cycle. It endeavors to infer the maximum amount of information feasible from the provided facts and regulations. If there is no additional knowledge to acquire, the process concludes.

Final rules

For optimization purposes, it can be useful to stop the engine as soon as a specific rule has fired. This can be achieved by settings the respective rules' property final to true. Default, of course, is false.

Activation groups

Only one rule within an activation group will fire during a match-resolve-act cycle, i.e., the first one to fire discards all other rules within the same activation group. Use the rule's activationGroup property to set its activation group.