List Converter — Kotlin source
Transform a list between separators (newline, comma, space, pipe, semicolon, tab) with trim, dedupe, sort, and empty-removal options. Runs entirely in your browser, with a shareable link.
This is the Kotlin implementation — the same logic the interactive tool runs, in a shareable, citable form.
// List Converter — split/trim/dedup/sort/join between separators — Kotlin port of the list-converter tool.
object ListConverter {
/** Options mirror the TS ListOptions; defaults are from=newline, to=comma. */
data class Options(
val from: String = "newline",
val to: String = "comma",
val trim: Boolean = false,
val removeEmpty: Boolean = false,
val unique: Boolean = false,
val sort: Boolean = false,
val caseInsensitive: Boolean = false,
)
/** Resolve a TS-style separator name to its glue; unknown names pass
* through as a literal separator (the TS resolveSep behavior). */
fun resolveSep(name: String): String = when (name) {
"newline" -> "\n"
"comma" -> ","
"space" -> " "
"pipe" -> "|"
"semicolon" -> ";"
"tab" -> "\t"
else -> name
}
/** Convert a list between separators: split -> (trim) -> (drop empties) ->
* (dedup, first occurrence wins) -> (stable sort) -> join. Mirrors the Go twin. */
fun convert(input: String, options: Options = Options()): String {
val o = options
// String.split keeps trailing empties, matching Go's strings.Split.
var items = input.split(resolveSep(o.from))
val to = resolveSep(o.to)
if (o.trim) items = items.map { it.trim() }
if (o.removeEmpty) items = items.filter { it.isNotEmpty() }
if (o.unique) { // the set holds keys only; items keep their casing
val seen = LinkedHashSet<String>()
items = items.filter { seen.add(if (o.caseInsensitive) it.lowercase() else it) }
}
if (o.sort) { // sortedWith is documented stable; the key lowercases
// only for comparison, leaving the stored values untouched.
items = items.sortedWith(
compareBy { if (o.caseInsensitive) it.lowercase() else it }
)
}
return items.joinToString(to)
}
}
fun main() {
// "b\n a \nB\na\n\nc" -> trim, drop empties, case-insensitive dedup+sort
val o = ListConverter.Options(
trim = true, removeEmpty = true, unique = true,
sort = true, caseInsensitive = true,
)
println(ListConverter.convert("b\n a \nB\na\n\nc", o)) // a,b,c
}
Also available in 13 other languages
Every CosmoDev tool ships its pure logic in TypeScript (web) and Go (CLI), with authored implementations in a dozen-plus languages — the same contract, ported. Compare all languages side by side →