-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathqueryString.js
123 lines (104 loc) · 3.55 KB
/
queryString.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
/*!
query-string
Parse and stringify URL query strings
https://github.com/sindresorhus/query-string
by Sindre Sorhus
MIT License
Some modifications by Jacob Fischer
*/
(function () {
'use strict';
var queryString = {};
queryString.parse = function (str) {
if (typeof str !== 'string') {
return {};
}
str = str.trim().replace(/^\?/, '');
if (!str) {
return {};
}
return str.trim().split('&').reduce(function (ret, param) {
var parts = param.replace(/\+/g, ' ').split('=');
var key = parts[0];
var val = parts[1];
key = decodeURIComponent(key);
// missing `=` should be `null`:
// http://w3.org/TR/2012/WD-url-20120524/#collect-url-parameters
val = val === undefined ? null : decodeURIComponent(val);
if (!ret.hasOwnProperty(key)) {
ret[key] = val;
} else if (Array.isArray(ret[key])) {
ret[key].push(val);
} else {
ret[key] = [ret[key], val];
}
return ret;
}, {});
};
queryString.stringify = function (obj) {
return obj ? Object.keys(obj).map(function (key) {
var val = obj[key];
if (Array.isArray(val)) {
return val.map(function (val2) {
return encodeURIComponent(key) + '=' + encodeURIComponent(val2);
}).join('&');
}
return encodeURIComponent(key) + '=' + encodeURIComponent(val);
}).join('&') : '';
};
queryString.setAll = function(key_values) {
var params = queryString.parse(location.search);
var changed = false;
for(var key in key_values) {
if(key_values.hasOwnProperty(key)) {
var value = key_values[key];
if(params[key] !== value) {
changed = true;
}
if(value === undefined) { // delete the key
delete params[key];
}
else {
params[key] = key_values[key];
}
}
}
if(changed) {
var new_params_string = queryString.stringify(params);
history.pushState({}, "", window.location.pathname + '?' + new_params_string);
}
};
queryString.getAll = function() {
return queryString.parse(location.search);
};
queryString.clear = function() {
var params = queryString.getAll();
for(var key in params) {
params[key] = undefined;
}
queryString.setAll(params);
};
queryString.set = function (key, new_value) {
var params = queryString.parse(location.search);
if(params[key] === new_value) {
return;
}
params[key] = new_value;
var new_params_string = queryString.stringify(params)
history.pushState({}, "", window.location.pathname + '?' + new_params_string);
};
queryString.remove = function (key) {
var params = queryString.parse(location.search);
if(!params.hasOwnProperty(key)) {
return;
}
delete params[key]
var new_params_string = queryString.stringify(params)
history.pushState({}, "", window.location.pathname + '?' + new_params_string);
};
if (typeof module !== 'undefined' && module.exports) {
module.exports = queryString;
} else {
window.queryString = queryString;
}
})();