From d5b635ace2632fcfb9d51fd745fdb4f859131263 Mon Sep 17 00:00:00 2001 From: Michael Rittmeister Date: Tue, 19 Sep 2023 13:56:05 +0200 Subject: [PATCH] Drop leading ? from query --- .../tonbrett/app/web/resource/RouteHandler.kt | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/app/web/src/jsMain/kotlin/dev/schlaubi/tonbrett/app/web/resource/RouteHandler.kt b/app/web/src/jsMain/kotlin/dev/schlaubi/tonbrett/app/web/resource/RouteHandler.kt index 48b4d51..65d824f 100644 --- a/app/web/src/jsMain/kotlin/dev/schlaubi/tonbrett/app/web/resource/RouteHandler.kt +++ b/app/web/src/jsMain/kotlin/dev/schlaubi/tonbrett/app/web/resource/RouteHandler.kt @@ -25,9 +25,9 @@ inline fun routeHandler(body: (R) -> Unit) { "Specified type must be resource" } val pattern = format.encodeToPathPattern(serializer) - if (window.location.pathname.replaceLeadingSlash() != pattern) return + if (window.location.pathname.dropLeadingSlash() != pattern) return - val parameters = window.location.search.parseUrlEncodedParameters() + val parameters = window.location.search.dropLeadingQuery().parseUrlEncodedParameters() val resources = format.decodeFromParameters(serializer, parameters) body(resources) @@ -48,10 +48,15 @@ inline fun ComposeRouteHandler(crossinline content: @Composabl } @PublishedApi -internal fun String.replaceLeadingSlash() = replaceFirstChar { - if (it == '/') { +internal fun String.dropLeadingSlash() = dropLeading('/') +@PublishedApi +internal fun String.dropLeadingQuery() = dropLeading('?') + +@PublishedApi +internal fun String.dropLeading(char: Char) = replaceFirstChar { + if (it == char) { "" } else { it.toString() } -} \ No newline at end of file +}