Skip to content
Snippets Groups Projects
Commit 6412f8ae authored by Simon Knox's avatar Simon Knox
Browse files

Merge branch 'add-sort-option-to-mergeUrlParams' into 'master'

Add sort option to mergeUrlParams

See merge request gitlab-org/gitlab!42561
parents 733323a7 7012486b
No related branches found
No related tags found
No related merge requests found
Loading
Loading
@@ -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);
Loading
Loading
@@ -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];
Loading
Loading
Loading
Loading
@@ -161,6 +161,15 @@ describe('URL utility', () => {
);
});
 
it('sorts params in alphabetical order with sort option', () => {
expect(mergeUrlParams({ c: 'c', b: 'b', a: 'a' }, 'https://host/path', { sort: true })).toBe(
'https://host/path?a=a&b=b&c=c',
);
expect(
mergeUrlParams({ alpha: 'alpha' }, 'https://host/path?op=/&foo=bar', { sort: true }),
).toBe('https://host/path?alpha=alpha&foo=bar&op=%2F');
});
describe('with spread array option', () => {
const spreadArrayOptions = { spreadArrays: true };
 
Loading
Loading
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment