mirror of
https://github.com/Sendouc/sendou.ink.git
synced 2026-05-12 13:49:22 -05:00
27 lines
574 B
TypeScript
27 lines
574 B
TypeScript
export const setSearchParams = (
|
|
key: string,
|
|
value: string | string[] | undefined
|
|
) => {
|
|
const url = new URL(window.location.href);
|
|
const params = new URLSearchParams(url.search);
|
|
|
|
if (!value) {
|
|
params.delete(key);
|
|
} else if (Array.isArray(value)) {
|
|
params.delete(key);
|
|
value.forEach((element) => {
|
|
params.append(key, element);
|
|
});
|
|
} else {
|
|
params.set(key, value);
|
|
}
|
|
|
|
history.replaceState(
|
|
{},
|
|
"",
|
|
`${window.location.pathname}${
|
|
Array.from(params.entries()).length ? "?" : ""
|
|
}${params.toString()}`
|
|
);
|
|
};
|