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

the-decider

v1.0.0

Published

...but I'm the decider.

Downloads

8

Readme

Simple Decider

The Simple Decider is a lightweight JavaScript tool for evaluating conditions against a set of rules and determining outcomes. It focuses on straightforward decision-making scenarios, without the complexity of multiple matches or strict matching options.

Features

  • Simple Rule Evaluation: Provides core functionality for evaluating a set of conditions against predefined rules.
  • Default Result Handling: Returns a default result when no rule matches or throws an error if no default is provided.

Installation

To install the Simple Decider, add it to your project using npm:

npm install decider

Usage

Import the Simple Decider and create an engine instance with your set of rules.

Example

const { simpleDecider } = require('./simpleDecider');

// Define the rules
const rules = [
    { rule: { hasAccess: false }, result: "Access Denied" },
    { rule: { hasAccess: true, isAdmin: true }, result: "Admin Access Granted" },
    { rule: { hasAccess: true }, result: "User Access Granted" }
];

// Create an engine instance
const engine = simpleDecider(rules, { defaultResult: "Default Access" });

// Evaluate a subject
const subject1 = { hasAccess: true, isAdmin: true };
console.log(engine(subject1));  // Output: "Admin Access Granted"

const subject2 = { hasAccess: false };
console.log(engine(subject2));  // Output: "Access Denied"

const subject3 = { isAdmin: true };  // Missing hasAccess flag
console.log(engine(subject3));  // Output: "Default Access"

Differences from Decider

  • No Multiple Matches: The Simple Decider does not support returning multiple matches. It returns the first match found.
  • No Strict Matching: The strict option is not available, meaning extra properties in the subject will not prevent a match.
  • Simplified Logic: The core logic is simplified to ensure quick evaluations for basic rule sets.

Error Handling

If no rules match and no default result is specified, the engine throws an error.

const rules = [
    { rule: { hasAccess: true, isAdmin: true }, result: "Admin Access Granted" }
];

const engine = simpleDecider(rules);
const subject = { hasAccess: false };

// Throws: "No matching rule found for the provided subject."
console.log(engine(subject));

Decider

The Decider builds on the functionality of the Simple Decider to handle more complex scenarios. It provides additional features such as matching specific rules, collecting all matches, and strict matching.

I am the decider

Features

  • Simple Rule Evaluation: Provides core functionality for evaluating a set of conditions against predefined rules.
  • Strict Matching: Allows for strict matching where no extra properties beyond those in the rule are allowed in the subject.
  • Multiple Matches: Supports returning all possible matches instead of just the first match.
  • Default Result Handling: Returns a default result when no rule matches or throws an error if no default is provided.
  • Array-Based Conditions: Supports array-based conditions for scenarios where a value must match one of several possibilities.

Usage

Import the Decider and create a decider instance with your set of rules.

Example

const { decider } = require('./decider');

// Define the rules
const rules = [
    { rule: { hasAccess: false }, result: "Access Denied" },
    { rule: { hasAccess: true, isAdmin: true }, result: "Admin Access Granted" },
    { rule: { hasAccess: true }, result: "User Access Granted" }
];

// Create an engine instance
const engine = decider(rules, { returnAllMatches: false, defaultResult: "Default Access" });

// Evaluate a subject
const subject1 = { hasAccess: true, isAdmin: true };
console.log(engine(subject1));  // Output: "Admin Access Granted"

const subject2 = { hasAccess: false };
console.log(engine(subject2));  // Output: "Access Denied"

const subject3 = { isAdmin: true };  // Missing hasAccess flag
console.log(engine(subject3));  // Output: "Default Access"

Parameters

  • rules: An array of rule objects where each rule has:

    • rule: An object defining the conditions for matching (e.g., { hasAccess: true }).
    • result: The result to return if the rule matches (e.g., "Access Granted").
    • strict (optional): A boolean indicating if strict matching should be applied (default is false).
  • options: An optional object with the following properties:

    • defaultResult: A value to return if no rules match.
    • returnAllMatches: If true, returns an array of all matching results. Defaults to false.

Advanced Features

Strict Matching

The strict option enforces that no extra properties are allowed beyond those specified in the rule. If the subject contains extra properties, the rule will not match.

const rules = [
    { rule: { hasAccess: true, isAdmin: true }, result: "Strict Admin Access", strict: true }
];

const engine = decider(rules);
const subject = { hasAccess: true, isAdmin: true, extraProperty: true };

// Throws an error or returns default if set, since strict mode disallows extra properties
console.log(engine(subject));

Returning All Matches

You can configure the Decider to return all matching rules instead of just the first match.

const rules = [
    { rule: { hasAccess: true, isAdmin: true }, result: "Admin Access Granted" },
    { rule: { hasAccess: true }, result: "User Access Granted" }
];

const engine = decider(rules, { returnAllMatches: true });
const subject = { hasAccess: true, isAdmin: true };

// Output: ["Admin Access Granted", "User Access Granted"]
console.log(engine(subject));

Testing

To run tests for the Decider and Simple Decider, you can use Jest or any other JavaScript testing framework. Here's a sample unit test using Jest:

const { decider } = require('./decider');
const { simpleDecider } = require('./simpleDecider');

test("Returns 'Admin Access Granted' when both hasAccess and isAdmin are true", () => {
    const rules = [
        { rule: { hasAccess: true, isAdmin: true }, result: "Admin Access Granted" },
        { rule: { hasAccess: true }, result: "User Access Granted" }
    ];
    const engine = decider(rules);
    const subject = { hasAccess: true, isAdmin: true };
    expect(engine(subject)).toBe("Admin Access Granted");

    const simpleEngine = simpleDecider(rules);
    expect(simpleEngine(subject)).toBe("Admin Access Granted");
});

License

This project is licensed under the MIT License. See the LICENSE file for more details.