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:
19
helper.go
19
helper.go
@@ -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
22
helper_test.go
Normal 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)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user