Skip to content

Commit e495e2d

Browse files
committedOct 12, 2023
add helper.UnderlineToCamel
1 parent 40f743c commit e495e2d

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed
 

‎helper.go

+19
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,22 @@ func MessageUnescape(text string) string {
4747
text = strings.ReplaceAll(text, ">", ">")
4848
return text
4949
}
50+
51+
// UnderlineToCamel convert abc_def to AbcDef
52+
func UnderlineToCamel(s string) string {
53+
sb := strings.Builder{}
54+
isnextupper := true
55+
for _, c := range []byte(strings.ToLower(s)) {
56+
if c == '_' {
57+
isnextupper = true
58+
continue
59+
}
60+
if isnextupper {
61+
sb.WriteString(strings.ToUpper(string(c)))
62+
isnextupper = false
63+
continue
64+
}
65+
sb.WriteByte(c)
66+
}
67+
return sb.String()
68+
}

‎helper_test.go

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package nano
2+
3+
import "testing"
4+
5+
func TestUnderlineToCamel(t *testing.T) {
6+
x := UnderlineToCamel("GUILD_CREATE")
7+
if x != "GuildCreate" {
8+
t.Fatal("expected GuildCreate but got", x)
9+
}
10+
x = UnderlineToCamel("GUILD_MEMBER_UPDATE")
11+
if x != "GuildMemberUpdate" {
12+
t.Fatal("expected GuildMemberUpdate but got", x)
13+
}
14+
x = UnderlineToCamel("OPEN_FORUM_THREAD_CREATE")
15+
if x != "OpenForumThreadCreate" {
16+
t.Fatal("expected OpenForumThreadCreate but got", x)
17+
}
18+
x = UnderlineToCamel("AUDIO_OR_LIVE_CHANNEL_MEMBER_ENTER")
19+
if x != "AudioOrLiveChannelMemberEnter" {
20+
t.Fatal("expected AudioOrLiveChannelMemberEnter but got", x)
21+
}
22+
}

0 commit comments

Comments
 (0)
Please sign in to comment.