-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
87 lines (70 loc) · 2.5 KB
/
index.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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
var URLUtils = (function(){
var Constructor = function(url) {
if(url.constructor == Constructor) {
this.__copy(url);
} else {
this.__parse(url);
}
};
Constructor.prototype = {
// RegExp author cite: http://stackoverflow.com/a/11976301
__urlRegex: /(?:([^\:]*)\:\/\/)?(?:([^\:\@]*)(?:\:([^\@]*))?\@)?(?:([^\/\:]*)\.(?=[^\.\/\:]*\.[^\.\/\:]*))?([^\.\/\:]*)(?:\.([^\/\.\:]*))?(?:\:([0-9]*))?(\/[^\?#]*(?=.*?\/)\/)?([^\?#]*)?(?:\?([^#]*))?(?:#(.*))?/,
hash: "",
host: "",
hostname: "",
href: "",
origin: "",
password: "",
pathname: "",
port: "",
protocol: "",
search: "",
username: "",
__copy: function(oldConstructor) {
for(var x in oldConstructor) {
this[x] = oldConstructor[x];
}
},
__parse: function(urlStr) {
var matches = urlStr.match(this.__urlRegex);
// 1 2 3 4 5 6 7 8 9 10 11
// ["ftp", "user", "pass", "www.cs", "server", "com", "8080", "/dir1/dir2/", "file.php", "param1=value1", "hashtag"]
// ftp://user:[email protected]:8080/dir1/dir2/file.php?param1=value1#hashtag
this.protocol = matches[1] + ":";
var hostNameArr = [];
if(matches[4]) hostNameArr.push(matches[4]);
if(matches[5]) hostNameArr.push(matches[5]);
if(matches[6]) hostNameArr.push(matches[6]);
this.hostname = hostNameArr.join('.');
this.port = matches[7] || "";
this.host = this.hostname + (
this.port ?
":"+this.port
:
""
);
this.origin = this.protocol + "//" + this.host;
this.username = matches[2];
this.password = matches[3];
this.pathname = (matches[8] || "") + (matches[9] || "");
this.search = (
matches[10] ?
"?" + matches[10]
:
""
);
this.hash = (
matches[11] ?
"#" + matches[11]
:
""
);
this.href = matches[0];
},
toString: function(){
return this.href;
}
};
return Constructor;
})();
module.exports = URLUtils;