]> git.ipfire.org Git - thirdparty/vuejs/router.git/commitdiff
fix(query): fix empty query
authorEduardo San Martin Morote <posva13@gmail.com>
Sun, 30 Jun 2019 16:00:44 +0000 (18:00 +0200)
committerEduardo San Martin Morote <posva13@gmail.com>
Sun, 30 Jun 2019 16:00:44 +0000 (18:00 +0200)
__tests__/query.spec.js
src/history/utils.ts

index fe561dbb34ba36c245bf3a6bb47b79ddfe073d19..47d3ad1090e1b3e2afea0d1b35810234f361ac60 100644 (file)
@@ -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({})
+  })
 })
index 70857fcd35a45df29cabaf5da5dc72dcce3b4b9d..3ad842632334085335d09cccae0b50a4671a48cd 100644 (file)
@@ -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('=')