Last active
September 21, 2021 09:11
-
-
Save TanmayChakrabarty/216e9af5b54ac49551434a40d40c230e to your computer and use it in GitHub Desktop.
add, replace, delete URL query parameters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//jsFiddle link: https://jsfiddle.net/tanmayszone/rvpbdc9u/ | |
function buildJsonFromUrlQuery(query){ | |
if(!query || typeof query !== 'string' || !query.length) return {}; | |
var partsArray = query.split('&'); | |
var partsObject = {}; | |
for(let i in partsArray){ | |
let thisPart = partsArray[i]; | |
let keyValue = thisPart.split('='); | |
let key = decodeURI(keyValue[0]); | |
let value = keyValue[1]; | |
if(key.includes('[')){ | |
let keyJsonString = '{"' + key.replaceAll(/\]\[/ig, '":{"').replaceAll(/\[/ig, '":{"').replaceAll(/\]/ig, '":') + '"' + value + '"' + ''.padEnd((key.match(/\]/g) || []).length, '}') + '}'; | |
let keyJsonObject = JSON.parse(keyJsonString); | |
$.extend(true, partsObject, keyJsonObject); | |
} | |
else{ | |
partsObject[key] = value; | |
} | |
} | |
return partsObject; | |
} | |
function buildUrlQueryFromJson(data, opening_array, encode){ | |
let output = []; | |
if(typeof encode === 'undefined') encode = false; | |
if(typeof opening_array !== 'string') opening_array = ''; | |
let next_opening_array = ''; | |
if((typeof data === 'object' || Array.isArray(data)) && data !== null){ | |
for(var i in data){ | |
if(opening_array.length) | |
next_opening_array = opening_array+'['+encodeURIComponent(i)+']'; | |
else | |
next_opening_array = i; | |
if((typeof data[i] === 'object' || Array.isArray(data[i])) && data[i] !== null){ | |
output.push(buildUrlQueryFromJson(data[i], next_opening_array)); | |
} | |
else if(data[i] !== null){ | |
if(opening_array.length) | |
output.push(opening_array+'['+i+']'+'='+encodeURIComponent(data[i])); | |
else | |
output.push(i+'='+encodeURIComponent(data[i])); | |
} | |
} | |
} | |
return encode ? encodeURI(output.join('&')) : output.join('&'); | |
} | |
function build_url(replaceItems, deleteItems, theUrl, onlyReturnQuery){ | |
if(typeof onlyReturnQuery === 'undefined') onlyReturnQuery = false; | |
if(!replaceItems || !Object.keys(replaceItems).length) replaceItems = {}; | |
if(!deleteItems) deleteItems = []; | |
if(!theUrl) theUrl = null; | |
var currentUrl = theUrl ? theUrl : window.location.href; | |
var urlParts = currentUrl.split('?'); | |
var directory = urlParts[0]; | |
var i = 0, | |
v = 0, | |
j = 0, | |
thisTerm = '', | |
thisTermParts = '', | |
found = false; | |
var query = typeof urlParts[1] !== 'undefined' ? urlParts[1] : null; | |
var existingQuery = buildJsonFromUrlQuery(query); | |
$.extend(true, existingQuery, replaceItems); | |
if(Object.keys(existingQuery).length && deleteItems.length){ | |
for(let i in deleteItems){ | |
if(existingQuery.hasOwnProperty(deleteItems[i])) | |
delete existingQuery[deleteItems[i]]; | |
} | |
} | |
let updatedQueryPart = encodeURI(buildUrlQueryFromJson(existingQuery)); | |
if(onlyReturnQuery) return updatedQueryPart; | |
else return directory + '?' + updatedQueryPart; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let testUrl = 'http://example.com/some/path/to/some/place?action=view&month_year=2021-09&fk_district_id=&fk_subdistrict_id=&fk_union_id=null&order%5B0%5D%5Bcolumn%5D=1&order%5B0%5D%5Bdir%5D=asc'; | |
let replaces = { | |
order: [ | |
{ | |
column: 1, | |
dir: 'asc' | |
}, | |
{ | |
column: 3, | |
dir: 'desc' | |
} | |
] | |
}; | |
let deletes = ['fk_subdistrict_id']; | |
console.log(decodeURI(build_url(replaces, deletes, testUrl, true))); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment