@clipboard-health/rules-engine 1.15.0 → 1.16.1
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.
- package/README.md +21 -21
- 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
|
-
|
|
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
|
-
|
|
33
|
-
|
|
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
|
-
|
|
43
|
-
|
|
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.
|
|
52
|
+
runIf: (input) => input.a > 0 && input.b > 0,
|
|
50
53
|
run: (context) => {
|
|
51
|
-
const {
|
|
52
|
-
|
|
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.
|
|
60
|
+
runIf: (input) => input.a > 0 && input.b > 0,
|
|
59
61
|
run: (context) => {
|
|
60
|
-
const {
|
|
61
|
-
|
|
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.
|
|
68
|
+
runIf: (input) => input.a < 0 && input.b < 0,
|
|
68
69
|
run: (context) => {
|
|
69
|
-
const {
|
|
70
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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.
|
|
4
|
+
"version": "1.16.1",
|
|
5
5
|
"bugs": "https://github.com/ClipboardHealth/core-utils/issues",
|
|
6
6
|
"dependencies": {
|
|
7
7
|
"tslib": "2.8.0",
|