From 917d27b0cf781351b48d4150c4aa07f43a3f0e5b Mon Sep 17 00:00:00 2001 From: Alexander Lichter Date: Tue, 29 Nov 2022 09:04:05 +0100 Subject: [PATCH] fix(query): uniform handling of empty array values --- src/query.ts | 2 +- test/query.test.ts | 8 ++++++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/src/query.ts b/src/query.ts index eb808dc0..3eccff86 100644 --- a/src/query.ts +++ b/src/query.ts @@ -21,7 +21,7 @@ export function parseQuery (parametersString: string = ""): QueryObject { continue; } const value = decodeQueryValue(s[2] || ""); - if (object[key]) { + if (typeof object[key] !== "undefined") { if (Array.isArray(object[key])) { (object[key] as string[]).push(value); } else { diff --git a/test/query.test.ts b/test/query.test.ts index 20d0e6bc..48dbff78 100644 --- a/test/query.test.ts +++ b/test/query.test.ts @@ -27,7 +27,9 @@ describe("withQuery", () => { out: "/?str=%26&str2=%2526" }, { input: "/?x=1,2,3", query: { y: "1,2,3" }, out: "/?x=1,2,3&y=1,2,3" }, - { input: "http://a.com?v=1", query: { x: 2 }, out: "http://a.com?v=1&x=2" } + { input: "http://a.com?v=1", query: { x: 2 }, out: "http://a.com?v=1&x=2" }, + { input: "/", query: { param: ["3", ""] }, out: "/?param=3¶m=" }, + { input: "/", query: { param: ["", "3"] }, out: "/?param=¶m=3" } ]; for (const t of tests) { @@ -39,7 +41,9 @@ describe("withQuery", () => { describe("getQuery", () => { const tests = { - "http://foo.com/foo?test=123&unicode=%E5%A5%BD": { test: "123", unicode: "好" } + "http://foo.com/foo?test=123&unicode=%E5%A5%BD": { test: "123", unicode: "好" }, + "http://foo.com/?param=3¶m=": { param: ["3", ""] }, + "http://foo.com/?param=¶m=3": { param: ["", "3"] } }; for (const t in tests) {