-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfilter.js
More file actions
43 lines (39 loc) · 1.48 KB
/
filter.js
File metadata and controls
43 lines (39 loc) · 1.48 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
var test = require("./test1.js");
console.time('test');
function filter(messages, rules){
var stack=[];
rules.forEach(function(element) {
if(!(element.from === undefined)){
element.from = element.from.replace(/\*/g,"(.)*");
element.from = new RegExp(element.from.replace(/\?/g,"(.)?"));
}
if(!(element.to === undefined)){
element.to = element.to.replace(/\*/g,"(.)*");
element.to = new RegExp(element.to.replace(/\?/g,"(.)?"));
}
}, this);
for(var msg in messages){
var result=[];
var match=true;
if(stack[messages[msg].from+messages[msg].to] === undefined){
rules.forEach(function(element){
if(!(element.from === undefined))
if(messages[msg].from.match(element.from)==null)
match=false;
if(!(element.to === undefined)&&(match))
if(messages[msg].to.match(element.to)==null)
match=false;
(match) ? result.push(element.action):null;
match=true;
}, this);
stack[messages[msg].from+messages[msg].to]=result;
}else{
result=stack[messages[msg].from+messages[msg].to];
}
messages[msg] = result;
}
return messages;
}
filter(test.a["messages"],test.a["rules"]);
console.timeEnd('test');
exports.filter=filter;