rc-from-msg.js
1.15 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
"use strict";
function logOnDebug(msg) {
if (process.env.KOMODO_SDK_DEBUG_RC_FROM_MSG) {
console.log(msg);
}
}
function run(msg, rules) {
if (typeof msg !== 'string') {
logOnDebug('RC-FROM-MSG: invalid msg type === ' + typeof msg);
return;
}
if (!rules) {
logOnDebug('RC-FROM-MSG: invalid rules');
return;
}
if (!rules.length) {
logOnDebug('RC-FROM-MSG: rules is empty');
return;
}
const rules_count = rules.length;
for(let i = 0; i < rules_count; i++) {
const rule = rules[i];
if (typeof rule.pattern !== 'string') {
continue;
}
if (typeof rule.rc !== 'string' && typeof rule.result !== 'string') {
continue;
}
logOnDebug('RC-FROM-MSG: checking with rule: ' + JSON.stringify(rule));
const re = (typeof rule.flags === 'string') ? new RegExp(rule.pattern, rule.flags) : new RegExp(rule.pattern);
if (msg.search(re) >= 0) {
logOnDebug('RC-FROM-MSG: match with rule: ' + JSON.stringify(rule));
return rule.rc || rule.result;
}
}
}
module.exports = run;