diff options
author | Paul Frazee <pfrazee@gmail.com> | 2023-03-21 17:58:50 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-03-21 17:58:50 -0500 |
commit | a7e3ce25854d6186b77e68c155a9a8bcdbd896ec (patch) | |
tree | 55c1a86575876c50824be7175a047c3e409ff7e6 /src/lib/routes/router.ts | |
parent | 48e18662f69530d5c201d08014a039126c88e7dd (diff) | |
download | voidsky-a7e3ce25854d6186b77e68c155a9a8bcdbd896ec.tar.zst |
* Refactor mobile search screen * Remove 'staleness' fetch trigger on search * Implement a temporary fulltext search solution * Add missing key from profile search result * A few UI & UX improvements to the search suggestions * Update web search suggestions * Implement search in web build
Diffstat (limited to 'src/lib/routes/router.ts')
-rw-r--r-- | src/lib/routes/router.ts | 29 |
1 files changed, 22 insertions, 7 deletions
diff --git a/src/lib/routes/router.ts b/src/lib/routes/router.ts index 05e0a63de..00defaeda 100644 --- a/src/lib/routes/router.ts +++ b/src/lib/routes/router.ts @@ -32,24 +32,39 @@ export class Router { } function createRoute(pattern: string): Route { - let matcherReInternal = pattern.replace( - /:([\w]+)/g, - (_m, name) => `(?<${name}>[^/]+)`, - ) + const pathParamNames: Set<string> = new Set() + let matcherReInternal = pattern.replace(/:([\w]+)/g, (_m, name) => { + pathParamNames.add(name) + return `(?<${name}>[^/]+)` + }) const matcherRe = new RegExp(`^${matcherReInternal}([?]|$)`, 'i') return { match(path: string) { - const res = matcherRe.exec(path) + const {pathname, searchParams} = new URL(path, 'http://throwaway.com') + const addedParams = Object.fromEntries(searchParams.entries()) + + const res = matcherRe.exec(pathname) if (res) { - return {params: res.groups || {}} + return {params: Object.assign(addedParams, res.groups || {})} } return undefined }, build(params: Record<string, string>) { - return pattern.replace( + const str = pattern.replace( /:([\w]+)/g, (_m, name) => params[name] || 'undefined', ) + + let hasQp = false + const qp = new URLSearchParams() + for (const paramName in params) { + if (!pathParamNames.has(paramName)) { + qp.set(paramName, params[paramName]) + hasQp = true + } + } + + return str + (hasQp ? `?${qp.toString()}` : '') }, } } |