titleize.go 772 B

123456789101112131415161718192021222324252627282930
  1. package flect
  2. import (
  3. "strings"
  4. "unicode"
  5. )
  6. // Titleize will capitalize the start of each part
  7. // "Nice to see you!" = "Nice To See You!"
  8. // "i've read a book! have you?" = "I've Read A Book! Have You?"
  9. // "This is `code` ok" = "This Is `code` OK"
  10. func Titleize(s string) string {
  11. return New(s).Titleize().String()
  12. }
  13. // Titleize will capitalize the start of each part
  14. // "Nice to see you!" = "Nice To See You!"
  15. // "i've read a book! have you?" = "I've Read A Book! Have You?"
  16. // "This is `code` ok" = "This Is `code` OK"
  17. func (i Ident) Titleize() Ident {
  18. var parts []string
  19. for _, part := range i.Parts {
  20. x := string(unicode.ToTitle(rune(part[0])))
  21. if len(part) > 1 {
  22. x += part[1:]
  23. }
  24. parts = append(parts, x)
  25. }
  26. return New(strings.Join(parts, " "))
  27. }