diff options
author | Tim Zallmann <tzallmann@gitlab.com> | 2018-09-07 10:20:27 +0000 |
---|---|---|
committer | Tim Zallmann <tzallmann@gitlab.com> | 2018-09-07 10:20:27 +0000 |
commit | 44cb8a7407d3c9df958328088c914c5bedea284f (patch) | |
tree | 284d9d30036558b8d0be13ec8ba53368e52f6322 /app/assets/javascripts/lib | |
parent | 3276e88bd5d372004a599d4e3cbb3dda6b32a777 (diff) | |
parent | 8a5121eb8efcd330b518ad0194cd9779a18facf6 (diff) | |
download | gitlab-ce-44cb8a7407d3c9df958328088c914c5bedea284f.tar.gz |
Merge branch 'boards-querystring-backport' into 'master'
don't add trailing = when in urlParamsToArray function
See merge request gitlab-org/gitlab-ce!21179
Diffstat (limited to 'app/assets/javascripts/lib')
-rw-r--r-- | app/assets/javascripts/lib/utils/common_utils.js | 47 |
1 files changed, 37 insertions, 10 deletions
diff --git a/app/assets/javascripts/lib/utils/common_utils.js b/app/assets/javascripts/lib/utils/common_utils.js index 3e208764b3e..0849d97bc1a 100644 --- a/app/assets/javascripts/lib/utils/common_utils.js +++ b/app/assets/javascripts/lib/utils/common_utils.js @@ -131,16 +131,43 @@ export const parseUrlPathname = url => { return parsedUrl.pathname.charAt(0) === '/' ? parsedUrl.pathname : `/${parsedUrl.pathname}`; }; -// We can trust that each param has one & since values containing & will be encoded -// Remove the first character of search as it is always ? -export const getUrlParamsArray = () => - window.location.search - .slice(1) - .split('&') - .map(param => { - const split = param.split('='); - return [decodeURI(split[0]), split[1]].join('='); - }); +const splitPath = (path = '') => path + .replace(/^\?/, '') + .split('&'); + +export const urlParamsToArray = (path = '') => splitPath(path) + .filter(param => param.length > 0) + .map(param => { + const split = param.split('='); + return [decodeURI(split[0]), split[1]].join('='); + }); + +export const getUrlParamsArray = () => urlParamsToArray(window.location.search); + +export const urlParamsToObject = (path = '') => splitPath(path) + .reduce((dataParam, filterParam) => { + if (filterParam === '') { + return dataParam; + } + + const data = dataParam; + let [key, value] = filterParam.split('='); + const isArray = key.includes('[]'); + key = key.replace('[]', ''); + value = decodeURIComponent(value.replace(/\+/g, ' ')); + + if (isArray) { + if (!data[key]) { + data[key] = []; + } + + data[key].push(value); + } else { + data[key] = value; + } + + return data; + }, {}); export const isMetaKey = e => e.metaKey || e.ctrlKey || e.altKey || e.shiftKey; |