Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions src/serialize.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,27 @@ test("serializer should not mutate params", () => {
expect(params.location).toBe(location);
});

test("serializer should not format an explicit null/undefined value, and should drop it from the query string", () => {
expect(
serializer(
{ location: latLngToString },
"http://mock.url"
)({
radius: 50000,
location: undefined,
})
).toBe("radius=50000");
expect(
serializer(
{ location: latLngToString },
"http://mock.url"
)({
radius: 50000,
location: null,
})
).toBe("radius=50000");
});

test("serializer should return pipe joined arrays by default", () => {
expect(serializer({}, "http://mock.url")({ foo: ["b", "a", "r"] })).toBe(
"foo=b|a|r"
Expand Down
12 changes: 11 additions & 1 deletion src/serialize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,17 @@ export function serializer(

for (const key of Object.keys(format)) {
if (key in serializedParams) {
serializedParams[key] = format[key](serializedParams[key]);
// An explicit `null`/`undefined` (e.g. `{ location: undefined }`) means
// "not supplied", not "format this value" β€” several format functions
// (e.g. `latLngToString`) throw on it. Deleting the key rather than
// leaving it as-is also sidesteps `qs()`'s inconsistent handling of
// the two: it drops `undefined` from the query string by default but
// keeps `null` as a bare, value-less key.
if (serializedParams[key] == null) {
delete serializedParams[key];
} else {
serializedParams[key] = format[key](serializedParams[key]);
}
}
}

Expand Down