titleize.go 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738
  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. // TODO: we need to reconsider the design.
  20. // this approach preserves inline code block as is but it also
  21. // preserves the other words start with a special character.
  22. // I would prefer: "*wonderful* world" to be "*Wonderful* World"
  23. for _, part := range i.Parts {
  24. // CAUTION: in unicode, []rune(str)[0] is not rune(str[0])
  25. runes := []rune(part)
  26. x := string(unicode.ToTitle(runes[0]))
  27. if len(runes) > 1 {
  28. x += string(runes[1:])
  29. }
  30. parts = append(parts, x)
  31. }
  32. return New(strings.Join(parts, " "))
  33. }