Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@clipboard-health/rules-engine

Package Overview
Dependencies
Maintainers
0
Versions
53
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@clipboard-health/rules-engine

A pure functional rules engine.

  • 1.5.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
804
decreased by-53.74%
Maintainers
0
Weekly downloads
 
Created
Source

@clipboard-health/rules-engine

A pure functional rules engine with two rule runners:

  • firstMatch: Runs the first rule that matches the criteria
  • all: Runs all the rules that matches the criteria

Table of contents

  • Install
  • Usage
  • Local development commands

Install

npm install @clipboard-health/rules-engine

Usage

// ./examples/rules.ts

import {
  all,
  appendOutput,
  firstMatch,
  type Rule,
  type RuleContext,
} from "@clipboard-health/rules-engine";

interface Input {
  number1: number;
  number2: number;
}

interface Output {
  result: number;
}

const exampleContext: RuleContext<Input, Output> = {
  input: {
    number1: 2,
    number2: 5,
  },
  output: [],
};

const addNumbersIfPositiveRule: Rule<Input, Output> = {
  runIf: (input) => input.number1 > 0 && input.number2 > 0,
  run: (context) => {
    const { number1, number2 } = context.input;
    const sum = number1 + number2;
    return appendOutput(context, { result: sum });
  },
};

const multiplyNumbersIfPositiveRule: Rule<Input, Output> = {
  runIf: (input) => input.number1 > 0 && input.number2 > 0,
  run: (context) => {
    const { number1, number2 } = context.input;
    const sum = number1 * number2;
    return appendOutput(context, { result: sum });
  },
};

const divideNumbersIfNegative: Rule<Input, Output> = {
  runIf: (input) => input.number1 < 0 && input.number2 < 0,
  run: (context) => {
    const { number1, number2 } = context.input;
    const sum = number1 * number2;
    return appendOutput(context, { result: sum });
  },
};

// Using all() applies all the rules to the context
const allResult = all(
  addNumbersIfPositiveRule,
  divideNumbersIfNegative,
  multiplyNumbersIfPositiveRule,
).run(exampleContext);

console.log(allResult.output);
// => [{ result: 7 }, { result: 10 }]

// Using firstMatch() applies the first the rules to the context
const firstMatchResult = firstMatch(
  divideNumbersIfNegative,
  addNumbersIfPositiveRule,
  multiplyNumbersIfPositiveRule,
).run(exampleContext);

console.log(firstMatchResult.output);
// => [{ result: 7 }]

Local development commands

See package.json scripts for a list of commands.

FAQs

Package last updated on 22 Oct 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc