@konker.dev/tiny-rules-fp
TypeScript icon, indicating that this package has built-in type declarations

0.0.2 • Public • Published

@konker.dev/tiny-rules-fp

A small rules engine for Node.js based on Effect-TS

CI License

Usage

import * as P from '@konker.dev/effect-ts-prelude';
import * as R from 'tiny-rules-fp';

// Define a context
type TestContext = {
  readonly foo: string;
  readonly bar: number;
};

// Define a set of facts
type TestFacts = {
  readonly isFooValid: boolean;
  readonly isBarEven: boolean;
  readonly isBaz: boolean;
};

// Create a rule-set with a set of initial facts, and a set of rules
P.pipe(
  R.createRuleSet<never, TestContext, never, TestFacts>({ isFooValid: false, isBarEven: false, isBaz: true }),
  R.sequence([
    R.addRuleFunc('isFooValid', (c: TestContext, _f: TestFacts) => c.foo === 'VALID'),
    R.addRuleFuncEffect('isBarEven', (c: TestContext, _f: TestFacts) => P.Effect.succeed(c.bar % 2 === 0)),
    R.addRuleFunc('isBaz', (_c: TestContext, _f: TestFacts) => false, 'isBaz is always false'),
  ])
);

// Execute the rules with a given context to derive new facts
const facts1 = P.Effect.runSync(
  unit.decide<never, TestContext, never, TestFacts>({ foo: 'NOT VALID', bar: 15 })(TEST_RULESET_2)
);
// facts1 === { isFooValid: false, isBarEven: false, isBaz: false }

const facts2 = P.Effect.runSync(
  unit.decide<never, TestContext, never, TestFacts>({ foo: 'VALID', bar: 16 })(TEST_RULESET_2)
);
// facts2 === { isFooValid: true, isBarEven: true, isBaz: false }

Package Sidebar

Install

npm i @konker.dev/tiny-rules-fp

Weekly Downloads

1

Version

0.0.2

License

ISC

Unpacked Size

12.3 kB

Total Files

6

Last publish

Collaborators

  • konker
  • morningwoodsoftware