-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflow.js
54 lines (45 loc) · 1.29 KB
/
flow.js
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
// Used for console log colors
const chalk = require('chalk');
/**
* @id {Number} id of the searched rule
* @rules {Array} set of rules
*/
function getRuleById(id, rules) {
return rules.filter(function(item) {
return item.id === id
})[0];
}
/**
* @rule {Object} rule to by applied
* @object {Object} the object to run the rules by
* @rules {Array} set of rules
*/
function applyRule(rule, object, rules) {
const ruleResult = eval("(" + rule.body + ")").call(null, object);
const log = {
color: ruleResult ? chalk.bold.green : chalk.bold.red,
text : ruleResult ? 'passed' : 'failed',
}
const nextRuleId = ruleResult ? rule.true_id : rule.false_id;
// Logg rule result
console.log(log.color('Rule #' + rule.id + ' -> ' + log.text));
// End flow if next step is null
if (nextRuleId === null) {
console.log(chalk.bgRed('THE END'));
return false;
}
// Next flow rule
applyRule(getRuleById(nextRuleId, rules), object, rules);
}
/**
* flowEngine - executes a flow consisting of several linked rules
*
* @rules {Array} set of rules
* @object {Object} the object to run the rules by
*/
function flowEngine(rules, object) {
// Start flow engine
applyRule(rules[0], object, rules);
}
exports.flowEngine = flowEngine;
exports.getRuleById = getRuleById;