-
-
Notifications
You must be signed in to change notification settings - Fork 156
/
Url.js
244 lines (219 loc) · 6.76 KB
/
Url.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
/* Simple url manipulator.
*
* ### constructor
*
* |Name |Desc |
* |------------|----------|
* |url=location|Url string|
*
* ### setQuery
*
* Set query value.
*
* |Name |Desc |
* |------|-----------|
* |name |Query name |
* |val |Query value|
* |return|this |
*
* |Name |Desc |
* |------|------------|
* |query |query object|
* |return|this |
*
* ### rmQuery
*
* Remove query value.
*
* |Name |Desc |
* |------|----------|
* |name |Query name|
* |return|this |
*
* ### parse
*
* [static] Parse url into an object.
*
* |Name |Desc |
* |------|----------|
* |url |Url string|
* |return|Url object|
*
* ### stringify
*
* [static] Stringify url object into a string.
*
* |Name |Desc |
* |------|----------|
* |url |Url object|
* |return|Url string|
*
* An url object contains the following properties:
*
* |Name |Desc |
* |--------|--------------------------------------------------------------------------------------|
* |protocol|The protocol scheme of the URL (e.g. http:) |
* |slashes |A boolean which indicates whether the protocol is followed by two forward slashes (//)|
* |auth |Authentication information portion (e.g. username:password) |
* |hostname|Host name without port number |
* |port |Optional port number |
* |pathname|URL path |
* |query |Parsed object containing query string |
* |hash |The "fragment" portion of the URL including the pound-sign (#) |
*/
/* example
* const url = new Url('http://example.com:8080?eruda=true');
* console.log(url.port); // -> '8080'
* url.query.foo = 'bar';
* url.rmQuery('eruda');
* url.toString(); // -> 'http://example.com:8080/?foo=bar'
*/
/* module
* env: all
*/
/* typescript
* export declare namespace Url {
* interface IUrl {
* protocol: string;
* auth: string;
* hostname: string;
* hash: string;
* query: any;
* port: string;
* pathname: string;
* slashes: boolean;
* }
* }
* export declare class Url {
* protocol: string;
* auth: string;
* hostname: string;
* hash: string;
* query: any;
* port: string;
* pathname: string;
* slashes: boolean;
* constructor(url?: string);
* setQuery(name: string, val: string | number): Url;
* setQuery(query: types.PlainObj<string | number>): Url;
* rmQuery(name: string | string[]): Url;
* toString(): string;
* static parse(url: string): Url.IUrl;
* static stringify(object: Url.IUrl): string;
* }
*/
_(
'Class extend trim query isEmpty each isArr toArr isBrowser isObj types toStr'
);
exports = Class(
{
className: 'Url',
initialize(url) {
if (!url && isBrowser) url = window.location.href;
extend(this, exports.parse(url || ''));
},
setQuery(name, val) {
const query = this.query;
if (isObj(name)) {
each(name, function(val, key) {
query[key] = toStr(val);
});
} else {
query[name] = toStr(val);
}
return this;
},
rmQuery(name) {
const query = this.query;
if (!isArr(name)) name = toArr(name);
each(name, function(key) {
delete query[key];
});
return this;
},
toString() {
return exports.stringify(this);
}
},
{
parse(url) {
const ret = {
protocol: '',
auth: '',
hostname: '',
hash: '',
query: {},
port: '',
pathname: '',
slashes: false
};
let rest = trim(url);
let slashes = false;
let proto = rest.match(regProto);
if (proto) {
proto = proto[0];
ret.protocol = proto.toLowerCase();
rest = rest.substr(proto.length);
}
if (proto) {
slashes = rest.substr(0, 2) === '//';
if (slashes) {
rest = rest.slice(2);
ret.slashes = true;
}
}
if (slashes) {
let host = rest;
let hostEnd = -1;
for (let i = 0, len = hostEndingChars.length; i < len; i++) {
const pos = rest.indexOf(hostEndingChars[i]);
if (pos !== -1 && (hostEnd === -1 || pos < hostEnd))
hostEnd = pos;
}
if (hostEnd > -1) {
host = rest.slice(0, hostEnd);
rest = rest.slice(hostEnd);
}
const atSign = host.lastIndexOf('@');
if (atSign !== -1) {
ret.auth = decodeURIComponent(host.slice(0, atSign));
host = host.slice(atSign + 1);
}
ret.hostname = host;
let port = host.match(regPort);
if (port) {
port = port[0];
if (port !== ':') ret.port = port.substr(1);
ret.hostname = host.substr(0, host.length - port.length);
}
}
const hash = rest.indexOf('#');
if (hash !== -1) {
ret.hash = rest.substr(hash);
rest = rest.slice(0, hash);
}
const queryMark = rest.indexOf('?');
if (queryMark !== -1) {
ret.query = query.parse(rest.substr(queryMark + 1));
rest = rest.slice(0, queryMark);
}
ret.pathname = rest || '/';
return ret;
},
stringify(obj) {
let ret =
obj.protocol +
(obj.slashes ? '//' : '') +
(obj.auth ? encodeURIComponent(obj.auth) + '@' : '') +
obj.hostname +
(obj.port ? ':' + obj.port : '') +
obj.pathname;
if (!isEmpty(obj.query)) ret += '?' + query.stringify(obj.query);
if (obj.hash) ret += obj.hash;
return ret;
}
}
);
const regProto = /^([a-z0-9.+-]+:)/i;
const regPort = /:[0-9]*$/;
const hostEndingChars = ['/', '?', '#'];