Skip to content

Kairo Utilities

A home for simple utilities.

Utilities modules like this can easily become junk drawers; we make an effort to keep kairo-util small and focused.

Install kairo-util.

build.gradle.kts
dependencies {
implementation("software.airborne.kairo:kairo-util")
}

The most popular functions are documented here.

canonicalize() turns an arbitrary string into exclusively lowercase latin characters.

canonicalize(" Con | dãnas^t")
// => con danast

slugify() works similarly, but allows you to choose the delimiter.

slugify(" Con | dãnas^t", delimiter = "-")
// => con-danast

For exceptions, returns the first recursive cause of the specified type, or null if there is no matching cause.

e.firstCauseOf<MyExceptionType>()

Leverages Guava to fetch a given resource URL.

val resource = resource("...")
println(resource)

Note: This uses Guava under the hood. Guava is a large library and is not included in this artifact. Usage of resource() requires Guava to be available at runtime.

Kotlin’s single() and singleOrNull() functions are excellent utilities, but singleOrNull() can be surprising because it returns null when there are multiple items.

singleNullOrThrow() returns the single element, or null if the collection is empty. If the collection has more than one element, throws IllegalArgumentException.

This works with Array, Iterable, and Sequence receivers, as well as Flow through kairo-coroutines.

emptyList<Int>().singleNullOrThrow()
// => null
listOf(1).singleNullOrThrow()
// => 1
listOf(1, 2).singleNullOrThrow()
// => IllegalArgumentException