Skip to content

Commit c4fe393

Browse files
authored
fix(internal): Fix plural acronyms in SnakeCase function (#16530)
1 parent 0dcdbe4 commit c4fe393

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

internal/internal.go

+9-2
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,15 @@ func SnakeCase(in string) string {
112112

113113
var out []rune
114114
for i := 0; i < length; i++ {
115-
if i > 0 && unicode.IsUpper(runes[i]) && ((i+1 < length && unicode.IsLower(runes[i+1])) || unicode.IsLower(runes[i-1])) {
116-
out = append(out, '_')
115+
if i > 0 && unicode.IsUpper(runes[i]) {
116+
prevLower := unicode.IsLower(runes[i-1])
117+
nextLower := i+1 < length && unicode.IsLower(runes[i+1])
118+
// Special case for plural acronyms
119+
nextPlural := i+1 < length && runes[i+1] == 's'
120+
121+
if prevLower || (nextLower && !nextPlural) {
122+
out = append(out, '_')
123+
}
117124
}
118125
out = append(out, unicode.ToLower(runes[i]))
119126
}

internal/internal_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ var tests = []SnakeTest{
3434
{"LinuxMOTD", "linux_motd"},
3535
{"OMGWTFBBQ", "omgwtfbbq"},
3636
{"omg_wtf_bbq", "omg_wtf_bbq"},
37+
{"ConsumedLCUs", "consumed_lcus"},
3738
}
3839

3940
func TestSnakeCase(t *testing.T) {

0 commit comments

Comments
 (0)