-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
802d2d0
commit 594599b
Showing
4 changed files
with
44 additions
and
37 deletions.
There are no files selected for viewing
42 changes: 19 additions & 23 deletions
42
server-core/src/main/kotlin/com/lightningkite/lightningserver/casingUtils.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,25 @@ | ||
package com.lightningkite.lightningserver | ||
|
||
import java.util.* | ||
|
||
private val camelRegex = "[a-z][A-Z]".toRegex() | ||
private val snakeRegex = "_[a-zA-Z]".toRegex() | ||
private val kabobRegex = "-[a-zA-Z]".toRegex() | ||
internal fun String.humanize(): String = camelRegex.replace(this) { | ||
"${it.value[0]} ${it.value[1].uppercase()}" | ||
}.let { | ||
snakeRegex.replace(it) { | ||
" " + it.value[1].uppercase() | ||
} | ||
}.replaceFirstChar { it.uppercaseChar() }.trim() | ||
|
||
internal fun String.kabobCase(): String = camelRegex.replace(this) { | ||
"${it.value[0]}-${it.value[1]}" | ||
}.let { | ||
snakeRegex.replace(it) { | ||
"-" + it.value[1] | ||
val `casing separator regex` = Regex("([-_\\s]+([A-Z]*[a-z0-9]+))|([-_\\s]*[A-Z]+)") | ||
inline fun String.caseAlter(crossinline update: (after: String) -> String): String = | ||
`casing separator regex`.replace(this) { | ||
update(it.value.filter { !(it == '-' || it == '_' || it.isWhitespace()) }) | ||
} | ||
}.lowercase().trim() | ||
|
||
internal fun String.camelCase(): String = snakeRegex.replace(this) { | ||
"${it.value[0]}${it.value[1].uppercase()}" | ||
}.let { | ||
kabobRegex.replace(it) { | ||
it.value[1].uppercase() | ||
} | ||
}.lowercase().trim() | ||
private fun String.capitalize(): String = replaceFirstChar { if (it.isLowerCase()) it.titlecase(Locale.getDefault()) else it.toString() } | ||
|
||
fun String.titleCase() = | ||
caseAlter { " " + it.capitalize() }.capitalize() | ||
|
||
fun String.kabobCase() = caseAlter { "-$it" }.lowercase() | ||
fun String.snakeCase() = caseAlter { "_$it" }.lowercase() | ||
fun String.screamingSnakeCase() = caseAlter { "_$it" }.uppercase() | ||
fun String.camelCase() = caseAlter { it.capitalize() } | ||
fun String.pascalCase() = caseAlter { it.capitalize() }.replaceFirstChar { | ||
if (it.isLowerCase()) it.titlecase( | ||
Locale.getDefault() | ||
) else it.toString() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
server-core/src/test/kotlin/com/lightningkite/lightningserver/CasingUtilsKtTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package com.lightningkite.lightningserver | ||
|
||
import org.junit.Assert.* | ||
import org.junit.Test | ||
|
||
class CasingUtilsKtTest { | ||
@Test fun testQuick() { | ||
val dumb = "as--d_f-_ XdcCase ID" | ||
assertEquals("As D F Xdc Case ID", dumb.titleCase()) | ||
assertEquals("as-d-f-xdc-case-id", dumb.kabobCase()) | ||
assertEquals("as_d_f_xdc_case_id", dumb.snakeCase()) | ||
assertEquals("AS_D_F_XDC_CASE_ID", dumb.screamingSnakeCase()) | ||
assertEquals("asDFXdcCaseID", dumb.camelCase()) | ||
assertEquals("AsDFXdcCaseID", dumb.pascalCase()) | ||
} | ||
} |