Skip to content

Instantly share code, notes, and snippets.

@delabassee
Last active August 7, 2018 09:28
Show Gist options
  • Save delabassee/58b2aec9ddad525986f4898802900bde to your computer and use it in GitHub Desktop.
Save delabassee/58b2aec9ddad525986f4898802900bde to your computer and use it in GitHub Desktop.
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
import com.delabassee.Country
fun country(input: String): String {
return jacksonObjectMapper().writeValueAsString( listCountries(input) )
}
fun listCountries(input: String): List<Country> = when {
input.isEmpty() -> Country.getAll()
else -> Country.getAll().filteredOrAll {
it.name.contains(input.trim(), true)
}
}
private fun <T> List<T>.filteredOrAll(predicate: (T) -> Boolean): List<T> {
val filtered = filter(predicate)
return when {
filtered.isEmpty() -> this
else -> filtered
}
}
@delabassee
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment