Skip to content

Commit 18b9b52

Browse files
committed
support arrays in search params tweaks
1 parent f586b1b commit 18b9b52

File tree

4 files changed

+11
-9
lines changed

4 files changed

+11
-9
lines changed

.changeset/fresh-wasps-explode.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@solidjs/router": patch
3+
---
4+
5+
Support arrays in Search Params

src/routing.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ export function getRouteMatches(branches: Branch[], location: string): RouteMatc
231231
function createLocation(
232232
path: Accessor<string>,
233233
state: Accessor<any>,
234-
queryWrapper?: (getQuery: () => Params) => Params
234+
queryWrapper?: (getQuery: () => SearchParams) => SearchParams
235235
): Location {
236236
const origin = new URL(mockBase);
237237
const url = createMemo<URL>(
@@ -254,7 +254,7 @@ function createLocation(
254254
const search = createMemo(() => url().search, true);
255255
const hash = createMemo(() => url().hash);
256256
const key = () => "";
257-
const queryFn = on(search, () => extractSearchParams(url())) as () => Params;
257+
const queryFn = on(search, () => extractSearchParams(url())) as () => SearchParams;
258258

259259
return {
260260
get pathname() {

src/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ export interface RouterUtils {
167167
go(delta: number): void;
168168
beforeLeave: BeforeLeaveLifecycle;
169169
paramsWrapper: (getParams: () => Params, branches: () => Branch[]) => Params;
170-
queryWrapper: (getQuery: () => Params) => Params;
170+
queryWrapper: (getQuery: () => SearchParams) => SearchParams;
171171
}
172172

173173
export interface RouterContext {

src/utils.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,9 @@ export function extractSearchParams(url: URL): SearchParams {
5151
const params: SearchParams = {};
5252
url.searchParams.forEach((value, key) => {
5353
if (key in params) {
54-
params[key] = Array.isArray(params[key])
55-
? ([...params[key], value] as string[])
56-
: ([params[key], value] as string[]);
57-
} else {
58-
params[key] = value;
59-
}
54+
if (Array.isArray(params[key])) (params[key] as string[]).push(value);
55+
else params[key] = [params[key] as string, value];
56+
} else params[key] = value;
6057
});
6158
return params;
6259
}

0 commit comments

Comments
 (0)