BufPool Singular and Plural

This commit is contained in:
Patrick O'brien 2016-08-13 23:39:13 +10:00
parent e4800f9af8
commit ddb3aff125
2 changed files with 38 additions and 4 deletions

View file

@ -94,16 +94,48 @@ func Identifier(in int) string {
// Plural converts singular words to plural words (eg: person to people)
func Plural(name string) string {
buf := GetBuffer()
defer PutBuffer(buf)
splits := strings.Split(name, "_")
splits[len(splits)-1] = inflection.Plural(splits[len(splits)-1])
return strings.Join(splits, "_")
for i := 0; i < len(splits); i++ {
if i != 0 {
buf.WriteByte('_')
}
if i == len(splits)-1 {
buf.WriteString(inflection.Plural(splits[len(splits)-1]))
break
}
buf.WriteString(splits[i])
}
return buf.String()
}
// Singular converts plural words to singular words (eg: people to person)
func Singular(name string) string {
buf := GetBuffer()
defer PutBuffer(buf)
splits := strings.Split(name, "_")
splits[len(splits)-1] = inflection.Singular(splits[len(splits)-1])
return strings.Join(splits, "_")
for i := 0; i < len(splits); i++ {
if i != 0 {
buf.WriteByte('_')
}
if i == len(splits)-1 {
buf.WriteString(inflection.Singular(splits[len(splits)-1]))
break
}
buf.WriteString(splits[i])
}
return buf.String()
}
// TitleCase changes a snake-case variable name

View file

@ -124,6 +124,7 @@ func TestSingular(t *testing.T) {
{"hello_people", "hello_person"},
{"hello_person", "hello_person"},
{"friends", "friend"},
{"hello_there_people", "hello_there_person"},
}
for i, test := range tests {
@ -143,6 +144,7 @@ func TestPlural(t *testing.T) {
{"hello_person", "hello_people"},
{"friend", "friends"},
{"friends", "friends"},
{"hello_there_person", "hello_there_people"},
}
for i, test := range tests {