Skip to content

Commit

Permalink
Casing utils
Browse files Browse the repository at this point in the history
  • Loading branch information
UnknownJoe796 committed Oct 19, 2023
1 parent 802d2d0 commit 594599b
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 37 deletions.
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()
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@ import com.lightningkite.lightningdb.*
import com.lightningkite.lightningserver.core.ServerPath
import com.lightningkite.lightningserver.db.ModelRestEndpoints
import com.lightningkite.lightningserver.files.UploadEarlyEndpoint
import com.lightningkite.lightningserver.humanize
import com.lightningkite.lightningserver.titleCase
import com.lightningkite.lightningserver.kabobCase
import com.lightningkite.lightningserver.routes.fullUrl
import com.lightningkite.lightningserver.serialization.Serialization
import com.lightningkite.lightningserver.typed.Documentable
import com.lightningkite.lightningserver.typed.docGroup
import com.lightningkite.lightningserver.typed.uncontextualize
import kotlinx.serialization.*
import kotlinx.serialization.builtins.ArraySerializer
import kotlinx.serialization.descriptors.*
Expand Down Expand Up @@ -88,7 +87,7 @@ val lightningServerSchema: LightningServerSchema by lazy {
}.toList(),
models = ModelRestEndpoints.all.associate {
it.collectionName.kabobCase() to LightningServerSchemaModel(
collectionName = it.collectionName.humanize(),
collectionName = it.collectionName.titleCase(),
url = it.path.fullUrl(),
ref = builder.refString(it.info.serialization.serializer),
conditionRef = builder.refString(Condition.serializer(it.info.serialization.serializer)),
Expand Down Expand Up @@ -431,15 +430,15 @@ class JsonSchemaBuilder(

SerialKind.ENUM -> defining(serializer) {
JsonSchemaType(
title = ser.descriptor.serialName.substringBefore('<').substringAfterLast('.').humanize(),
title = ser.descriptor.serialName.substringBefore('<').substringAfterLast('.').titleCase(),
type = JsonType3(JsonType2.STRING),
oneOf = (0 until ser.descriptor.elementsCount)
.map {
val value = ser.descriptor.getElementName(it)
JsonSchemaType(
title = ser.descriptor.getElementAnnotations(it).filterIsInstance<DisplayName>()
.firstOrNull()?.text
?: value.humanize(),
?: value.titleCase(),
const = value
)
}
Expand All @@ -460,10 +459,10 @@ class JsonSchemaBuilder(

StructureKind.CLASS -> defining(serializer) {
JsonSchemaType(
title = ser.descriptor.serialName.substringBefore('<').substringAfterLast('.').humanize(),
title = ser.descriptor.serialName.substringBefore('<').substringAfterLast('.').titleCase(),
type = JsonType3(JsonType2.OBJECT),
properties = ser.serializableProperties?.associate {
val propTitle = it.name.humanize()
val propTitle = it.name.titleCase()
it.name to get(
it.serializer,
ser.descriptor.getElementIndex(it.name).takeUnless { it == -1 }?.let { ser.descriptor.getElementAnnotations(it) } ?: listOf(),
Expand All @@ -473,7 +472,7 @@ class JsonSchemaBuilder(
)
} ?: ser.tryChildSerializers()?.withIndex()?.associate {
val name = ser.descriptor.getElementName(it.index)
val propTitle = name.humanize()
val propTitle = name.titleCase()
name to get(
it.value,
ser.descriptor.getElementAnnotations(it.index),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
package com.lightningkite.lightningserver.jsonschema

import com.lightningkite.lightningdb.HasId
import com.lightningkite.lightningserver.core.ContentType
import com.lightningkite.lightningserver.core.ServerPath
import com.lightningkite.lightningserver.http.HttpMethod
import com.lightningkite.lightningserver.humanize
import com.lightningkite.lightningserver.titleCase
import com.lightningkite.lightningserver.serialization.Serialization
import com.lightningkite.lightningserver.serialization.encodeToFormData
import com.lightningkite.lightningserver.settings.generalSettings
import com.lightningkite.lightningserver.typed.ApiEndpoint
import com.lightningkite.lightningserver.typed.Documentable
Expand All @@ -16,10 +14,8 @@ import kotlinx.serialization.KSerializer
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import kotlinx.serialization.builtins.serializer
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.JsonElement
import kotlinx.serialization.json.JsonNull
import kotlinx.serialization.json.JsonPrimitive

@Serializable
data class OpenApiRoot(
Expand Down Expand Up @@ -160,7 +156,7 @@ private fun <T> make(type: KSerializer<T>, item: T): Map<String, OpenApiExample>

private fun ApiEndpoint<*, *, *, *>.openApi(builder: JsonSchemaBuilder): OpenApiOperation =
OpenApiOperation(
summary = (this.docGroup?.let { it.humanize() + " " } ?: "") + " - " + summary,
summary = (this.docGroup?.let { it.titleCase() + " " } ?: "") + " - " + summary,
description = description,
tags = listOfNotNull(this.docGroup),
operationId = (this.docGroup ?: "") + "_" + this.functionName,
Expand Down
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())
}
}

0 comments on commit 594599b

Please sign in to comment.