Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Re-implement util functions using URL API #2954

Merged
merged 15 commits into from
Aug 10, 2021
22 changes: 6 additions & 16 deletions app/assets/javascripts/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,25 +99,15 @@ function updateArrayURLParameter(url, param, _paramVals) {
return baseURL + "?" + newAdditionalURL + rowsTxt;
}

function getURLParameter(_name, _url) {
const url = _url || window.location.href;
const name = _name.replace(/[[\]]/g, "\\$&");
const regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)");
const results = regex.exec(url);
if (!results) return null;
if (!results[2]) return "";
return decodeURIComponent(results[2].replace(/\+/g, " "));
function getURLParameter(name, _url) {
const url = new URL(_url ?? window.location.href);
const result = url.searchParams.get(name);
return result;
TimonDB marked this conversation as resolved.
Show resolved Hide resolved
}

function getArrayURLParameter(name, _url) {
const url = _url || window.location.href;
const result = [];
for (const part of url.split(/[?&]/)) {
const regResults = new RegExp(`${name}%5B%5D=([^#]+)`).exec(part);
if (regResults && regResults[1]) {
result.push(decodeURIComponent(regResults[1]));
}
}
const url = new URL(_url ?? window.location.href);
const result = url.searchParams.getAll(name);
TimonDB marked this conversation as resolved.
Show resolved Hide resolved
return result;
TimonDB marked this conversation as resolved.
Show resolved Hide resolved
}

Expand Down