convertstringtocamelcase.go 724 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. package kata
  2. import (
  3. "fmt"
  4. "strings"
  5. "unicode"
  6. )
  7. func ToCamelCase(s string) string {
  8. // Only handle the underscore case.
  9. u := strings.ReplaceAll(s, `-`, `_`)
  10. fmt.Println("Checking", u)
  11. res := make([]rune, 0, len(u))
  12. atFirst := true
  13. atInitial := true
  14. for _, r := range u {
  15. fmt.Printf("%c: ", r)
  16. if r == '_' {
  17. fmt.Println("Skipping _, setting Initial")
  18. atInitial = true
  19. continue
  20. }
  21. if atFirst {
  22. fmt.Println("First")
  23. res = append(res, r)
  24. atFirst = false
  25. atInitial = false
  26. continue
  27. } else if atInitial {
  28. fmt.Println("atInitial")
  29. res = append(res, unicode.ToUpper(r))
  30. } else {
  31. fmt.Println("not")
  32. res = append(res, r)
  33. }
  34. atInitial = false
  35. }
  36. return string(res)
  37. }