github.com/kyleu/dbaudit@v0.0.2-0.20240321155047-ff2f2c940496/app/util/plural.go (about) 1 // Package util - Content managed by Project Forge, see [projectforge.md] for details. 2 package util 3 4 import ( 5 "fmt" 6 "unicode" 7 8 "github.com/gertd/go-pluralize" 9 ) 10 11 var plrl *pluralize.Client 12 13 func plrlSvc() { 14 if plrl == nil { 15 plrl = pluralize.NewClient() 16 } 17 } 18 19 func StringToPlural(s string) string { 20 plrlSvc() 21 ret := plrl.Plural(s) 22 if len(ret) < 3 { 23 return ret 24 } 25 if ret[len(ret)-1] == 'S' { 26 runes := []rune(ret) 27 c2 := runes[len(runes)-2] 28 c3 := runes[len(runes)-3] 29 if unicode.IsUpper(c2) && unicode.IsUpper(c3) { 30 runes[len(runes)-1] = 's' 31 ret = string(runes) 32 } 33 } 34 return ret 35 } 36 37 func StringToSingular(s string) string { 38 plrlSvc() 39 return plrl.Singular(s) 40 } 41 42 func StringForms(s string) (string, string) { 43 return StringToSingular(s), StringToPlural(s) 44 } 45 46 func StringPlural(count int, s string) string { 47 if count == 1 || count == -1 { 48 return fmt.Sprintf("%d %s", count, StringToSingular(s)) 49 } 50 return fmt.Sprintf("%d %s", count, StringToPlural(s)) 51 }