Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

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

Merged
merged 1 commit into from
Feb 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions internal/internal.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,15 @@ func SnakeCase(in string) string {

var out []rune
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])) {
out = append(out, '_')
if i > 0 && unicode.IsUpper(runes[i]) {
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]))
}
Expand Down
1 change: 1 addition & 0 deletions internal/internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ var tests = []SnakeTest{
{"LinuxMOTD", "linux_motd"},
{"OMGWTFBBQ", "omgwtfbbq"},
{"omg_wtf_bbq", "omg_wtf_bbq"},
{"ConsumedLCUs", "consumed_lcus"},
}

func TestSnakeCase(t *testing.T) {
Expand Down
Loading