Uri Extensions
hasQueryValue
Checks whether this Uri has a query parameter with the specified name
and value
.
- Example
- Declaration
// returns true
Uri.of("/test?q=kotlin").hasQueryValue("q", "kotlin")
// returns false
Uri.of("/test?size=large").hasQueryValue("size", "small")
fun Uri.hasQueryValue(name: String, value: String): Boolean
removeQueryValue
Returns a new Uri
without the specified name
parameter, if the parameter's value matches
the specified value
. All other query params are left unchanged.
- Example
- Declaration
// Returns "/test"
Uri.of("/test?q=John").removeQueryValue("q", "John")
// Returns "/test?q=kotlin"
Uri.of("/test?q=kotlin&q=java").removeQueryValue("q", "java")
fun Uri.removeQueryValue(name: String, value: Any): Uri
toggleQueryValue
Returns a new Uri
and:
- Adds a query parameter with the specified
name
andvalue
when one isn't already present - Removes the query parameter with the specified
name
andvalue
when present
All other query params are left unchanged.
- Example
- Declaration
// Returns "/test?q=John"
Uri.of("/test").toggleQueryValue("q", "John")
// Returns "/test"
Uri.of("/test?q=John").toggleQueryValue("q", "John")
fun Uri.toggleQueryValue(name: String, value: Any): Uri
removeQueries
Removes all query parameters with the specified names
(immaterial of their values).
All other query params are left unchanged.
- Example
- Declaration
// Returns "/test"
Uri.of("/test?q=john&filter=name").removeQueries(listOf("q", "filter"))
fun Uri.removeQueries(names: Collection<String>): Uri
replaceQuery
Replaces the specified query parameters with the given values and returns a new Uri
.
All other query params are left unchanged.
- Example
- Declaration
// Returns "/test?q=John&size=10"
Uri.of("/test?q=nothing&size=5").replaceQuery("q" to "John", "size" to "10")
fun Uri.replaceQuery(vararg queryValues: Pair<String, Any>): Uri
fun Uri.replaceQuery(queries: Map<String, Any>): Uri
replacePage
Returns a new Uri
with the page parameters (&from=x&size=y
) replaced with new values matching
the specified invirt.data.Page
.
- Example
- Declaration
// Returns "/test?from=10&size=5"
Uri.of("/test?from=0&size=10").replacePage(Page(10, 5))
fun Uri.replacePage(page: Page): Uri
replaceSort
Returns a new Uri
with the sort query param (&sort=name:asc
) replaced with a new value matching
the specified sort
argument. The function resets pagination by removing the from
and size
query parameters. This can be disabled
by passing false
for the resetPagination
argument.
- Example
- Declaration
// Returns "/test?sort=createdAt:desc"
Uri.of("/test?sort=name:asc").replaceSort(Sort.desc("createdAt"))
fun Uri.replaceSort(sort: Sort, resetPagination: Boolean = true): Uri