Skip to content

Commit 4efea3f

Browse files
Merge pull request #5 from ghost/improvements
2 parents 6b65d56 + 46462e6 commit 4efea3f

File tree

5 files changed

+69
-5
lines changed

5 files changed

+69
-5
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
"foot".pluralize() # => "feet"
2121
```
2222

23-
**Singuralization:**
23+
**Singularization:**
2424

2525
```kotlin
2626
"words".singularize() # => "word"

library/build.gradle

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ apply plugin: 'maven-publish'
1515

1616
dependencies {
1717
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
18+
testCompile 'junit:junit:4.13'
1819
}
1920
repositories {
2021
mavenCentral()

library/src/main/kotlin/com/cesarferreira/pluralize/Pluralize.kt

+7-4
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ private fun String.pluralizer(): String {
5050
var found = Pattern.compile(rule.component1(), Pattern.CASE_INSENSITIVE).matcher(this).replaceAll(rule.component2())
5151
val endsWith = exceptions().firstOrNull { this.endsWith(it.component1()) }
5252
if (endsWith != null) found = this.replace(endsWith.component1(), endsWith.component2())
53-
val exception = exceptions().firstOrNull() { this.equals(it.component1()) }
53+
val exception = exceptions().firstOrNull() { this.equals(it.component1(), true) }
5454
if (exception != null) found = exception.component2()
5555
return found
5656
}
@@ -59,7 +59,7 @@ private fun String.singularizer(): String {
5959
if (unCountable().contains(this.toLowerCase())) {
6060
return this
6161
}
62-
val exceptions = exceptions().firstOrNull() { this.equals(it.component2()) }
62+
val exceptions = exceptions().firstOrNull() { this.equals(it.component2(), true) }
6363

6464
if (exceptions != null) {
6565
return exceptions.component1()
@@ -147,7 +147,8 @@ fun exceptions(): List<Pair<String, String>> {
147147
"noumenon" to "noumena",
148148
"organon" to "organa",
149149
"asyndeton" to "asyndeta",
150-
"hyperbaton" to "hyperbata")
150+
"hyperbaton" to "hyperbata",
151+
"foot" to "feet")
151152
}
152153

153154
fun pluralizeRules(): List<Pair<String, String>> {
@@ -176,7 +177,9 @@ fun pluralizeRules(): List<Pair<String, String>> {
176177
"f$" to "ves",
177178
"fe$" to "ves",
178179
"um$" to "a",
179-
"on$" to "a")
180+
"on$" to "a",
181+
"tion" to "tions",
182+
"sion" to "sions")
180183
}
181184

182185
fun singularizeRules(): List<Pair<String, String>> {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.cesarferreira.pluralize
2+
3+
import org.junit.Assert.assertEquals
4+
import org.junit.Test
5+
6+
class PluralizationTest {
7+
8+
@Test
9+
fun `Should add default "s" suffix`() {
10+
assertEquals("posts", "post".pluralize())
11+
assertEquals("descriptions", "description".pluralize())
12+
assertEquals("colections", "colection".pluralize())
13+
assertEquals("versions", "version".pluralize())
14+
}
15+
16+
@Test
17+
fun `Should handle unCountable words`() {
18+
assertEquals("aircraft", "aircraft".pluralize())
19+
}
20+
21+
@Test
22+
fun `Should handle exception words`() {
23+
assertEquals("men", "man".pluralize())
24+
assertEquals("feet", "foot".pluralize())
25+
}
26+
27+
@Test
28+
fun `Should handle in case insensitive manner`() {
29+
assertEquals("people", "Person".pluralize())
30+
}
31+
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.cesarferreira.pluralize
2+
3+
import org.junit.Assert.assertEquals
4+
import org.junit.Test
5+
6+
class SingularizationTest {
7+
8+
@Test
9+
fun `Should remove default "s" suffix`() {
10+
assertEquals("word", "words".singularize())
11+
}
12+
13+
@Test
14+
fun `Should handle unCountable words`() {
15+
assertEquals("scissors", "scissors".singularize())
16+
}
17+
18+
@Test
19+
fun `Should handle exception words`() {
20+
assertEquals("person", "people".singularize())
21+
}
22+
23+
@Test
24+
fun `Should handle in case insensitive manner`() {
25+
assertEquals("goy", "Goyim".singularize())
26+
}
27+
28+
}

0 commit comments

Comments
 (0)