From: Eduardo San Martin Morote Date: Sun, 30 Jun 2019 16:00:44 +0000 (+0200) Subject: fix(query): fix empty query X-Git-Tag: v4.0.0-alpha.0~324 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=5c68ad0f764ea2f2ca4831449b1226696551c68c;p=thirdparty%2Fvuejs%2Frouter.git fix(query): fix empty query --- 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('=')