-
Notifications
You must be signed in to change notification settings - Fork 0
/
RuleCollection.ts
53 lines (46 loc) · 1.31 KB
/
RuleCollection.ts
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
///<reference path="Rule.ts"/>
///<reference path="ErrorRule.ts"/>
/**
* Created by Evan on 10/17/2016.
*/
class RuleCollection{
rules: Rule[] = [];
public getObjName(obj): string{
return Object.prototype.toString.call(obj).slice(8, -1);
}
public findRuleFor(token: string): Rule{
console.log("Inside findRuleFor");
var result: Rule = null;
for(var rule of this.rules){
console.log("Inside checkToken");
var isClear: boolean = rule.checkToken(token);
if(isClear){
if(result != null){
this.throwDuplicateRuleWarning(token);
}
result = rule;
console.log("Chosen: " + this.getObjName(result) + "\n");
}
else{
console.log("Tried: " + this.getObjName(result) + "\n");
}
}
if(result == null){
return new ErrorRule();
}
else{
return result;
}
}
private throwDuplicateRuleWarning(token: string) {
console.log("Duplicate rule warning at: " + token);
}
public add(rule: Rule): void{
this.rules.push(rule);
}
public addAll(rules: Rule[]): void{
for(var rule of rules){
this.add(rule);
}
}
}