-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpsl_util.js
33 lines (31 loc) · 1006 Bytes
/
psl_util.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
const WILDCARD = "*";
const VALUE_KEY = "!";
var exceptionRuleFlag = false;
var bestIndex = 0;
function getBaseDomain(domain) {
exceptionRuleFlag = false;
let domainArray = domain.split(".");
bestIndex = domainArray.length-1;
traverse(PSL_TREE, domainArray.length-1, domainArray);
bestIndex = bestIndex < 0 ? 0 : bestIndex;
return domainArray.slice(bestIndex, domainArray.length).join(".");
}
function traverse(tree, index, domainArray) {
if(tree[VALUE_KEY] === 1){
bestIndex = Math.min(bestIndex, index);
}
else if(tree[VALUE_KEY] === 2) {
bestIndex = index + 1;
exceptionRuleFlag = true;
return;
}
if(index >= 0) {
let label = domainArray[index];
if (!exceptionRuleFlag && tree[label]) {
traverse(tree[label], index - 1, domainArray);
}
if (!exceptionRuleFlag && tree[WILDCARD] && label !== WILDCARD) {
traverse(tree[WILDCARD], index - 1, domainArray);
}
}
}