commonMain.moe.tlaster.precompose.navigation.QueryString.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of precompose-jvm Show documentation
Show all versions of precompose-jvm Show documentation
A third-party Jetbrains Compose library with ViewModel, LiveData and Navigation support.
package moe.tlaster.precompose.navigation
data class QueryString(
private val rawInput: String,
) {
val map by lazy {
rawInput
.substringAfter("?")
.splitToSequence("&")
.map { it.split("=") }
.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()
}
}
inline fun QueryString.query(name: String, default: T? = null): T? {
val value = map[name]?.firstOrNull() ?: return default
return convertValue(value)
}
inline fun QueryString.queryList(name: String): List {
val value = map[name] ?: return emptyList()
return value.mapNotNull { convertValue(it) }
}