string.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. package stringx
  2. import (
  3. "bytes"
  4. "strings"
  5. "unicode"
  6. )
  7. // String provides for converting the source text into other spell case,like lower,snake,camel
  8. type String struct {
  9. source string
  10. }
  11. // From converts the input text to String and returns it
  12. func From(data string) String {
  13. return String{source: data}
  14. }
  15. // IsEmptyOrSpace returns true if the length of the string value is 0 after call strings.TrimSpace, or else returns false
  16. func (s String) IsEmptyOrSpace() bool {
  17. if len(s.source) == 0 {
  18. return true
  19. }
  20. if strings.TrimSpace(s.source) == "" {
  21. return true
  22. }
  23. return false
  24. }
  25. // Lower calls the strings.ToLower
  26. func (s String) Lower() string {
  27. return strings.ToLower(s.source)
  28. }
  29. // Upper calls the strings.ToUpper
  30. func (s String) Upper() string {
  31. return strings.ToUpper(s.source)
  32. }
  33. // ReplaceAll calls the strings.ReplaceAll
  34. func (s String) ReplaceAll(old, new string) string {
  35. return strings.ReplaceAll(s.source, old, new)
  36. }
  37. // Source returns the source string value
  38. func (s String) Source() string {
  39. return s.source
  40. }
  41. // Title calls the strings.Title
  42. func (s String) Title() string {
  43. if s.IsEmptyOrSpace() {
  44. return s.source
  45. }
  46. return strings.Title(s.source)
  47. }
  48. // ToCamel converts the input text into camel case
  49. func (s String) ToCamel() string {
  50. list := s.splitBy(func(r rune) bool {
  51. return r == '_'
  52. }, true)
  53. var target []string
  54. for _, item := range list {
  55. target = append(target, From(item).Title())
  56. }
  57. return strings.Join(target, "")
  58. }
  59. // ToSnake converts the input text into snake case
  60. func (s String) ToSnake() string {
  61. list := s.splitBy(unicode.IsUpper, false)
  62. var target []string
  63. for _, item := range list {
  64. target = append(target, From(item).Lower())
  65. }
  66. return strings.Join(target, "_")
  67. }
  68. // Untitle return the original string if rune is not letter at index 0
  69. func (s String) Untitle() string {
  70. if s.IsEmptyOrSpace() {
  71. return s.source
  72. }
  73. r := rune(s.source[0])
  74. if !unicode.IsUpper(r) && !unicode.IsLower(r) {
  75. return s.source
  76. }
  77. return string(unicode.ToLower(r)) + s.source[1:]
  78. }
  79. // it will not ignore spaces
  80. func (s String) splitBy(fn func(r rune) bool, remove bool) []string {
  81. if s.IsEmptyOrSpace() {
  82. return nil
  83. }
  84. var list []string
  85. buffer := new(bytes.Buffer)
  86. for _, r := range s.source {
  87. if fn(r) {
  88. if buffer.Len() != 0 {
  89. list = append(list, buffer.String())
  90. buffer.Reset()
  91. }
  92. if !remove {
  93. buffer.WriteRune(r)
  94. }
  95. continue
  96. }
  97. buffer.WriteRune(r)
  98. }
  99. if buffer.Len() != 0 {
  100. list = append(list, buffer.String())
  101. }
  102. return list
  103. }