Skip to content

Commit

Permalink
fix(search-params): if array values passed, first clear existing values
Browse files Browse the repository at this point in the history
ensure override, rather than append
  • Loading branch information
markojerkic committed Oct 14, 2024
1 parent c4d9715 commit 6e9191d
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,8 @@ export function mergeSearchString(search: string, params: SetSearchParams) {
merged.delete(key);
} else {
if (value instanceof Array) {
// Delete all instances of the key before appending
merged.delete(key);
value.forEach(v => {
merged.append(key, String(v));
});
Expand Down
6 changes: 6 additions & 0 deletions test/utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,12 @@ describe("mergeSearchString should", () => {
const actual = mergeSearchString("?foo=2&foo=3", { foo: [] });
expect(actual).toBe(expected);
});

test("return array containing only new value when current is present and new is an array with one value", () => {
const expected = "?foo=1&foo=2";
const actual = mergeSearchString("?foo=3&foo=4", { foo: [1, 2] });
expect(actual).toBe(expected);
});
});

describe("extractSearchParams should", () => {
Expand Down

0 comments on commit 6e9191d

Please sign in to comment.