@clipboard-health/rules-engine 1.16.5 → 1.16.7
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 +12 -0
- package/package.json +2 -2
- package/src/lib/runners/allIf.d.ts +16 -0
- package/src/lib/runners/allIf.js +26 -0
- package/src/lib/runners/allIf.js.map +1 -0
- package/src/lib/runners/index.d.ts +1 -0
- package/src/lib/runners/index.js +1 -0
- package/src/lib/runners/index.js.map +1 -1
- package/src/lib/runners/allIfFirst.d.ts +0 -7
- package/src/lib/runners/allIfFirst.js +0 -17
- package/src/lib/runners/allIfFirst.js.map +0 -1
package/README.md
CHANGED
|
@@ -25,6 +25,7 @@ import { deepEqual } from "node:assert/strict";
|
|
|
25
25
|
|
|
26
26
|
import {
|
|
27
27
|
all,
|
|
28
|
+
allIf,
|
|
28
29
|
appendOutput,
|
|
29
30
|
firstMatch,
|
|
30
31
|
type Rule,
|
|
@@ -89,6 +90,17 @@ const firstMatchResult = firstMatch(
|
|
|
89
90
|
).run(exampleContext);
|
|
90
91
|
|
|
91
92
|
deepEqual(firstMatchResult.output, [{ result: 7 }]);
|
|
93
|
+
|
|
94
|
+
// Using allIf() applies all the rules that return true for `runIf` to the context when the predicate
|
|
95
|
+
// (a function received as firs argument) returns true
|
|
96
|
+
const allIfResult = allIf(
|
|
97
|
+
(input) => input.a === 2,
|
|
98
|
+
divideNumbersIfNegative,
|
|
99
|
+
addNumbersIfPositiveRule,
|
|
100
|
+
multiplyNumbersIfPositiveRule,
|
|
101
|
+
).run(exampleContext);
|
|
102
|
+
|
|
103
|
+
deepEqual(allIfResult.output, [{ result: 7 }, { result: 10 }]);
|
|
92
104
|
```
|
|
93
105
|
|
|
94
106
|
</embedex>
|
package/package.json
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
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.16.
|
|
4
|
+
"version": "1.16.7",
|
|
5
5
|
"bugs": "https://github.com/ClipboardHealth/core-utils/issues",
|
|
6
6
|
"dependencies": {
|
|
7
7
|
"tslib": "2.8.0",
|
|
8
|
-
"type-fest": "4.
|
|
8
|
+
"type-fest": "4.33.0"
|
|
9
9
|
},
|
|
10
10
|
"keywords": [
|
|
11
11
|
"engine",
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { type Rule, type RuleContext } from "../rule";
|
|
2
|
+
/**
|
|
3
|
+
* Run all rules that return true for their runIf condition, but only when the predicate function returns true.
|
|
4
|
+
*
|
|
5
|
+
* @param allIfPredicate - Function that determines if rules should be evaluated
|
|
6
|
+
* @param rules - Array of rules to evaluate when predicate is true
|
|
7
|
+
* @returns A Rule that combines the behavior of all matching rules
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* const rule = allIf(
|
|
11
|
+
* (input) => input.type === 'special',
|
|
12
|
+
* rule1,
|
|
13
|
+
* rule2
|
|
14
|
+
* );
|
|
15
|
+
*/
|
|
16
|
+
export declare function allIf<TInput, TOutput, TContext extends RuleContext<TInput, TOutput>>(allIfPredicate: (input: RuleContext<TInput, TOutput>["input"]) => boolean, ...rules: Array<Rule<TInput, TOutput, TContext>>): Rule<TInput, TOutput, TContext>;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.allIf = allIf;
|
|
4
|
+
/**
|
|
5
|
+
* Run all rules that return true for their runIf condition, but only when the predicate function returns true.
|
|
6
|
+
*
|
|
7
|
+
* @param allIfPredicate - Function that determines if rules should be evaluated
|
|
8
|
+
* @param rules - Array of rules to evaluate when predicate is true
|
|
9
|
+
* @returns A Rule that combines the behavior of all matching rules
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* const rule = allIf(
|
|
13
|
+
* (input) => input.type === 'special',
|
|
14
|
+
* rule1,
|
|
15
|
+
* rule2
|
|
16
|
+
* );
|
|
17
|
+
*/
|
|
18
|
+
function allIf(allIfPredicate, ...rules) {
|
|
19
|
+
return {
|
|
20
|
+
runIf: (input) => allIfPredicate(input),
|
|
21
|
+
run: (context) => rules
|
|
22
|
+
.filter((rule) => rule.runIf(context.input))
|
|
23
|
+
.reduce((previousContext, rule) => rule.run(previousContext), context),
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
//# sourceMappingURL=allIf.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"allIf.js","sourceRoot":"","sources":["../../../../../../packages/rules-engine/src/lib/runners/allIf.ts"],"names":[],"mappings":";;AAgBA,sBAWC;AAzBD;;;;;;;;;;;;;GAaG;AACH,SAAgB,KAAK,CACnB,cAAyE,EACzE,GAAG,KAA6C;IAEhD,OAAO;QACL,KAAK,EAAE,CAAC,KAAK,EAAE,EAAE,CAAC,cAAc,CAAC,KAAK,CAAC;QACvC,GAAG,EAAE,CAAC,OAAiB,EAAE,EAAE,CACzB,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"}
|
package/src/lib/runners/index.js
CHANGED
|
@@ -2,5 +2,6 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
const tslib_1 = require("tslib");
|
|
4
4
|
tslib_1.__exportStar(require("./all"), exports);
|
|
5
|
+
tslib_1.__exportStar(require("./allIf"), exports);
|
|
5
6
|
tslib_1.__exportStar(require("./firstMatch"), exports);
|
|
6
7
|
//# sourceMappingURL=index.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../../../packages/rules-engine/src/lib/runners/index.ts"],"names":[],"mappings":";;;AAAA,gDAAsB;AACtB,uDAA6B"}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../../../packages/rules-engine/src/lib/runners/index.ts"],"names":[],"mappings":";;;AAAA,gDAAsB;AACtB,kDAAwB;AACxB,uDAA6B"}
|
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
import { type Rule, type RuleContext } from "../rule";
|
|
2
|
-
/**
|
|
3
|
-
* Run all rules that return true for `runIf` only if the first rule returns true.
|
|
4
|
-
*
|
|
5
|
-
* @param rules The rules to run.
|
|
6
|
-
*/
|
|
7
|
-
export declare function allIfFirst<TInput, TOutput, TContext extends RuleContext<TInput, TOutput>>(...rules: Array<Rule<TInput, TOutput, TContext>>): Rule<TInput, TOutput, TContext>;
|
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.allIfFirst = allIfFirst;
|
|
4
|
-
/**
|
|
5
|
-
* Run all rules that return true for `runIf` only if the first rule returns true.
|
|
6
|
-
*
|
|
7
|
-
* @param rules The rules to run.
|
|
8
|
-
*/
|
|
9
|
-
function allIfFirst(...rules) {
|
|
10
|
-
return {
|
|
11
|
-
runIf: (input) => Boolean(rules[0]?.runIf(input)),
|
|
12
|
-
run: (context) => rules
|
|
13
|
-
.filter((rule) => rule.runIf(context.input))
|
|
14
|
-
.reduce((previousContext, rule) => rule.run(previousContext), context),
|
|
15
|
-
};
|
|
16
|
-
}
|
|
17
|
-
//# sourceMappingURL=allIfFirst.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"allIfFirst.js","sourceRoot":"","sources":["../../../../../../packages/rules-engine/src/lib/runners/allIfFirst.ts"],"names":[],"mappings":";;AAOA,gCAUC;AAfD;;;;GAIG;AACH,SAAgB,UAAU,CACxB,GAAG,KAA6C;IAEhD,OAAO;QACL,KAAK,EAAE,CAAC,KAAK,EAAE,EAAE,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC,CAAC,EAAE,KAAK,CAAC,KAAK,CAAC,CAAC;QACjD,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"}
|