-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathquery.js
135 lines (113 loc) · 3.57 KB
/
query.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
(function (ns) {
"use strict";
function UrlQuery() {}
// Get query string parameter value.
UrlQuery.get = function (url, field, defaultValue, skipDecode) {
if (!url) {
return;
}
var re = new RegExp("[?&]" + field + "=([^&]*)");
var match = url.match(re);
if (match && match.length > 1) {
return skipDecode ? match[1] : decodeURIComponent(match[1]);
} else {
return defaultValue;
}
};
UrlQuery.getAll = function (url, skipDecode) {
if (!url) {
return {};
}
var query = {};
var qsIndex = url.indexOf("?");
if (url.length > 1 && qsIndex >= 0) {
var queryParts = url.substring(qsIndex + 1).split("&");
for (var i = 0; i < queryParts.length; i++) {
var param = queryParts[i].split("=");
if (param.length === 2) {
query[param[0]] = skipDecode ? param[1] : decodeURIComponent(param[1]);
}
}
}
return query;
};
// Add or update query string parameter from an url.
UrlQuery.update = function (url, field, value, skipEncode) {
if (url === undefined) {
return url;
}
var re = new RegExp("([?&])" + field + "=[^&]*");
var match = url.match(re);
if (match && match.length > 1) {
return url.replace(re, "$1" + field + "=" + (skipEncode ? value : encodeURIComponent(value)));
} else {
var separator = url.indexOf("?") > 0 ? "&": "?";
return url + separator + field + "=" + (skipEncode ? value : encodeURIComponent(value));
}
};
// Add or update multiple query string parameters from an url.
UrlQuery.updateAll = function (url, fields, values, skipEncode) {
if (url === undefined || !fields || !values) {
return url;
}
if (fields.length !== values.length) {
return url;
}
var updatedUrl = url;
for (var i = 0; i < fields.length; i++) {
updatedUrl = UrlQuery.update(updatedUrl, fields[i], values[i], skipEncode);
}
return updatedUrl;
};
// Remove query string parameter from an url.
UrlQuery.remove = function (url, field) {
if (url === undefined) {
return url;
}
// Check middle field
var re = new RegExp("[&]" + field + "=[^&]*");
if (url.match(re)) {
return url.replace(re, "");
}
// Check last field
re = new RegExp("[?]" + field + "=[^&]*$");
if (url.match(re)) {
return url.replace(re, "");
}
// Check first
re = new RegExp("[?]" + field + "=[^&]*&");
if (url.match(re)) {
return url.replace(re, "?");
}
return url;
}
// Remove multiple query string parameters from an url.
UrlQuery.removeAll = function (url, fields) {
if (url === undefined || !fields) {
return url;
}
var updatedUrl = url;
for (var i = 0; i < fields.length; i++) {
updatedUrl = UrlQuery.remove(updatedUrl, fields[i]);
}
return updatedUrl;
};
// In case we have AMD support and also need UrlQuery to be added to the window
if (typeof ns === "object") {
ns.UrlQuery = UrlQuery;
}
// AMD support - from https://gist.github.com/CrocoDillon/9990078
if (typeof define === "function" && define.amd) {
define(function () { return UrlQuery; });
// CommonJS and Node.js module support.
} else if (typeof exports !== "undefined") {
// Support Node.js specific `module.exports` (which can be a function)
if (typeof module !== "undefined" && module.exports) {
module.exports = UrlQuery;
}
// But always support CommonJS module 1.1.1 spec (`exports` cannot be a function)
exports.UrlQuery = UrlQuery;
} else {
ns.UrlQuery = UrlQuery;
}
})(this);