-
Notifications
You must be signed in to change notification settings - Fork 511
Expand file tree
/
Copy path12-using-custom-almanac.mts
More file actions
104 lines (91 loc) · 2.45 KB
/
12-using-custom-almanac.mts
File metadata and controls
104 lines (91 loc) · 2.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import "colors";
import { Almanac, Engine } from "json-rules-engine";
type Pipe<T> = (value: T) => unknown;
/**
* Almanac that support piping values through named functions
*/
class PipedAlmanac extends Almanac {
pipes = new Map<string, Pipe<unknown>>();
addPipe<T>(name: string, pipe: Pipe<T>) {
this.pipes.set(name, pipe as Pipe<unknown>);
}
async factValue<T>(
factId: string,
params?: Record<string, unknown>,
path?: string,
) {
let pipes: string[] = [];
if (params && "pipes" in params && Array.isArray(params.pipes)) {
pipes = params.pipes;
delete params.pipes;
}
const value = await super.factValue(factId, params, path);
return pipes.reduce((value, pipeName) => {
const pipe = this.pipes.get(pipeName);
if (pipe) {
return pipe(value);
}
return value;
}, value) as T;
}
}
async function start() {
const engine = new Engine().addRule({
conditions: {
all: [
{
fact: "age",
params: {
// the addOne pipe adds one to the value
pipes: ["addOne"],
},
operator: "greaterThanInclusive",
value: 21,
},
],
},
event: {
type: "Over 21(ish)",
},
});
engine.on("success", async (event, almanac) => {
const name = await almanac.factValue("name");
const age = await almanac.factValue("age");
console.log(`${name} is ${age} years old and ${"is".green} ${event.type}`);
});
engine.on("failure", async (event, almanac) => {
const name = await almanac.factValue("name");
const age = await almanac.factValue("age");
console.log(
`${name} is ${age} years old and ${"is not".red} ${event.type}`,
);
});
const createAlmanacWithPipes = () => {
const almanac = new PipedAlmanac();
almanac.addPipe("addOne", (v: number) => v + 1);
return almanac;
};
// first run Bob who is less than 20
await engine.run(
{ name: "Bob", age: 19 },
{ almanac: createAlmanacWithPipes() },
);
// second run Alice who is 21
await engine.run(
{ name: "Alice", age: 21 },
{ almanac: createAlmanacWithPipes() },
);
// third run Chad who is 20
await engine.run(
{ name: "Chad", age: 20 },
{ almanac: createAlmanacWithPipes() },
);
}
export default start();
/*
* OUTPUT:
*
* Bob is 19 years old and is not Over 21(ish)
* Alice is 21 years old and is Over 21(ish)
* Chad is 20 years old and is Over 21(ish)
*/