Commit ad5c2fee authored by Jose Vargas's avatar Jose Vargas

Extend queryToObject function

This extends the queryToObject function
to remove undefined values from the resulting
object
parent 586aafd7
......@@ -271,7 +271,9 @@ 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]);
if (p[1] !== undefined) {
accumulator[decodeURIComponent(p[0])] = decodeURIComponent(p[1]);
}
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