All Downloads are FREE. Search and download functionalities are using the official Maven repository.

commonMain.moe.tlaster.precompose.navigation.QueryString.kt Maven / Gradle / Ivy

Go to download

A third-party Jetbrains Compose library with ViewModel, LiveData and Navigation support.

There is a newer version: 1.7.0-alpha03
Show newest version
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) }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy