From 76dd8c22eb4504b56ad2159c1e443865a1cbd116 Mon Sep 17 00:00:00 2001 From: Oleg Michailov Date: Wed, 5 Aug 2020 17:10:50 +0300 Subject: [PATCH] Handling some words mentioned in #4 --- .../kotlin/com/cesarferreira/pluralize/Pluralize.kt | 11 +++++++---- .../com/cesarferreira/pluralize/PluralizationTest.kt | 9 +++++++++ .../cesarferreira/pluralize/SingularizationTest.kt | 5 +++++ 3 files changed, 21 insertions(+), 4 deletions(-) diff --git a/library/src/main/kotlin/com/cesarferreira/pluralize/Pluralize.kt b/library/src/main/kotlin/com/cesarferreira/pluralize/Pluralize.kt index 159c823..df7667f 100644 --- a/library/src/main/kotlin/com/cesarferreira/pluralize/Pluralize.kt +++ b/library/src/main/kotlin/com/cesarferreira/pluralize/Pluralize.kt @@ -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 } @@ -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() @@ -147,7 +147,8 @@ fun exceptions(): List> { "noumenon" to "noumena", "organon" to "organa", "asyndeton" to "asyndeta", - "hyperbaton" to "hyperbata") + "hyperbaton" to "hyperbata", + "foot" to "feet") } fun pluralizeRules(): List> { @@ -176,7 +177,9 @@ fun pluralizeRules(): List> { "f$" to "ves", "fe$" to "ves", "um$" to "a", - "on$" to "a") + "on$" to "a", + "tion" to "tions", + "sion" to "sions") } fun singularizeRules(): List> { diff --git a/library/src/test/kotlin/com/cesarferreira/pluralize/PluralizationTest.kt b/library/src/test/kotlin/com/cesarferreira/pluralize/PluralizationTest.kt index 35df4d0..3ab36ff 100644 --- a/library/src/test/kotlin/com/cesarferreira/pluralize/PluralizationTest.kt +++ b/library/src/test/kotlin/com/cesarferreira/pluralize/PluralizationTest.kt @@ -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 @@ -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()) } } diff --git a/library/src/test/kotlin/com/cesarferreira/pluralize/SingularizationTest.kt b/library/src/test/kotlin/com/cesarferreira/pluralize/SingularizationTest.kt index c89700f..857b484 100644 --- a/library/src/test/kotlin/com/cesarferreira/pluralize/SingularizationTest.kt +++ b/library/src/test/kotlin/com/cesarferreira/pluralize/SingularizationTest.kt @@ -20,4 +20,9 @@ class SingularizationTest { assertEquals("person", "people".singularize()) } + @Test + fun `Should handle in case insensitive manner`() { + assertEquals("goy", "Goyim".singularize()) + } + }