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.
Installation
Section titled “Installation”Install kairo-util.
dependencies { implementation("software.airborne.kairo:kairo-util")}The most popular functions are documented here.
canonicalize() and slugify()
Section titled “canonicalize() and slugify()”canonicalize() turns an arbitrary string into exclusively lowercase latin characters.
canonicalize(" Con | dãnas^t")// => con danastslugify() works similarly, but allows you to choose the delimiter.
slugify(" Con | dãnas^t", delimiter = "-")// => con-danastfirstCauseOf<T>()
Section titled “firstCauseOf<T>()”For exceptions, returns the first recursive cause of the specified type, or null if there is no matching cause.
e.firstCauseOf<MyExceptionType>()resource()
Section titled “resource()”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.
singleNullOrThrow()
Section titled “singleNullOrThrow()”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