diff options
Diffstat (limited to 'app/assets/javascripts/lib/utils/url_utility.js')
-rw-r--r-- | app/assets/javascripts/lib/utils/url_utility.js | 27 |
1 files changed, 22 insertions, 5 deletions
diff --git a/app/assets/javascripts/lib/utils/url_utility.js b/app/assets/javascripts/lib/utils/url_utility.js index 8077570158a..e9c3fe0a406 100644 --- a/app/assets/javascripts/lib/utils/url_utility.js +++ b/app/assets/javascripts/lib/utils/url_utility.js @@ -75,7 +75,7 @@ export function getParameterValues(sParam, url = window.location) { * @param {Boolean} options.spreadArrays - split array values into separate key/value-pairs */ export function mergeUrlParams(params, url, options = {}) { - const { spreadArrays = false } = options; + const { spreadArrays = false, sort = false } = options; const re = /^([^?#]*)(\?[^#]*)?(.*)/; let merged = {}; const [, fullpath, query, fragment] = url.match(re); @@ -108,7 +108,9 @@ export function mergeUrlParams(params, url, options = {}) { Object.assign(merged, params); - const newQuery = Object.keys(merged) + const mergedKeys = sort ? Object.keys(merged).sort() : Object.keys(merged); + + const newQuery = mergedKeys .filter(key => merged[key] !== null) .map(key => { let value = merged[key]; @@ -334,17 +336,32 @@ export function getWebSocketUrl(path) { * Convert search query into an object * * @param {String} query from "document.location.search" + * @param {Object} options + * @param {Boolean} options.gatherArrays - gather array values into an Array * @returns {Object} * * ex: "?one=1&two=2" into {one: 1, two: 2} */ -export function queryToObject(query) { +export function queryToObject(query, options = {}) { + const { gatherArrays = false } = options; const removeQuestionMarkFromQuery = String(query).startsWith('?') ? query.slice(1) : query; return removeQuestionMarkFromQuery.split('&').reduce((accumulator, curr) => { const [key, value] = curr.split('='); - if (value !== undefined) { - accumulator[decodeURIComponent(key)] = decodeURIComponent(value); + if (value === undefined) { + return accumulator; } + const decodedValue = decodeURIComponent(value); + + if (gatherArrays && key.endsWith('[]')) { + const decodedKey = decodeURIComponent(key.slice(0, -2)); + if (!Array.isArray(accumulator[decodedKey])) { + accumulator[decodedKey] = []; + } + accumulator[decodedKey].push(decodedValue); + } else { + accumulator[decodeURIComponent(key)] = decodedValue; + } + return accumulator; }, {}); } |