1
0
mirror of https://github.com/fumiama/NanoBot.git synced 2026-06-05 02:30:23 +08:00

add helper.UnderlineToCamel

This commit is contained in:
源文雨
2023-10-13 00:45:21 +09:00
parent 40f743cf46
commit e495e2d4d0
2 changed files with 41 additions and 0 deletions

View File

@@ -47,3 +47,22 @@ func MessageUnescape(text string) string {
text = strings.ReplaceAll(text, ">", ">")
return text
}
// UnderlineToCamel convert abc_def to AbcDef
func UnderlineToCamel(s string) string {
sb := strings.Builder{}
isnextupper := true
for _, c := range []byte(strings.ToLower(s)) {
if c == '_' {
isnextupper = true
continue
}
if isnextupper {
sb.WriteString(strings.ToUpper(string(c)))
isnextupper = false
continue
}
sb.WriteByte(c)
}
return sb.String()
}

22
helper_test.go Normal file
View File

@@ -0,0 +1,22 @@
package nano
import "testing"
func TestUnderlineToCamel(t *testing.T) {
x := UnderlineToCamel("GUILD_CREATE")
if x != "GuildCreate" {
t.Fatal("expected GuildCreate but got", x)
}
x = UnderlineToCamel("GUILD_MEMBER_UPDATE")
if x != "GuildMemberUpdate" {
t.Fatal("expected GuildMemberUpdate but got", x)
}
x = UnderlineToCamel("OPEN_FORUM_THREAD_CREATE")
if x != "OpenForumThreadCreate" {
t.Fatal("expected OpenForumThreadCreate but got", x)
}
x = UnderlineToCamel("AUDIO_OR_LIVE_CHANNEL_MEMBER_ENTER")
if x != "AudioOrLiveChannelMemberEnter" {
t.Fatal("expected AudioOrLiveChannelMemberEnter but got", x)
}
}