Skip to content

Commit

Permalink
Handling some words mentioned in #4
Browse files Browse the repository at this point in the history
  • Loading branch information
Oleg Michailov committed Aug 5, 2020
1 parent e1f9882 commit 76dd8c2
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 4 deletions.
11 changes: 7 additions & 4 deletions library/src/main/kotlin/com/cesarferreira/pluralize/Pluralize.kt
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ private fun String.pluralizer(): String {
var found = Pattern.compile(rule.component1(), Pattern.CASE_INSENSITIVE).matcher(this).replaceAll(rule.component2())
val endsWith = exceptions().firstOrNull { this.endsWith(it.component1()) }
if (endsWith != null) found = this.replace(endsWith.component1(), endsWith.component2())
val exception = exceptions().firstOrNull() { this.equals(it.component1()) }
val exception = exceptions().firstOrNull() { this.equals(it.component1(), true) }
if (exception != null) found = exception.component2()
return found
}
Expand All @@ -59,7 +59,7 @@ private fun String.singularizer(): String {
if (unCountable().contains(this.toLowerCase())) {
return this
}
val exceptions = exceptions().firstOrNull() { this.equals(it.component2()) }
val exceptions = exceptions().firstOrNull() { this.equals(it.component2(), true) }

if (exceptions != null) {
return exceptions.component1()
Expand Down Expand Up @@ -147,7 +147,8 @@ fun exceptions(): List<Pair<String, String>> {
"noumenon" to "noumena",
"organon" to "organa",
"asyndeton" to "asyndeta",
"hyperbaton" to "hyperbata")
"hyperbaton" to "hyperbata",
"foot" to "feet")
}

fun pluralizeRules(): List<Pair<String, String>> {
Expand Down Expand Up @@ -176,7 +177,9 @@ fun pluralizeRules(): List<Pair<String, String>> {
"f$" to "ves",
"fe$" to "ves",
"um$" to "a",
"on$" to "a")
"on$" to "a",
"tion" to "tions",
"sion" to "sions")
}

fun singularizeRules(): List<Pair<String, String>> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ class PluralizationTest {
@Test
fun `Should add default "s" suffix`() {
assertEquals("posts", "post".pluralize())
assertEquals("descriptions", "description".pluralize())
assertEquals("colections", "colection".pluralize())
assertEquals("versions", "version".pluralize())
}

@Test
Expand All @@ -18,6 +21,12 @@ class PluralizationTest {
@Test
fun `Should handle exception words`() {
assertEquals("men", "man".pluralize())
assertEquals("feet", "foot".pluralize())
}

@Test
fun `Should handle in case insensitive manner`() {
assertEquals("people", "Person".pluralize())
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,9 @@ class SingularizationTest {
assertEquals("person", "people".singularize())
}

@Test
fun `Should handle in case insensitive manner`() {
assertEquals("goy", "Goyim".singularize())
}

}

0 comments on commit 76dd8c2

Please sign in to comment.