Commit 7c4295a1 authored by Andrew Fontaine's avatar Andrew Fontaine

Merge branch 'jivanvl-extend-query-to-object-remove-null-undefined' into 'master'

Extend queryToObject function to remove undefined

See merge request gitlab-org/gitlab!31376
parents 98a943b8 e781e1a3
......@@ -270,8 +270,10 @@ export function getWebSocketUrl(path) {
export function queryToObject(query) {
const removeQuestionMarkFromQuery = String(query).startsWith('?') ? query.slice(1) : query;
return removeQuestionMarkFromQuery.split('&').reduce((accumulator, curr) => {
const p = curr.split('=');
accumulator[decodeURIComponent(p[0])] = decodeURIComponent(p[1]);
const [key, value] = curr.split('=');
if (value !== undefined) {
accumulator[decodeURIComponent(key)] = decodeURIComponent(value);
}
return accumulator;
}, {});
}
......
......@@ -425,6 +425,12 @@ describe('URL utility', () => {
expect(urlUtils.queryToObject(searchQuery)).toEqual({ one: '1', two: '2' });
});
it('removes undefined values from the search query', () => {
const searchQuery = '?one=1&two=2&three';
expect(urlUtils.queryToObject(searchQuery)).toEqual({ one: '1', two: '2' });
});
});
describe('objectToQuery', () => {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment