-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathqurl.js
303 lines (239 loc) · 7.26 KB
/
qurl.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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
(function () {
'use strict';
var toString = Function.prototype.call.bind(Object.prototype.toString);
function Qurl(options) {
options = options || {};
if (!(this instanceof Qurl)) {
return new Qurl(options);
}
this.updateHistory = options.updateHistory === void 0 ? true : options.updateHistory;
this.isStateReplaced = options.isStateReplaced;
this.getHistoryModifyMethod = function () {
var modifyMethod = this.isStateReplaced ? history.replaceState : history.pushState;
return modifyMethod && modifyMethod.bind(history);
};
this.getQueryString = function () {
return location.search;
};
}
Qurl.prototype.go = function (url, state, title) {
var modifyMethod = this.getHistoryModifyMethod();
if (modifyMethod) {
modifyMethod(state || null, title || '', '?' + url);
}
};
Qurl.prototype.isHistoryApiSupported = function () {
return !!this.getHistoryModifyMethod();
};
Qurl.prototype.query = function (key, value, override) {
var queryString = getQueryString(),
params = getParams(queryString),
typeofKey = typeof key,
typeofValue = typeof value;
if (!key) {
return params;
}
if (typeofKey === 'string') {
if (typeofValue === 'undefined') {
return getParamValue(params, key);
}
queryString = setParamValue(params, key, value);
} else if (typeofKey === 'object') {
queryString = setParamsStringFromObject(params, key, override);
}
if (queryString && this.updateHistory) {
this.go(queryString);
}
return params[key];
};
Qurl.prototype.scopeTo = function (key) {
return this.query.bind(this, key);
};
Qurl.prototype.remove = function (keys) {
var i,
max,
params,
queryString = getQueryString();
keys = toString(keys) === '[object Array]' ? keys : [keys];
params = getParams(queryString);
for (i = 0, max = keys.length; i < max; i += 1) {
delete params[keys[i]];
}
queryString = setParamsStringFromObject(params);
if (this.updateHistory) {
this.go(queryString);
}
};
Qurl.prototype.removeAll = function () {
this.queryString = setParamsStringFromObject({}, true);
if (this.updateHistory) {
this.go(this.queryString);
}
};
Qurl.prototype.toString = function () {
var queryString = getQueryString(),
params = getParams(queryString);
return getParamStringFromObject(params);
};
function getQueryString() {
return location.search;
}
function setParamValue(params, key, value) {
params[key] = value;
return setParamsStringFromObject(params);
}
function getParamValue(params, key) {
return params[key];
}
function setParamsStringFromObject(params, newParamsObj, override) {
var mergedParamsObj = newParamsObj;
if (!override) {
mergedParamsObj = mergeObjects(params, newParamsObj, true);
}
return getParamStringFromObject(mergedParamsObj);
}
function getParamStringFromObject(paramsObj) {
var prop,
part,
max,
value,
joinedKeys,
parts = [],
i = 0,
values = [];
if (toString(paramsObj) !== '[object Object]') {
throw new TypeError('Invalid arguments supplied, paramsObj must be an object.');
}
for (prop in paramsObj) {
if (!paramsObj.hasOwnProperty(prop)) {
continue;
}
traverseProperty(prop, paramsObj[prop]);
}
for (max = parts.length; i < max; i += 1) {
part = parts[i];
joinedKeys = part.keys.join('.');
value = encodeURIComponent(joinedKeys) + '=' + encodeURIComponent(part.value);
values.push(value);
}
return values.join('&');
function traverse(obj, keyChain) {
var prop,
value,
max,
i,
name,
typeOfValue = toString(obj);
if (typeOfValue === '[object Array]') {
for (i = 0, max = obj.length; i < max; i += 1) {
value = obj[i];
name = '[' + i + ']';
traverseProperty(name, value, keyChain, true);
}
} else if (typeOfValue === '[object Object]') {
for (prop in obj) {
if (!obj.hasOwnProperty(prop)) {
continue;
}
value = obj[prop];
name = prop;
traverseProperty(name, value, keyChain);
}
}
}
function traverseProperty(propertyName, propertyValue, keyChain, appendName) {
if (appendName) {
keyChain = [].concat(keyChain);
keyChain[keyChain.length - 1] += propertyName;
} else {
keyChain = keyChain ? [].concat(keyChain || [], propertyName) : [propertyName];
}
if (typeof propertyValue === 'object') {
traverse(propertyValue, keyChain);
} else {
parts.push({
keys: keyChain,
value: propertyValue
});
}
}
}
function getParams(queryString) {
var max,
i,
parameterParts,
keyParts,
value,
decodedParameter,
valueAsOriginalType,
parameters = (queryString[0] === '?' ? queryString.substr(1) : queryString).split('&'),
params = {};
for (i = 0, max = parameters.length; i < max; i += 1) {
decodedParameter = decodeURIComponent(parameters[i]);
if (!decodedParameter) {
continue;
}
parameterParts = decodedParameter.split('=');
keyParts = parameterParts[0].split('.');
value = parameterParts[1];
valueAsOriginalType = toOriginalType(value);
processKeyParts(keyParts, valueAsOriginalType);
}
return params;
function processKeyParts(keyParts, value, constructedParam) {
var keyArrayIndex,
keyPartValue,
finalPart,
keyPart = keyParts.shift(),
keyNameParts = keyPart.split('['),
keyArrayIndexPart = keyNameParts[1],
keyNamePart = keyNameParts[0];
if (keyArrayIndexPart) {
keyArrayIndex = keyArrayIndexPart.slice(0, -1);
keyParts = [].concat(keyArrayIndex, keyParts);
}
finalPart = !keyParts.length;
keyPartValue = finalPart ? value || null : keyArrayIndexPart ? [] : {};
constructedParam =
constructedParam || params[keyNamePart] || (params[keyNamePart] = keyPartValue);
if (
constructedParam !== null &&
typeof constructedParam === 'object' &&
constructedParam !== params[keyNamePart]
) {
constructedParam =
constructedParam[keyNamePart] || (constructedParam[keyNamePart] = keyPartValue);
}
if (!finalPart) {
processKeyParts(keyParts, value, constructedParam);
}
}
}
function mergeObjects(obj, objToMerge, override) {
var prop;
for (prop in objToMerge) {
if (!objToMerge.hasOwnProperty(prop)) {
continue;
}
if (obj[prop] && !override) {
continue;
}
obj[prop] = objToMerge[prop];
}
return obj;
}
function toOriginalType(s) {
return s === 'true' ? true : s === 'false' ? false : !isNaN(s) ? +s : s;
}
if (typeof module !== 'undefined' && typeof module.exports !== 'undefined') {
module.exports = Qurl;
} else {
if (typeof define === 'function' && define.amd) {
define([], function () {
return Qurl;
});
} else {
window.Qurl = Qurl;
}
}
})();