2017-05-23 10:46:52 +02:00
|
|
|
export function parseQueryParams(queryString) {
|
2017-12-21 18:32:51 +01:00
|
|
|
if (queryString === '') return {};
|
2017-11-24 15:31:05 +01:00
|
|
|
const parts = queryString
|
2017-12-21 18:32:51 +01:00
|
|
|
.split('?')
|
2017-11-24 15:31:05 +01:00
|
|
|
.pop()
|
2017-12-21 18:32:51 +01:00
|
|
|
.split('&')
|
|
|
|
.map(p => p.split('='));
|
2017-05-07 14:50:32 +02:00
|
|
|
|
|
|
|
const params = {};
|
2017-12-21 18:32:51 +01:00
|
|
|
parts.forEach(array => {
|
|
|
|
const [first, second] = array;
|
|
|
|
params[first] = second;
|
2017-06-06 23:19:12 +02:00
|
|
|
});
|
2017-05-07 14:50:32 +02:00
|
|
|
return params;
|
|
|
|
}
|
|
|
|
|
2017-05-23 10:46:52 +02:00
|
|
|
export function toQueryString(params) {
|
2017-12-21 18:32:51 +01:00
|
|
|
if (!params) return '';
|
2017-05-23 10:46:52 +02:00
|
|
|
|
2017-06-06 23:19:12 +02:00
|
|
|
const parts = [];
|
2017-12-21 18:32:51 +01:00
|
|
|
Object.keys(params).forEach(key => {
|
|
|
|
if (Object.prototype.hasOwnProperty.call(params, key) && params[key]) {
|
2017-12-13 22:36:30 +01:00
|
|
|
parts.push(`${key}=${params[key]}`);
|
2017-05-23 10:46:52 +02:00
|
|
|
}
|
2017-12-21 18:32:51 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
return parts.join('&');
|
2017-05-23 10:46:52 +02:00
|
|
|
}
|
2019-07-17 22:49:06 +02:00
|
|
|
|
2019-07-19 16:52:42 +02:00
|
|
|
// https://stackoverflow.com/questions/5999118/how-can-i-add-or-update-a-query-string-parameter
|
2019-07-17 22:49:06 +02:00
|
|
|
export function updateQueryParam(uri, key, value) {
|
|
|
|
const re = new RegExp('([?&])' + key + '=.*?(&|$)', 'i');
|
2019-10-10 07:23:42 +02:00
|
|
|
const separator = uri.includes('?') ? '&' : '?';
|
2019-07-17 22:49:06 +02:00
|
|
|
if (uri.match(re)) {
|
|
|
|
return uri.replace(re, '$1' + key + '=' + value + '$2');
|
|
|
|
} else {
|
|
|
|
return uri + separator + key + '=' + value;
|
|
|
|
}
|
|
|
|
}
|