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: 7 additions & 15 deletions app/assets/javascripts/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,24 +100,16 @@ function updateArrayURLParameter(url, param, _paramVals) {
}

function getURLParameter(_name, _url) {
TimonDB marked this conversation as resolved.
Show resolved Hide resolved
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, " "));
const url = new URL(_url || window.location.href);
bmesuere marked this conversation as resolved.
Show resolved Hide resolved
const result = url.searchParams.get(_name);
TimonDB marked this conversation as resolved.
Show resolved Hide resolved
if (!result) return null;
return result;
}

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
if (!result) return [];
return result;
}

Expand Down