foo: 'a',
})
})
+
+ it('works with an empty string', () => {
+ const emptyQuery = parseQuery('')
+ expect(Object.keys(emptyQuery)).toHaveLength(0)
+ expect(emptyQuery).toEqual({})
+ expect(parseQuery('?')).toEqual({})
+ })
})
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('=')