@clipboard-health/rules-engine 1.15.0 → 1.16.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/README.md +21 -21
  2. package/package.json +1 -1
package/README.md CHANGED
@@ -18,8 +18,11 @@ npm install @clipboard-health/rules-engine
18
18
 
19
19
  ## Usage
20
20
 
21
+ <embedex source="packages/rules-engine/examples/rules.ts">
22
+
21
23
  ```ts
22
- // packages/rules-engine/examples/rules.ts
24
+ import { deepEqual } from "node:assert/strict";
25
+
23
26
  import {
24
27
  all,
25
28
  appendOutput,
@@ -29,8 +32,8 @@ import {
29
32
  } from "@clipboard-health/rules-engine";
30
33
 
31
34
  interface Input {
32
- number1: number;
33
- number2: number;
35
+ a: number;
36
+ b: number;
34
37
  }
35
38
 
36
39
  interface Output {
@@ -39,36 +42,33 @@ interface Output {
39
42
 
40
43
  const exampleContext: RuleContext<Input, Output> = {
41
44
  input: {
42
- number1: 2,
43
- number2: 5,
45
+ a: 2,
46
+ b: 5,
44
47
  },
45
48
  output: [],
46
49
  };
47
50
 
48
51
  const addNumbersIfPositiveRule: Rule<Input, Output> = {
49
- runIf: (input) => input.number1 > 0 && input.number2 > 0,
52
+ runIf: (input) => input.a > 0 && input.b > 0,
50
53
  run: (context) => {
51
- const { number1, number2 } = context.input;
52
- const sum = number1 + number2;
53
- return appendOutput(context, { result: sum });
54
+ const { a, b } = context.input;
55
+ return appendOutput(context, { result: a + b });
54
56
  },
55
57
  };
56
58
 
57
59
  const multiplyNumbersIfPositiveRule: Rule<Input, Output> = {
58
- runIf: (input) => input.number1 > 0 && input.number2 > 0,
60
+ runIf: (input) => input.a > 0 && input.b > 0,
59
61
  run: (context) => {
60
- const { number1, number2 } = context.input;
61
- const sum = number1 * number2;
62
- return appendOutput(context, { result: sum });
62
+ const { a, b } = context.input;
63
+ return appendOutput(context, { result: a * b });
63
64
  },
64
65
  };
65
66
 
66
67
  const divideNumbersIfNegative: Rule<Input, Output> = {
67
- runIf: (input) => input.number1 < 0 && input.number2 < 0,
68
+ runIf: (input) => input.a < 0 && input.b < 0,
68
69
  run: (context) => {
69
- const { number1, number2 } = context.input;
70
- const sum = number1 * number2;
71
- return appendOutput(context, { result: sum });
70
+ const { a, b } = context.input;
71
+ return appendOutput(context, { result: a / b });
72
72
  },
73
73
  };
74
74
 
@@ -79,8 +79,7 @@ const allResult = all(
79
79
  multiplyNumbersIfPositiveRule,
80
80
  ).run(exampleContext);
81
81
 
82
- console.log(allResult.output);
83
- // => [{ result: 7 }, { result: 10 }]
82
+ deepEqual(allResult.output, [{ result: 7 }, { result: 10 }]);
84
83
 
85
84
  // Using firstMatch() applies the first the rules to the context
86
85
  const firstMatchResult = firstMatch(
@@ -89,10 +88,11 @@ const firstMatchResult = firstMatch(
89
88
  multiplyNumbersIfPositiveRule,
90
89
  ).run(exampleContext);
91
90
 
92
- console.log(firstMatchResult.output);
93
- // => [{ result: 7 }]
91
+ deepEqual(firstMatchResult.output, [{ result: 7 }]);
94
92
  ```
95
93
 
94
+ </embedex>
95
+
96
96
  ## Local development commands
97
97
 
98
98
  See [`package.json`](./package.json) `scripts` for a list of commands.
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@clipboard-health/rules-engine",
3
3
  "description": "A pure functional rules engine to keep logic-dense code simple, reliable, understandable, and explainable.",
4
- "version": "1.15.0",
4
+ "version": "1.16.0",
5
5
  "bugs": "https://github.com/ClipboardHealth/core-utils/issues",
6
6
  "dependencies": {
7
7
  "tslib": "2.8.0",