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 🙏

© 2026 – Pkg Stats / Ryan Hefner

nested-rules-engine

v3.0.0

Published

Nested Conditional Rules Engine

Readme

nested-rules-engine

Synopsis

nested-rules-engine runs a decision tree that you describe with plain JavaScript objects or JSON.

You provide three things:

  1. inputs: the data you want to check.
  2. rules: the decision tree.
  3. functions: small functions that either choose a branch or return the final result.

The engine walks the tree from top to bottom. When a condition function returns true, it follows that branch. When it reaches a leaf, it runs the matching result function.

Features

  • Write rules as readable JSON-like objects.
  • Nest rules to model multi-step decisions.
  • Change the input object while the tree is running, if needed.
  • Run one tree or multiple trees against the same inputs.

Installation

npm install nested-rules-engine --save
# or
yarn add nested-rules-engine

Basic Example

import { executeEngine } from 'nested-rules-engine';

interface Inputs {
  day: string;
  weather: string;
}

interface Result {
  plan: string;
}

// 1. Describe the tree.
const rules = {
  is_weekend: {
    is_raining: 'stay_home',
    default: 'go_outside',
  },
  default: 'go_to_work',
} as const;

// 2. Provide the input data.
const inputs: Inputs = {
  day: 'saturday',
  weather: 'sunny',
};

// 3. Add one function for every rule name and result name.
const functions = {
  default: () => true,
  is_weekend: ({ day }: Inputs) => day === 'saturday' || day === 'sunday',
  is_raining: ({ weather }: Inputs) => weather === 'rainy',
  stay_home: (): Result => ({ plan: 'Stay home' }),
  go_outside: (): Result => ({ plan: 'Go outside' }),
  go_to_work: (): Result => ({ plan: 'Go to work' }),
};

// 4. Run the engine.
const res = executeEngine(inputs, functions, rules);

// res is:
/*
{
  result: { plan: 'Go outside' },
  logs: []
}
*/

Documentation

Execution signature:

executeEngine(variables: Record<string, any>, functions: Record<string, Function>, rules: Record<string, any>, options?: Options);

Inputs

  • variables: the input object passed to every function. Functions receive this object by reference, so they can add, edit, or delete values while the tree is running.

  • functions: a function map. Every key used in rules must have a matching function.

    • Branch functions should return true when the engine should follow that branch.
    • Leaf functions return the final value you want in result.
  • rules: the decision tree to run.

  • options: optional settings.

    • verbose (boolean): include execution logs.
    • multiple (boolean): run an array of decision trees against the same inputs.

Outputs

  • result: the value returned by the selected leaf function.
  • logs: execution logs. This is an empty array unless verbose is enabled, or an error object if validation fails.

How Defaults Work

Use a default branch when you want a fallback:

const rules = {
  is_admin: 'show_admin_dashboard',
  default: 'show_regular_dashboard',
};

const functions = {
  default: () => true,
  is_admin: ({ role }) => role === 'admin',
  show_admin_dashboard: () => 'Admin dashboard',
  show_regular_dashboard: () => 'Regular dashboard',
};

If is_admin returns false, the engine continues to the next rule. Since default returns true, the fallback result is used.

Advanced Examples

  1. Verbose output and multiple executions
  2. Changing inputs while the engine is running

License

MIT