diff --git a/precompose/src/commonMain/kotlin/moe/tlaster/precompose/navigation/QueryString.kt b/precompose/src/commonMain/kotlin/moe/tlaster/precompose/navigation/QueryString.kt index 1ac19bf4..1f8b49e1 100644 --- a/precompose/src/commonMain/kotlin/moe/tlaster/precompose/navigation/QueryString.kt +++ b/precompose/src/commonMain/kotlin/moe/tlaster/precompose/navigation/QueryString.kt @@ -5,20 +5,12 @@ data class QueryString( ) { val map by lazy { rawInput - .split("?") - .lastOrNull() - .let { - it ?: "" - } - .split("&") - .asSequence() + .substringAfter("?") + .splitToSequence("&") .map { it.split("=") } - .filter { !it.firstOrNull().isNullOrEmpty() } - .filter { it.size in 1..2 } - .map { it[0] to it.elementAtOrNull(1) } - .groupBy { it.first } - .map { it.key to it.value.mapNotNull { it.second.takeIf { !it.isNullOrEmpty() } } } - .toList() + .filter { it.size in 1..2 && it[0].isNotEmpty() } + .groupBy({ it[0] }, { it.getOrNull(1) }) + .map { it -> it.key to it.value.mapNotNull { it?.takeIf { it.isNotEmpty() } } } .toMap() } } diff --git a/precompose/src/commonTest/kotlin/moe/tlaster/precompose/navigation/QueryStringTest.kt b/precompose/src/commonTest/kotlin/moe/tlaster/precompose/navigation/QueryStringTest.kt index 9f66ac1f..bf9def31 100644 --- a/precompose/src/commonTest/kotlin/moe/tlaster/precompose/navigation/QueryStringTest.kt +++ b/precompose/src/commonTest/kotlin/moe/tlaster/precompose/navigation/QueryStringTest.kt @@ -13,24 +13,28 @@ class QueryStringTest { assertTrue(it.map.containsValue(listOf("bar"))) assertEquals(it.query("foo"), "bar") } + QueryString("foo=bar&").let { assertTrue(it.map.size == 1) assertTrue(it.map.containsKey("foo")) assertTrue(it.map.containsValue(listOf("bar"))) assertEquals(it.query("foo"), "bar") } + QueryString("foo=bar&&").let { assertTrue(it.map.size == 1) assertTrue(it.map.containsKey("foo")) assertTrue(it.map.containsValue(listOf("bar"))) assertEquals(it.query("foo"), "bar") } + QueryString("foo=bar").let { assertTrue(it.map.size == 1) assertTrue(it.map.containsKey("foo")) assertTrue(it.map.containsValue(listOf("bar"))) assertEquals(it.query("foo"), "bar") } + QueryString("a=1&b=2").let { assertTrue(it.map.size == 2) assertTrue(it.map.containsKey("a")) @@ -40,6 +44,7 @@ class QueryStringTest { assertEquals(it.query("a"), "1") assertEquals(it.query("b"), "2") } + QueryString("a=1&b=2&").let { assertTrue(it.map.size == 2) assertTrue(it.map.containsKey("a")) @@ -49,6 +54,7 @@ class QueryStringTest { assertEquals(it.query("a"), "1") assertEquals(it.query("b"), "2") } + QueryString("a=1&&b=2&").let { assertTrue(it.map.size == 2) assertTrue(it.map.containsKey("a")) @@ -58,32 +64,34 @@ class QueryStringTest { assertEquals(it.query("a"), "1") assertEquals(it.query("b"), "2") } + QueryString("a=1&a=2").let { assertTrue(it.map.size == 1) assertTrue(it.map.containsKey("a")) assertTrue(it.map.containsValue(listOf("1", "2"))) assertEquals(it.queryList("a"), listOf("1", "2")) } - QueryString("a=1;a=2").let { - assertTrue(it.map.isEmpty()) - } + + assertTrue(QueryString("a=1;a=2").map.isEmpty()) + QueryString("a=").let { assertTrue(it.map.size == 1) assertTrue(it.map.containsKey("a")) assertEquals(it.queryList("a"), emptyList()) } + QueryString("a=&").let { assertTrue(it.map.size == 1) assertTrue(it.map.containsKey("a")) assertEquals(it.queryList("a"), emptyList()) } + QueryString("a=&&").let { assertTrue(it.map.size == 1) assertTrue(it.map.containsKey("a")) assertEquals(it.queryList("a"), emptyList()) } - QueryString("").let { - assertTrue(it.map.isEmpty()) - } + + assertTrue(QueryString("").map.isEmpty()) } }