Привет, гуру js программирования!
Как можно сократить этот код? )
$(document).ready(function() {
var url_string = window.location.href;
function get_params_string() {
var arr = url_string.split('?');
var get_params = null;
if (url_string.length > 1 && arr[1] !== '') {
get_params = arr[1];
}
return get_params;
}
var url = new URL(url_string);
var sort_params = [
url.searchParams.get("start-date-sort"),
url.searchParams.get("end-date-sort"),
url.searchParams.get("start-sum-sort"),
url.searchParams.get("end-sum-sort")
];
console.log(get_params_string());
if (get_params_string() === undefined || get_params_string() === null) {
$('#start-date-sort').attr("href","?start-date-sort=DESC");
$('#end-date-sort').attr("href","?end-date-sort=DESC");
$('#start-sum-sort').attr("href","?start-sum-sort=DESC");
$('#end-sum-sort').attr("href","?start-date-sort=DESC");
} else {
re = /[a-z-]*-sort=(desc|asc)/gi;
str = get_params_string().replace(re, '');
if (sort_params[0] !== null) {
if (sort_params[0] === 'DESC') {
$('#start-date-sort').attr("href",'?'+str+'&start-date-sort=ASC');
} else if (sort_params[0] === 'ASC') {
$('#start-date-sort').attr("href",'?'+str+'&start-date-sort=DESC');
}
} else if (sort_params[0] === null || sort_params[0] === undefined) {
$('#start-date-sort').attr("href",'?'+str+'&start-date-sort=DESC');
}
if (sort_params[1] !== null) {
if (sort_params[1] === 'DESC') {
$('#end-date-sort').attr("href",'?'+str+'&end-date-sort=ASC');
} else if (sort_params[1] === 'ASC') {
$('#end-date-sort').attr("href",'?'+str+'&end-date-sort=DESC');
}
} else if (sort_params[1] === null || sort_params[1] === undefined) {
$('#end-date-sort').attr("href",'?'+str+'&end-date-sort=DESC');
}
if (sort_params[2] !== null) {
if (sort_params[2] === 'DESC') {
$('#start-sum-sort').attr("href",'?'+str+'&start-sum-sort=ASC');
} else if (sort_params[2] === 'ASC') {
$('#start-sum-sort').attr("href",'?'+str+'&start-sum-sort=DESC');
}
} else if (sort_params[2] === null || sort_params[2] === undefined) {
$('#start-sum-sort').attr("href",'?'+str+'&start-sum-sort=DESC');
}
if (sort_params[3] !== null) {
if (sort_params[3] === 'DESC') {
$('#end-sum-sort').attr("href",'?'+str+'&end-sum-sort=ASC');
} else if (sort_params[3] === 'ASC') {
$('#end-sum-sort').attr("href",'?'+str+'&end-sum-sort=DESC');
}
} else if (sort_params[3] === null || sort_params[3] === undefined) {
$('#end-sum-sort').attr("href",'?'+str+'&end-sum-sort=DESC');
}
}
});