Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validation for capitalizeFirstChar #14

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import com.cesarferreira.pluralize.pluralize
import com.cesarferreira.pluralize.singularize
import com.google.common.base.Strings
import me.carleslc.kotlinextensions.standard.letOrElse
import java.lang.IllegalArgumentException
import java.util.*

inline infix operator fun String.times(n: Int): String = Strings.repeat(this, n)
Expand Down Expand Up @@ -49,16 +50,24 @@ inline fun String.replace(ignoreCase: Boolean = false, vararg vars: Pair<String,
inline fun String.remove(substring: String) = replace(substring, "")

/**
* Capitalizes first character of every word.
* Words are separated by one of more space characters.
* Capitalized the first character of every word where word is delimited by a space character.
*/
inline fun String.capitalizeFirstChar() =
lowercase(Locale.getDefault())
.split(" +".toRegex())
.joinToString(" ", "") { d ->
d.replaceRange(0, 1, d.first().uppercaseChar().toString())
fun String.capitalizeFirstChar(saveSpacing: Boolean = true): String {

if (isEmpty() || isBlank()) throw IllegalArgumentException("Blank or empty String cannot be used.")

val deliminator = if (saveSpacing) " " else " +"

return this
.lowercase(Locale.getDefault())
.split(deliminator.toRegex())
.joinToString(" ") {
if (it.isEmpty()) it
else it.replaceRange(0, 1, it.first().uppercaseChar().toString())
}

}

/**
* Removes non-numerical characters from a string.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.BeforeAll
import org.junit.jupiter.api.Test
import strings.*
import java.lang.IllegalArgumentException
import java.util.*

class StringsExtensionsKtTest {
Expand Down Expand Up @@ -189,17 +190,27 @@ class StringsExtensionsKtTest {
@Test
fun capitalizeFirstCharTest() {

val expected = "Petro Canada"

val result1 = "petro canada".capitalizeFirstChar()
val result2 = "PETRO CANADA".capitalizeFirstChar()
val result3 = "PEtRO CAnaDA".capitalizeFirstChar()
val result4 = "pEtRO cAnaDA".capitalizeFirstChar()

Assertions.assertEquals(expected, result1)
Assertions.assertEquals(expected, result2)
Assertions.assertEquals(expected, result3)
Assertions.assertEquals(expected, result4)
val result1 = "aa bb cc dd".capitalizeFirstChar()
val result2 = "aa bb cc dd".capitalizeFirstChar()
val result3 = " aa bb ".capitalizeFirstChar()
val result4 = " aa bb ".capitalizeFirstChar()
val result5 = "aa bb cc dd".capitalizeFirstChar(false)
val result6 = "aa bb cc dd".capitalizeFirstChar(false)
val result7 = " aa bb ".capitalizeFirstChar(false)
val result8 = " aa bb ".capitalizeFirstChar(false)

Assertions.assertEquals("Aa Bb Cc Dd", result1)
Assertions.assertEquals("Aa Bb Cc Dd", result2)
Assertions.assertEquals(" Aa Bb ", result3)
Assertions.assertEquals(" Aa Bb ", result4)
Assertions.assertEquals("Aa Bb Cc Dd", result5)
Assertions.assertEquals("Aa Bb Cc Dd", result6)
Assertions.assertEquals(" Aa Bb ", result7)
Assertions.assertEquals(" Aa Bb ", result8)

Assertions.assertThrows(IllegalArgumentException::class.java) { "".capitalizeFirstChar() }
Assertions.assertThrows(IllegalArgumentException::class.java) { " ".capitalizeFirstChar() }
Assertions.assertThrows(IllegalArgumentException::class.java) { " ".capitalizeFirstChar() }

}

Expand Down