Skip to content

Commit fe29da0

Browse files
Merge pull request #20 from simplecloudapp/feat/pretty-name-placeholder
feat: pretty name placeholder
2 parents bb4b203 + f8e3e5c commit fe29da0

File tree

7 files changed

+150
-3
lines changed

7 files changed

+150
-3
lines changed

build.gradle.kts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ subprojects {
2828
apply(plugin = "net.thebugmc.gradle.sonatype-central-portal-publisher")
2929

3030
dependencies {
31-
compileOnly(rootProject.libs.kotlinJvm)
31+
testImplementation(rootProject.libs.kotlin.test)
32+
compileOnly(rootProject.libs.kotlin.jvm)
3233
compileOnly(rootProject.libs.bundles.simpleCloudController)
3334
compileOnly(rootProject.libs.bundles.adventure)
3435
}

gradle/libs.versions.toml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
[versions]
2-
kotlin = "2.0.0"
2+
kotlin = "2.0.20"
33
simpleCloudController = "0.0.30-dev.e3e27fc"
44
sonatype-central-portal-publisher = "1.2.3"
55
adventure = "4.18.0"
66

77
[libraries]
8-
kotlinJvm = { module = "org.jetbrains.kotlin:kotlin-stdlib-jdk8", version.ref = "kotlin" }
8+
kotlin-jvm = { module = "org.jetbrains.kotlin:kotlin-stdlib-jdk8", version.ref = "kotlin" }
9+
kotlin-test = { module = "org.jetbrains.kotlin:kotlin-test", version.ref = "kotlin" }
910

1011
simpleCloudControllerApi = { module = "app.simplecloud.controller:controller-api", version.ref = "simpleCloudController" }
1112
simpleCloudControllerShared = { module = "app.simplecloud.controller:controller-shared", version.ref = "simpleCloudController" }

plugin-shared/src/main/kotlin/app/simplecloud/plugin/api/shared/pattern/ServerPatternIdentifier.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package app.simplecloud.plugin.api.shared.pattern
33
import app.simplecloud.controller.api.ControllerApi
44
import app.simplecloud.controller.shared.group.Group
55
import app.simplecloud.controller.shared.server.Server
6+
import app.simplecloud.plugin.api.shared.pretty.StringPrettifier
67

78
/**
89
* @author Niklas Nieberler
@@ -42,6 +43,7 @@ class ServerPatternIdentifier(
4243
fun parseServerToPattern(server: Server): String {
4344
return this.pattern
4445
.replace("<group_name>", server.group)
46+
.replace("<group_pretty_name>", server.properties["pretty-name"] ?: StringPrettifier.prettify(server.group))
4547
.replace("<id>", server.uniqueId)
4648
.replace("<unique_id>", server.uniqueId)
4749
.replace("<numerical_id>", server.numericalId.toString())

plugin-shared/src/main/kotlin/app/simplecloud/plugin/api/shared/placeholder/single/SingleServerPlaceholderExecutor.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package app.simplecloud.plugin.api.shared.placeholder.single
33
import app.simplecloud.controller.api.ControllerApi
44
import app.simplecloud.controller.shared.server.Server
55
import app.simplecloud.plugin.api.shared.placeholder.async.AsyncPlaceholder
6+
import app.simplecloud.plugin.api.shared.pretty.StringPrettifier
67

78
/**
89
* @author Niklas Nieberler
@@ -14,6 +15,9 @@ class SingleServerPlaceholderExecutor : SinglePlaceholderExecutor<Server> {
1415
AsyncPlaceholder("id") { it.uniqueId },
1516
AsyncPlaceholder("numerical_id") { it.numericalId },
1617
AsyncPlaceholder("group_name") { it.group },
18+
AsyncPlaceholder("group_pretty_name") {
19+
it.properties["pretty-name"] ?: StringPrettifier.prettify(it.group)
20+
},
1721
AsyncPlaceholder("type") { it.type },
1822
AsyncPlaceholder("state") { it.state },
1923
AsyncPlaceholder("ip") { it.ip },
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package app.simplecloud.plugin.api.shared.pretty
2+
3+
enum class PrettifyCase {
4+
TITLE,
5+
CAMEL,
6+
PASCAL,
7+
SENTENCE,
8+
KEBAB,
9+
SNAKE
10+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package app.simplecloud.plugin.api.shared.pretty
2+
3+
object StringPrettifier {
4+
5+
/**
6+
* Converts various string formats to different cases
7+
*/
8+
fun prettify(input: String, case: PrettifyCase = PrettifyCase.TITLE): String {
9+
return when (case) {
10+
PrettifyCase.TITLE -> toTitleCase(input)
11+
PrettifyCase.CAMEL -> toCamelCase(input)
12+
PrettifyCase.PASCAL -> toPascalCase(input)
13+
PrettifyCase.SENTENCE -> toSentenceCase(input)
14+
PrettifyCase.KEBAB -> toKebabCase(input)
15+
PrettifyCase.SNAKE -> toSnakeCase(input)
16+
}
17+
}
18+
19+
private fun toTitleCase(input: String): String {
20+
return input.split(Regex("[\\s_-]"))
21+
.filter { it.isNotEmpty() }
22+
.joinToString(" ") { word ->
23+
word.lowercase().replaceFirstChar { it.uppercase() }
24+
}
25+
}
26+
27+
private fun toCamelCase(input: String): String {
28+
return input.split(Regex("[\\s_-]"))
29+
.filter { it.isNotEmpty() }
30+
.mapIndexed { index, word ->
31+
if (index == 0) word.lowercase()
32+
else word.lowercase().replaceFirstChar { it.uppercase() }
33+
}
34+
.joinToString("")
35+
}
36+
37+
private fun toPascalCase(input: String): String {
38+
return input.split(Regex("[\\s_-]"))
39+
.filter { it.isNotEmpty() }
40+
.joinToString("") { word ->
41+
word.lowercase().replaceFirstChar { it.uppercase() }
42+
}
43+
}
44+
45+
private fun toSentenceCase(input: String): String {
46+
return input.split(Regex("[\\s_-]"))
47+
.filter { it.isNotEmpty() }
48+
.joinToString(" ") { it.lowercase() }
49+
.replaceFirstChar { it.uppercase() }
50+
}
51+
52+
private fun toKebabCase(input: String): String {
53+
return input.split(Regex("[\\s_]"))
54+
.filter { it.isNotEmpty() }
55+
.joinToString("-") { it.lowercase() }
56+
}
57+
58+
private fun toSnakeCase(input: String): String {
59+
return input.split(Regex("[\\s-]"))
60+
.filter { it.isNotEmpty() }
61+
.joinToString("_") { it.lowercase() }
62+
}
63+
64+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package app.simplecloud.plugin.api.shared.pretty
2+
3+
import kotlin.test.*
4+
5+
class StringPrettifierTest {
6+
7+
@Test
8+
fun testTitleCaseConversion() {
9+
assertEquals("Hello World", StringPrettifier.prettify("hello-world"))
10+
assertEquals("Hello World", StringPrettifier.prettify("hello_world"))
11+
assertEquals("Hello World", StringPrettifier.prettify("hello world"))
12+
assertEquals("Hello Beautiful World", StringPrettifier.prettify("hello-beautiful_world"))
13+
}
14+
15+
@Test
16+
fun testCamelCaseConversion() {
17+
assertEquals("helloWorld", StringPrettifier.prettify("hello-world", PrettifyCase.CAMEL))
18+
assertEquals("helloWorld", StringPrettifier.prettify("hello_world", PrettifyCase.CAMEL))
19+
assertEquals("helloBeautifulWorld", StringPrettifier.prettify("hello beautiful world", PrettifyCase.CAMEL))
20+
assertEquals("myVariableName", StringPrettifier.prettify("my-variable-name", PrettifyCase.CAMEL))
21+
}
22+
23+
@Test
24+
fun testPascalCaseConversion() {
25+
assertEquals("HelloWorld", StringPrettifier.prettify("hello-world", PrettifyCase.PASCAL))
26+
assertEquals("HelloWorld", StringPrettifier.prettify("hello_world", PrettifyCase.PASCAL))
27+
assertEquals("HelloBeautifulWorld", StringPrettifier.prettify("hello beautiful world", PrettifyCase.PASCAL))
28+
assertEquals("MyVariableName", StringPrettifier.prettify("my-variable-name", PrettifyCase.PASCAL))
29+
}
30+
31+
@Test
32+
fun testSentenceCaseConversion() {
33+
assertEquals("Hello world", StringPrettifier.prettify("hello-world", PrettifyCase.SENTENCE))
34+
assertEquals("Hello beautiful world", StringPrettifier.prettify("hello_beautiful_world", PrettifyCase.SENTENCE))
35+
assertEquals("My variable name", StringPrettifier.prettify("my-variable-name", PrettifyCase.SENTENCE))
36+
}
37+
38+
@Test
39+
fun testKebabCaseConversion() {
40+
assertEquals("hello-world", StringPrettifier.prettify("hello world", PrettifyCase.KEBAB))
41+
assertEquals("hello-beautiful-world", StringPrettifier.prettify("hello_beautiful_world", PrettifyCase.KEBAB))
42+
assertEquals("my-variable-name", StringPrettifier.prettify("my variable name", PrettifyCase.KEBAB))
43+
}
44+
45+
@Test
46+
fun testSnakeCaseConversion() {
47+
assertEquals("hello_world", StringPrettifier.prettify("hello-world", PrettifyCase.SNAKE))
48+
assertEquals("hello_beautiful_world", StringPrettifier.prettify("hello beautiful world", PrettifyCase.SNAKE))
49+
assertEquals("my_variable_name", StringPrettifier.prettify("my-variable-name", PrettifyCase.SNAKE))
50+
}
51+
52+
@Test
53+
fun testEmptyStringHandling() {
54+
assertEquals("", StringPrettifier.prettify(""))
55+
assertEquals("", StringPrettifier.prettify("", PrettifyCase.CAMEL))
56+
assertEquals("", StringPrettifier.prettify("", PrettifyCase.PASCAL))
57+
}
58+
59+
@Test
60+
fun testMultipleDelimiterHandling() {
61+
assertEquals("Hello Beautiful World", StringPrettifier.prettify("hello-beautiful_world"))
62+
assertEquals("helloBeautifulWorld", StringPrettifier.prettify("hello-beautiful_world", PrettifyCase.CAMEL))
63+
assertEquals("HelloBeautifulWorld", StringPrettifier.prettify("hello-beautiful_world", PrettifyCase.PASCAL))
64+
}
65+
}

0 commit comments

Comments
 (0)