Skip to content

Instantly share code, notes, and snippets.

@taingmeng
Last active October 15, 2017 03:29
Show Gist options
  • Save taingmeng/d9be10be43e00a0bce0882cb4bd86f92 to your computer and use it in GitHub Desktop.
Save taingmeng/d9be10be43e00a0bce0882cb4bd86f92 to your computer and use it in GitHub Desktop.
Pluralize String with Kotlin Extension
fun String.pluralize(count: Int): String? {
return this.pluralize(count, null)
}
fun String.pluralize(count: Int, plural: String?): String? {
return if (count > 1) {
plural ?: this + 's'
} else {
this
}
}
import org.assertj.core.api.Assertions.assertThat
import org.junit.Test
class PluralizeTest {
@Test
fun pluralize() {
assertThat("word".pluralize(0)).isEqualTo("word")
assertThat("word".pluralize(1)).isEqualTo("word")
assertThat("word".pluralize(2)).isEqualTo("words")
}
@Test
fun pluralizeWithPlural() {
assertThat("person".pluralize(0, "people")).isEqualTo("person")
assertThat("person".pluralize(1, "people")).isEqualTo("person")
assertThat("person".pluralize(2, "people")).isEqualTo("people")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment