fix(internal): Fix plural acronyms in SnakeCase function (#16530)

This commit is contained in:
Dane Strandboge 2025-02-24 08:51:00 -06:00 committed by GitHub
parent 0dcdbe4ab4
commit c4fe39376c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 10 additions and 2 deletions

View File

@ -112,8 +112,15 @@ func SnakeCase(in string) string {
var out []rune var out []rune
for i := 0; i < length; i++ { for i := 0; i < length; i++ {
if i > 0 && unicode.IsUpper(runes[i]) && ((i+1 < length && unicode.IsLower(runes[i+1])) || unicode.IsLower(runes[i-1])) { if i > 0 && unicode.IsUpper(runes[i]) {
out = append(out, '_') prevLower := unicode.IsLower(runes[i-1])
nextLower := i+1 < length && unicode.IsLower(runes[i+1])
// Special case for plural acronyms
nextPlural := i+1 < length && runes[i+1] == 's'
if prevLower || (nextLower && !nextPlural) {
out = append(out, '_')
}
} }
out = append(out, unicode.ToLower(runes[i])) out = append(out, unicode.ToLower(runes[i]))
} }

View File

@ -34,6 +34,7 @@ var tests = []SnakeTest{
{"LinuxMOTD", "linux_motd"}, {"LinuxMOTD", "linux_motd"},
{"OMGWTFBBQ", "omgwtfbbq"}, {"OMGWTFBBQ", "omgwtfbbq"},
{"omg_wtf_bbq", "omg_wtf_bbq"}, {"omg_wtf_bbq", "omg_wtf_bbq"},
{"ConsumedLCUs", "consumed_lcus"},
} }
func TestSnakeCase(t *testing.T) { func TestSnakeCase(t *testing.T) {