fix(internal): Fix plural acronyms in SnakeCase function (#16530)
This commit is contained in:
parent
0dcdbe4ab4
commit
c4fe39376c
|
|
@ -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]))
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -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) {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue