-
Notifications
You must be signed in to change notification settings - Fork 490
/
Copy path01-hello-world.mts
62 lines (54 loc) · 1.28 KB
/
01-hello-world.mts
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
/*
* This is the hello-world example from the README.
*
* Usage:
* node ./examples/01-hello-world.js
*
* For detailed output:
* DEBUG=json-rules-engine node ./examples/01-hello-world.js
*/
import "colors";
import { Engine } from "json-rules-engine";
async function start() {
/**
* Setup a new engine
*/
const engine = new Engine();
/**
* Create a rule
*/
engine.addRule({
// define the 'conditions' for when "hello world" should display
conditions: {
all: [
{
fact: "displayMessage",
operator: "equal",
value: true,
},
],
},
// define the 'event' that will fire when the condition evaluates truthy
event: {
type: "message",
params: {
data: "hello-world!",
},
},
});
/**
* Define a 'displayMessage' as a constant value
* Fact values do NOT need to be known at engine runtime; see the
* 03-dynamic-facts.js example for how to pull in data asynchronously during runtime
*/
const facts = { displayMessage: true };
// engine.run() evaluates the rule using the facts provided
const { events } = await engine.run(facts);
events.map((event) => console.log(event.params!.data.green));
}
export default start();
/*
* OUTPUT:
*
* hello-world!
*/