Skip to content

Commit

Permalink
fix(query): uniform handling of empty array values
Browse files Browse the repository at this point in the history
  • Loading branch information
manniL committed Nov 29, 2022
1 parent 672c11a commit 917d27b
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
8 changes: 6 additions & 2 deletions test/query.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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&param=" },
{ input: "/", query: { param: ["", "3"] }, out: "/?param=&param=3" }
];

for (const t of tests) {
Expand All @@ -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&param=": { param: ["3", ""] },
"http://foo.com/?param=&param=3": { param: ["", "3"] }
};

for (const t in tests) {
Expand Down

0 comments on commit 917d27b

Please sign in to comment.