Skip to content

Commit 6e9191d

Browse files
committed
fix(search-params): if array values passed, first clear existing values
ensure override, rather than append
1 parent c4d9715 commit 6e9191d

File tree

2 files changed

+8
-0
lines changed

2 files changed

+8
-0
lines changed

src/utils.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,8 @@ export function mergeSearchString(search: string, params: SetSearchParams) {
172172
merged.delete(key);
173173
} else {
174174
if (value instanceof Array) {
175+
// Delete all instances of the key before appending
176+
merged.delete(key);
175177
value.forEach(v => {
176178
merged.append(key, String(v));
177179
});

test/utils.spec.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,12 @@ describe("mergeSearchString should", () => {
136136
const actual = mergeSearchString("?foo=2&foo=3", { foo: [] });
137137
expect(actual).toBe(expected);
138138
});
139+
140+
test("return array containing only new value when current is present and new is an array with one value", () => {
141+
const expected = "?foo=1&foo=2";
142+
const actual = mergeSearchString("?foo=3&foo=4", { foo: [1, 2] });
143+
expect(actual).toBe(expected);
144+
});
139145
});
140146

141147
describe("extractSearchParams should", () => {

0 commit comments

Comments
 (0)