From 5c68ad0f764ea2f2ca4831449b1226696551c68c Mon Sep 17 00:00:00 2001 From: Eduardo San Martin Morote Date: Sun, 30 Jun 2019 18:00:44 +0200 Subject: [PATCH] fix(query): fix empty query --- __tests__/query.spec.js | 7 +++++++ src/history/utils.ts | 3 +++ 2 files changed, 10 insertions(+) diff --git a/__tests__/query.spec.js b/__tests__/query.spec.js index fe561dbb..47d3ad10 100644 --- a/__tests__/query.spec.js +++ b/__tests__/query.spec.js @@ -15,4 +15,11 @@ describe('parseQuery', () => { foo: 'a', }) }) + + it('works with an empty string', () => { + const emptyQuery = parseQuery('') + expect(Object.keys(emptyQuery)).toHaveLength(0) + expect(emptyQuery).toEqual({}) + expect(parseQuery('?')).toEqual({}) + }) }) diff --git a/src/history/utils.ts b/src/history/utils.ts index 70857fcd..3ad84263 100644 --- a/src/history/utils.ts +++ b/src/history/utils.ts @@ -54,6 +54,9 @@ export function parseURL(location: string): HistoryLocationNormalized { export function parseQuery(search: string): HistoryQuery { const hasLeadingIM = search[0] === '?' const query: HistoryQuery = {} + // avoid creating an object with an empty key and empty value + // because of split('&') + if (search === '' || search === '?') return query const searchParams = (hasLeadingIM ? search.slice(1) : search).split('&') for (let i = 0; i < searchParams.length; ++i) { const [key, value] = searchParams[i].split('=') -- 2.39.5