Skip to content
Kona

Support utilities

The kona-support module ships everyday helpers used across apps and the framework.

Collection wraps a list with a fluent Laravel-shaped API. Create one with collect(...), Collection.make(...), or Collection.of(...).

val names = collect(users)
.where("active", true)
.pluck("name")
.filter { it != null }
.join(", ")
collect(1, 2, 3, 4)
.reject { it % 2 == 0 }
.partition { it > 1 }
Collection.times(3) { "row-$it" }
Collection.wrap(listOf(1, 2)).merge(listOf(3))

Useful methods include map, flatMap, filter, reject, where / firstWhere, reduce, chunk, collapse, flatten, sum / avg / min / max, keyBy, keyed pluck, groupBy, partition, sortBy, unique, sole, diff / intersect, mapWithKeys, countBy, slice / nth / pad, pop / shift / prepend, and when / unless / whenEmpty.

Str.slug("Hello World") // hello-world
Str.of("hello_world").camel() // helloWorld
Str.headline("hello_world") // Hello World
Str.mask("5551234", "*", 0, 3) // ***1234
Str.beforeLast("a/b/c", "/") // a/b
Str.padLeft("7", 3, "0") // 007

Stringable from Str.of(...) chains the same transforms (lower, snake, slug, limit, append, finish, …).

KonaDate is a fluent Instant wrapper:

val d = KonaDate.parse("2024-01-15T12:30:00Z")
d.addMonths(1).startOfDay().toDateString()
d.diffInDays(d.subDays(2))
d.isSameDay(d.addHours(3))

Also: addWeeks / addYears, endOfDay, setTimezone, isPast / isFuture, toDateTimeString.

Arr.get, Arr.set, Arr.has, Arr.forget, Arr.only, Arr.except, Arr.wrap, Arr.flatten, Arr.pluck, and Arr.dot work with nested dotted keys (user.name).

val row = linkedMapOf<String, Any?>()
Arr.set(row, "user.name", "Ada")
Arr.forget(row, "user.name")
Arr.dot(mapOf("user" to mapOf("age" to 1))) // user.age → 1
val t = Translator().addLines("en", mapOf("hi" to "Hello :name"))
t.get("hi", mapOf("name" to "Ada"))
Lang.setTranslator(t)
translate("hi", mapOf("name" to "Ada"))
Translations.__("hi", Map.of("name", "Ada"));

Register SupportServiceProvider to bind a shared Translator. If lang.path is set or {base}/lang exists, JSON and key=value files are loaded automatically (lang/en.json, lang/en/messages.json, lang/en.properties, and so on).