Completed
Pull Request — master (#77)
by Sander
01:21
created

js/lib/parseTLD.js   A

Complexity

Total Complexity 6
Complexity/F 6

Size

Lines of Code 31
Function Count 1

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 0
c 1
b 0
f 0
nc 1
dl 0
loc 31
rs 10
wmc 6
mnd 2
bc 6
fnc 1
bpm 6
cpm 6
noi 1
1
var parse_host = function(host){
2
    if(!tlds){
0 ignored issues
show
Best Practice introduced by
If you intend to check if the variable tlds is declared in the current environment, consider using typeof tlds === "undefined" instead. This is safe if the variable is not actually declared.
Loading history...
3
        throw new Error('No TLDs!');
4
    }
5
6
    var parts = host.split(".");
7
    var stack = "";
8
    var tld_level = 1; //unknown tld are 1st level
9
    for(var i=parts.length-1, part;i>=0;i--){
10
        part = parts[i];
11
        stack = stack ? part + "." + stack : part;
12
        if(!tlds[stack]){
13
            break;
14
        }
15
        tld_level = tlds[stack];
16
    }
17
    if(parts.length <= tld_level ) {
18
        return {
19
            tld: null,
20
            domain: host
21
        };
22
    } else {
23
        return  {
24
            tld     : parts.slice(-tld_level).join('.'),
25
            domain  : parts.slice(-tld_level-1).join('.'),
26
            sub     : parts.slice(0, (-tld_level-1)).join('.'),
27
        };
28
    }
29
30
31
};