diff options
author | Thomas Holder <thomas@thomas-holder.de> | 2018-11-30 12:49:22 +0300 |
---|---|---|
committer | Phil Hughes <me@iamphill.com> | 2018-11-30 12:49:22 +0300 |
commit | fe76827f178924daed720117f0a34c1897f3ebdf (patch) | |
tree | cb20f64e4c171637b3526da31fdc36b6fc0c72b9 /app | |
parent | b6e70c8ae48e7816a81e738242bedca9cfcf1fd1 (diff) |
Resolve "mergeUrlParams wrong with fragment url"
Diffstat (limited to 'app')
-rw-r--r-- | app/assets/javascripts/lib/utils/url_utility.js | 38 |
1 files changed, 20 insertions, 18 deletions
diff --git a/app/assets/javascripts/lib/utils/url_utility.js b/app/assets/javascripts/lib/utils/url_utility.js index a282c2df441..9850f7ce782 100644 --- a/app/assets/javascripts/lib/utils/url_utility.js +++ b/app/assets/javascripts/lib/utils/url_utility.js @@ -17,27 +17,29 @@ export function getParameterValues(sParam) { // @param {Object} params - url keys and value to merge // @param {String} url export function mergeUrlParams(params, url) { - let newUrl = Object.keys(params).reduce((acc, paramName) => { - const paramValue = encodeURIComponent(params[paramName]); - const pattern = new RegExp(`\\b(${paramName}=).*?(&|$)`); - - if (paramValue === null) { - return acc.replace(pattern, ''); - } else if (url.search(pattern) !== -1) { - return acc.replace(pattern, `$1${paramValue}$2`); - } - - return `${acc}${acc.indexOf('?') > 0 ? '&' : '?'}${paramName}=${paramValue}`; - }, decodeURIComponent(url)); + const re = /^([^?#]*)(\?[^#]*)?(.*)/; + const merged = {}; + const urlparts = url.match(re); + + if (urlparts[2]) { + urlparts[2] + .substr(1) + .split('&') + .forEach(part => { + if (part.length) { + const kv = part.split('='); + merged[decodeURIComponent(kv[0])] = decodeURIComponent(kv.slice(1).join('=')); + } + }); + } - // Remove a trailing ampersand - const lastChar = newUrl[newUrl.length - 1]; + Object.assign(merged, params); - if (lastChar === '&') { - newUrl = newUrl.slice(0, -1); - } + const query = Object.keys(merged) + .map(key => `${encodeURIComponent(key)}=${encodeURIComponent(merged[key])}`) + .join('&'); - return newUrl; + return `${urlparts[1]}?${query}${urlparts[3]}`; } export function removeParamQueryString(url, param) { |