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

js/lib/parseUrl.js   A

Complexity

Total Complexity 21
Complexity/F 21

Size

Lines of Code 68
Function Count 1

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 0
c 1
b 1
f 0
nc 1
dl 0
loc 68
rs 10
wmc 21
mnd 3
bc 18
fnc 1
bpm 18
cpm 21
noi 0
1
function processURL(URL, ignoreProtocol, ignoreSubdomain, ignorePath, ignorePort) {
2
    if (URL === null || URL === "") {
3
        return URL;
4
    }
5
6
    var parser = document.createElement('a');
7
    parser.href = URL;
8
9
10
    var protocol = parser.protocol;
11
    var host = parser.hostname;
12
    var path = parser.pathname;
13
    var port = parser.port;
14
    if (host === null || host === "") {
15
        return URL;
16
    }
17
18
    var splittedURL = host.split(".");
19
    var isIP = false;
20
    if (splittedURL.length === 4) {
21
        isIP = true;
22
        for (var i = 0; i < splittedURL.length; i++) {
23
            if (isNaN(splittedURL[i]) || splittedURL[i] < 0 || splittedURL[i] > 255) {
24
                isIP = false;
25
                break;
26
            }
27
        }
28
    }
29
    var baseHost = null;
30
    if (isIP) {
31
        baseHost = host;
32
    }
33
    else {
34
        var tld = parse_host(host);
35
        if(tld) {
36
            baseHost = tld.domain;
37
        }
38
    }
39
    var returnURL = "";
40
    if (!ignoreProtocol) {
41
        returnURL += protocol + "//";
42
    }
43
44
    if (!ignoreSubdomain) {
45
        returnURL += host;
46
    }
47
    else {
48
        returnURL += baseHost;//return the hostname and the tld of the website if ignoreSubdomain is check
49
    }
50
51
    if (ignorePort) {
52
        if (port) {
53
            returnURL = returnURL.replace(':' + port, '');
54
        }
55
    } else {
56
        if (port) {
57
            returnURL += ':' + port;
58
        }
59
    }
60
61
    if (!ignorePath && path !== null && path) {
62
        returnURL += path;
63
    }
64
    if (returnURL.slice(-1) === "/") {
65
        returnURL = returnURL.slice(0, -1);
66
    }
67
    return returnURL;
68
}
69