From 21b3b95ce87b916523e3b511f94c9962248ba539 Mon Sep 17 00:00:00 2001 From: Eduardo San Martin Morote Date: Wed, 5 Feb 2020 12:41:05 +0100 Subject: [PATCH] fix: parse empty keys as null --- __tests__/parseQuery.spec.ts | 14 ++++++++++++++ src/history/common.ts | 7 +++++-- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/__tests__/parseQuery.spec.ts b/__tests__/parseQuery.spec.ts index fd7988ad..bb1863b9 100644 --- a/__tests__/parseQuery.spec.ts +++ b/__tests__/parseQuery.spec.ts @@ -9,6 +9,20 @@ describe('parseQuery', () => { }) }) + it('decodes empty values as null', () => { + expect(parseQuery('e&b&c=a')).toEqual({ + e: null, + b: null, + c: 'a', + }) + }) + + it('decodes empty values as null in arrays', () => { + expect(parseQuery('e&e&e=a')).toEqual({ + e: [null, null, 'a'], + }) + }) + it('decodes array values in query', () => { expect(parseQuery('e=%25&e=%22')).toEqual({ e: ['%', '"'], diff --git a/src/history/common.ts b/src/history/common.ts index 8cc83b43..edade696 100644 --- a/src/history/common.ts +++ b/src/history/common.ts @@ -172,10 +172,13 @@ export function parseQuery(search: string): HistoryQuery { const hasLeadingIM = search[0] === '?' const searchParams = (hasLeadingIM ? search.slice(1) : search).split('&') for (let i = 0; i < searchParams.length; ++i) { - let [key, value] = searchParams[i].split('=') + let [key, rawValue] = searchParams[i].split('=') as [ + string, + string | undefined + ] key = decode(key) // avoid decoding null - value = value && decode(value) + let value = rawValue == null ? null : decode(rawValue) if (key in query) { // an extra variable for ts types let currentValue = query[key] -- 2.47.3