humanize.go 759 B

1234567891011121314151617181920212223242526272829303132333435
  1. package flect
  2. import (
  3. "strings"
  4. )
  5. // Humanize returns first letter of sentence capitalized.
  6. // Common acronyms are capitalized as well.
  7. // Other capital letters in string are left as provided.
  8. // employee_salary = Employee salary
  9. // employee_id = employee ID
  10. // employee_mobile_number = Employee mobile number
  11. // first_Name = First Name
  12. // firstName = First Name
  13. func Humanize(s string) string {
  14. return New(s).Humanize().String()
  15. }
  16. // Humanize First letter of sentence capitalized
  17. func (i Ident) Humanize() Ident {
  18. if len(i.Original) == 0 {
  19. return New("")
  20. }
  21. var parts []string
  22. for index, part := range i.Parts {
  23. if index == 0 {
  24. part = strings.Title(i.Parts[0])
  25. }
  26. parts = xappend(parts, part)
  27. }
  28. return New(strings.Join(parts, " "))
  29. }