Files
mutiny-web/src/utils/objectToSearchParams.ts
2023-06-06 10:05:43 -05:00

16 lines
499 B
TypeScript

export function objectToSearchParams<
T extends Record<string, string | undefined>
>(obj: T): string {
return (
Object.entries(obj)
.filter(([_, value]) => value !== undefined)
// Value shouldn't be null we just filtered it out but typescript is dumb
.map(([key, value]) =>
value
? `${encodeURIComponent(key)}=${encodeURIComponent(value)}`
: ""
)
.join("&")
);
}