@clipboard-health/rules-engine 1.2.3 → 1.2.5

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/dist/README.md ADDED
@@ -0,0 +1,100 @@
1
+ # @clipboard-health/rules-engine
2
+
3
+ A pure functional rules engine with two rule runners:
4
+
5
+ - `firstMatch`: Runs the first rule that matches the criteria
6
+ - `all`: Runs all the rules that matches the criteria
7
+
8
+ ## Table of Contents
9
+
10
+ - [Install](#install)
11
+ - [Usage](#usage)
12
+ - [Local development commands](#local-development-commands)
13
+
14
+ ## Install
15
+
16
+ ```bash
17
+ npm install @clipboard-health/rules-engine
18
+ ```
19
+
20
+ ## Usage
21
+
22
+ ```ts
23
+ // ./examples/rules.ts
24
+
25
+ import {
26
+ all,
27
+ appendOutput,
28
+ firstMatch,
29
+ type Rule,
30
+ type RuleContext,
31
+ } from "@clipboard-health/rules-engine";
32
+
33
+ interface Input {
34
+ number1: number;
35
+ number2: number;
36
+ }
37
+
38
+ interface Output {
39
+ result: number;
40
+ }
41
+
42
+ const exampleContext: RuleContext<Input, Output> = {
43
+ input: {
44
+ number1: 2,
45
+ number2: 5,
46
+ },
47
+ output: [],
48
+ };
49
+
50
+ const addNumbersIfPositiveRule: Rule<Input, Output> = {
51
+ runIf: (input) => input.number1 > 0 && input.number2 > 0,
52
+ run: (context) => {
53
+ const { number1, number2 } = context.input;
54
+ const sum = number1 + number2;
55
+ return appendOutput(context, { result: sum });
56
+ },
57
+ };
58
+
59
+ const multiplyNumbersIfPositiveRule: Rule<Input, Output> = {
60
+ runIf: (input) => input.number1 > 0 && input.number2 > 0,
61
+ run: (context) => {
62
+ const { number1, number2 } = context.input;
63
+ const sum = number1 * number2;
64
+ return appendOutput(context, { result: sum });
65
+ },
66
+ };
67
+
68
+ const divideNumbersIfNegative: Rule<Input, Output> = {
69
+ runIf: (input) => input.number1 < 0 && input.number2 < 0,
70
+ run: (context) => {
71
+ const { number1, number2 } = context.input;
72
+ const sum = number1 * number2;
73
+ return appendOutput(context, { result: sum });
74
+ },
75
+ };
76
+
77
+ // Using all() applies all the rules to the context
78
+ const allResult = all(
79
+ addNumbersIfPositiveRule,
80
+ divideNumbersIfNegative,
81
+ multiplyNumbersIfPositiveRule,
82
+ ).run(exampleContext);
83
+
84
+ console.log(allResult.output);
85
+ // => [{ result: 7 }, { result: 10 }]
86
+
87
+ // Using firstMatch() applies the first the rules to the context
88
+ const firstMatchResult = firstMatch(
89
+ divideNumbersIfNegative,
90
+ addNumbersIfPositiveRule,
91
+ multiplyNumbersIfPositiveRule,
92
+ ).run(exampleContext);
93
+
94
+ console.log(firstMatchResult.output);
95
+ // => [{ result: 7 }]
96
+ ```
97
+
98
+ ## Local development commands
99
+
100
+ See [`package.json`](./package.json) `scripts` for a list of commands.
@@ -0,0 +1,30 @@
1
+ {
2
+ "name": "@clipboard-health/rules-engine",
3
+ "description": "A pure functional rules engine.",
4
+ "version": "1.2.5",
5
+ "bugs": "https://github.com/clipboardhealth/core-utils/issues",
6
+ "dependencies": {
7
+ "tslib": "2.7.0",
8
+ "type-fest": "4.26.1"
9
+ },
10
+ "files": [
11
+ "dist"
12
+ ],
13
+ "keywords": [],
14
+ "license": "MIT",
15
+ "main": "./dist/src/index.js",
16
+ "publishConfig": {
17
+ "access": "public"
18
+ },
19
+ "repository": {
20
+ "type": "git",
21
+ "url": "git+https://github.com/clipboardhealth/core-utils.git",
22
+ "directory": "packages/rules-engine"
23
+ },
24
+ "scripts": {
25
+ "embed": "embedme README.md"
26
+ },
27
+ "type": "commonjs",
28
+ "typings": "./src/index.d.ts",
29
+ "types": "./src/index.d.ts"
30
+ }
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";;;AAAA,6DAAmC;AACnC,qDAA2B;AAC3B,wDAA8B"}
@@ -0,0 +1 @@
1
+ {"version":3,"file":"appendOutput.js","sourceRoot":"","sources":["../../../src/lib/appendOutput.ts"],"names":[],"mappings":";;AAOA,oCAQC;AAXD;;GAEG;AACH,SAAgB,YAAY,CAC1B,OAAqC,EACrC,MAA6B;IAE7B,OAAO;QACL,KAAK,EAAE,OAAO,CAAC,KAAK;QACpB,MAAM,EAAE,CAAC,GAAG,OAAO,CAAC,MAAM,EAAE,MAAM,CAAC;KACpC,CAAC;AACJ,CAAC"}
@@ -5,9 +5,7 @@ export interface RuleContext<TInput, TOutput> {
5
5
  */
6
6
  input: ReadonlyDeep<TInput>;
7
7
  /**
8
- * Output is immutable, do not modify existing items, only append.
9
- *
10
- * @see {@link appendOutput}
8
+ * Output is immutable, do not modify existing items, only append using {@link appendOutput}.
11
9
  */
12
10
  output: ReadonlyArray<ReadonlyDeep<TOutput>>;
13
11
  }
@@ -0,0 +1 @@
1
+ {"version":3,"file":"rule.js","sourceRoot":"","sources":["../../../src/lib/rule.ts"],"names":[],"mappings":""}
@@ -0,0 +1 @@
1
+ {"version":3,"file":"all.js","sourceRoot":"","sources":["../../../../src/lib/runners/all.ts"],"names":[],"mappings":";;AAOA,kBAUC;AAfD;;;;GAIG;AACH,SAAgB,GAAG,CACjB,GAAG,KAA6C;IAEhD,OAAO;QACL,KAAK,EAAE,CAAC,KAAK,EAAE,EAAE,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC;QACzD,GAAG,EAAE,CAAC,OAAO,EAAE,EAAE,CACf,KAAK;aACF,MAAM,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC;aAC3C,MAAM,CAAC,CAAC,eAAe,EAAE,IAAI,EAAE,EAAE,CAAC,IAAI,CAAC,GAAG,CAAC,eAAe,CAAC,EAAE,OAAO,CAAC;KAC3E,CAAC;AACJ,CAAC"}
@@ -0,0 +1 @@
1
+ {"version":3,"file":"firstMatch.js","sourceRoot":"","sources":["../../../../src/lib/runners/firstMatch.ts"],"names":[],"mappings":";;AAOA,gCAUC;AAfD;;;;GAIG;AACH,SAAgB,UAAU,CACxB,GAAG,KAA6C;IAEhD,OAAO;QACL,KAAK,EAAE,CAAC,KAAK,EAAE,EAAE,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC;QACzD,GAAG,EAAE,CAAC,OAAO,EAAE,EAAE;YACf,MAAM,IAAI,GAAG,KAAK,CAAC,IAAI,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC,CAAC;YAC7D,OAAO,IAAI,CAAC,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC;QAC5C,CAAC;KACF,CAAC;AACJ,CAAC"}
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../src/lib/runners/index.ts"],"names":[],"mappings":";;;AAAA,gDAAsB;AACtB,uDAA6B"}
package/package.json CHANGED
@@ -1,15 +1,18 @@
1
1
  {
2
2
  "name": "@clipboard-health/rules-engine",
3
3
  "description": "A pure functional rules engine.",
4
- "version": "1.2.3",
4
+ "version": "1.2.5",
5
5
  "bugs": "https://github.com/clipboardhealth/core-utils/issues",
6
6
  "dependencies": {
7
- "tslib": "2.6.3",
8
- "type-fest": "4.25.0"
7
+ "tslib": "2.7.0",
8
+ "type-fest": "4.26.1"
9
9
  },
10
+ "files": [
11
+ "dist"
12
+ ],
10
13
  "keywords": [],
11
14
  "license": "MIT",
12
- "main": "./src/index.js",
15
+ "main": "./dist/src/index.js",
13
16
  "publishConfig": {
14
17
  "access": "public"
15
18
  },
@@ -22,6 +25,5 @@
22
25
  "embed": "embedme README.md"
23
26
  },
24
27
  "type": "commonjs",
25
- "typings": "./src/index.d.ts",
26
- "types": "./src/index.d.ts"
28
+ "typings": "./src/index.d.ts"
27
29
  }
package/src/index.js.map DELETED
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../packages/rules-engine/src/index.ts"],"names":[],"mappings":";;;AAAA,6DAAmC;AACnC,qDAA2B;AAC3B,wDAA8B"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"appendOutput.js","sourceRoot":"","sources":["../../../../../packages/rules-engine/src/lib/appendOutput.ts"],"names":[],"mappings":";;AAOA,oCAQC;AAXD;;GAEG;AACH,SAAgB,YAAY,CAC1B,OAAqC,EACrC,MAA6B;IAE7B,OAAO;QACL,KAAK,EAAE,OAAO,CAAC,KAAK;QACpB,MAAM,EAAE,CAAC,GAAG,OAAO,CAAC,MAAM,EAAE,MAAM,CAAC;KACpC,CAAC;AACJ,CAAC"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"rule.js","sourceRoot":"","sources":["../../../../../packages/rules-engine/src/lib/rule.ts"],"names":[],"mappings":""}
@@ -1 +0,0 @@
1
- {"version":3,"file":"all.js","sourceRoot":"","sources":["../../../../../../packages/rules-engine/src/lib/runners/all.ts"],"names":[],"mappings":";;AAOA,kBAUC;AAfD;;;;GAIG;AACH,SAAgB,GAAG,CACjB,GAAG,KAA6C;IAEhD,OAAO;QACL,KAAK,EAAE,CAAC,KAAK,EAAE,EAAE,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC;QACzD,GAAG,EAAE,CAAC,OAAO,EAAE,EAAE,CACf,KAAK;aACF,MAAM,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC;aAC3C,MAAM,CAAC,CAAC,eAAe,EAAE,IAAI,EAAE,EAAE,CAAC,IAAI,CAAC,GAAG,CAAC,eAAe,CAAC,EAAE,OAAO,CAAC;KAC3E,CAAC;AACJ,CAAC"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"firstMatch.js","sourceRoot":"","sources":["../../../../../../packages/rules-engine/src/lib/runners/firstMatch.ts"],"names":[],"mappings":";;AAOA,gCAUC;AAfD;;;;GAIG;AACH,SAAgB,UAAU,CACxB,GAAG,KAA6C;IAEhD,OAAO;QACL,KAAK,EAAE,CAAC,KAAK,EAAE,EAAE,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC;QACzD,GAAG,EAAE,CAAC,OAAO,EAAE,EAAE;YACf,MAAM,IAAI,GAAG,KAAK,CAAC,IAAI,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC,CAAC;YAC7D,OAAO,IAAI,CAAC,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC;QAC5C,CAAC;KACF,CAAC;AACJ,CAAC"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../../../packages/rules-engine/src/lib/runners/index.ts"],"names":[],"mappings":";;;AAAA,gDAAsB;AACtB,uDAA6B"}
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes