-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuri-manage-0.1.js
76 lines (68 loc) · 1.92 KB
/
uri-manage-0.1.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
/*
* Url Manage
* Version 0.1
* Parses and recombine parts of URLs
*
* MIT License
*
* Author: Igor Golodnitsky
* Author email: [email protected]
*
*
* Based on: uri parse by Steven Levithan
* =====================================================
* Examples:
* =====================================================
* Take any string where your url stayed
* var url = 'http://www.google.com'.url() // -> returns Url object
*
* url.attr('protocol', 'https'); // parts of url change
* url.param('id', 25); // query string change params
* url.param({id: 25, name: 'xuxel'}); // extend current params with object
*/
String.prototype.url = function() {
return new Uri(parseUri(this));
}
var Uri = function(parsed) {
this.parsed = parsed;
}
Uri.prototype = {
attr: function(name, value) {
return this.getset(this.parsed, name, value);
},
param: function(name, value) {
return this.getset(this.parsed.queryKey, name, value);
},
getset: function(inner, name, value) {
if (typeof (name) === 'object') {
// like extend
for (var key in name) {
inner[key] = name[key];
}
}
else {
if (value) {
// set
inner[name] = value;
}
else {
// get
return inner[name];
}
}
this.reload();
return this;
},
reload: function() {
this.parsed = parseUri(this.string());
},
string: function() {
var o = this.parsed;
var qs = '';
for (var key in o.queryKey) {
qs += key + '=' + o.queryKey[key] + '&';
}
qs = qs.slice(0, qs.length - 1);
return o.protocol + '://' + o.host + (o.port ? ':' + o.port : '') + o.path + '?' + qs;
}
}