Skip to main content

Uri Extensions

hasQueryValue

Checks whether this Uri has a query parameter with the specified name and value.

// returns true
Uri.of("/test?q=kotlin").hasQueryValue("q", "kotlin")

// returns false
Uri.of("/test?size=large").hasQueryValue("size", "small")

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.

// Returns "/test"
Uri.of("/test?q=John").removeQueryValue("q", "John")

// Returns "/test?q=kotlin"
Uri.of("/test?q=kotlin&q=java").removeQueryValue("q", "java")

toggleQueryValue

Returns a new Uri and:

  • Adds a query parameter with the specified name and value when one isn't already present
  • Removes the query parameter with the specified name and value when present

All other query params are left unchanged.

// Returns "/test?q=John"
Uri.of("/test").toggleQueryValue("q", "John")

// Returns "/test"
Uri.of("/test?q=John").toggleQueryValue("q", "John")

removeQueries

Removes all query parameters with the specified names (immaterial of their values). All other query params are left unchanged.

// Returns "/test"
Uri.of("/test?q=john&filter=name").removeQueries(listOf("q", "filter"))

replaceQuery

Replaces the specified query parameters with the given values and returns a new Uri. All other query params are left unchanged.

// Returns "/test?q=John&size=10"
Uri.of("/test?q=nothing&size=5").replaceQuery("q" to "John", "size" to "10")

replacePage

Returns a new Uri with the page parameters (&from=x&size=y) replaced with new values matching the specified invirt.data.Page.

// Returns "/test?from=10&size=5"
Uri.of("/test?from=0&size=10").replacePage(Page(10, 5))

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.

// Returns "/test?sort=createdAt:desc"
Uri.of("/test?sort=name:asc").replaceSort(Sort.desc("createdAt"))