Support utilities
The kona-support module ships everyday helpers used across apps and the framework.
Collections
Section titled “Collections”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.
Collection<Integer> nums = Collection.make(1, 2, 3, 4);nums.reject(n -> n % 2 == 0).toList();Collection.times(2, i -> i);Collection.wrap(List.of(1, 2)).join("-");Strings
Section titled “Strings”Str.slug("Hello World") // hello-worldStr.of("hello_world").camel() // helloWorldStr.headline("hello_world") // Hello WorldStr.mask("5551234", "*", 0, 3) // ***1234Str.beforeLast("a/b/c", "/") // a/bStr.padLeft("7", 3, "0") // 007Stringable 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.
Arrays / maps
Section titled “Arrays / maps”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 → 1Localization
Section titled “Localization”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).