-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathurl.js
63 lines (50 loc) · 1.6 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
/**
* Patterns URL - URL parsing utilities
*
* Copyright 2013 Simplon B.V.
*/
function UrlArgumentParser() {
this._cache = null;
if (window.addEventListener) window.addEventListener("popstate", this._reset);
}
UrlArgumentParser.prototype = {
space_pattern: /\+/g,
keyvalue_pattern: /^(.+?)(?:=(.*))/,
_reset: function UrlArgumentParser_reset() {
this._cache = null;
},
_decodeQS: function UrlArgumentParser_decodeQS(bit) {
return decodeURIComponent(bit.replace(this.space_pattern, " "));
},
_parse: function UrlArgumentParser_parse(qs) {
var query = /\?(.+)/.exec(qs),
params = {};
if (query === null) return params;
var parameters = query[1].split("&"),
i,
parts,
key,
value;
for (i = 0; i < parameters.length; i++) {
if ((parts = this.keyvalue_pattern.exec(parameters[i])) === null) {
key = this._decodeQS(parameters[i]);
value = null;
} else {
key = this._decodeQS(parts[1]);
value = this._decodeQS(parts[2]);
}
if (params[key] === undefined) params[key] = [];
params[key].push(value);
}
return params;
},
get: function UrlArgumentParser_get() {
if (this._cache === null) this._cache = this._parse(window.location.search);
return this._cache;
},
};
var url_parser = new UrlArgumentParser();
export default {
UrlArgumentParser: UrlArgumentParser,
parameters: url_parser.get.bind(url_parser),
};