You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

94 lines
3.0 KiB

  1. /*
  2. * The MIT License (MIT)
  3. *
  4. * Copyright (c) 2015 Ian Coleman
  5. * Copyright (c) 2018 Ma_124, <github.com/Ma124>
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a copy
  8. * of this software and associated documentation files (the "Software"), to deal
  9. * in the Software without restriction, including without limitation the rights
  10. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. * copies of the Software, and to permit persons to whom the Software is
  12. * furnished to do so, Subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be included in all
  15. * copies or Substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  23. * SOFTWARE.
  24. */
  25. package strcase
  26. import (
  27. "strings"
  28. )
  29. // ToSnake converts a string to snake_case
  30. func ToSnake(s string) string {
  31. return ToDelimited(s, '_')
  32. }
  33. // ToScreamingSnake converts a string to SCREAMING_SNAKE_CASE
  34. func ToScreamingSnake(s string) string {
  35. return ToScreamingDelimited(s, '_', true)
  36. }
  37. // ToKebab converts a string to kebab-case
  38. func ToKebab(s string) string {
  39. return ToDelimited(s, '-')
  40. }
  41. // ToScreamingKebab converts a string to SCREAMING-KEBAB-CASE
  42. func ToScreamingKebab(s string) string {
  43. return ToScreamingDelimited(s, '-', true)
  44. }
  45. // ToDelimited converts a string to delimited.snake.case (in this case `del = '.'`)
  46. func ToDelimited(s string, del uint8) string {
  47. return ToScreamingDelimited(s, del, false)
  48. }
  49. // ToScreamingDelimited converts a string to SCREAMING.DELIMITED.SNAKE.CASE (in this case `del = '.'; screaming = true`) or delimited.snake.case (in this case `del = '.'; screaming = false`)
  50. func ToScreamingDelimited(s string, del uint8, screaming bool) string {
  51. s = addWordBoundariesToNumbers(s)
  52. s = strings.Trim(s, " ")
  53. n := ""
  54. for i, v := range s {
  55. // treat acronyms as words, eg for JSONData -> JSON is a whole word
  56. nextCaseIsChanged := false
  57. if i+1 < len(s) {
  58. next := s[i+1]
  59. if (v >= 'A' && v <= 'Z' && next >= 'a' && next <= 'z') || (v >= 'a' && v <= 'z' && next >= 'A' && next <= 'Z') {
  60. nextCaseIsChanged = true
  61. }
  62. }
  63. if i > 0 && n[len(n)-1] != del && nextCaseIsChanged {
  64. // add underscore if next letter case type is changed
  65. if v >= 'A' && v <= 'Z' {
  66. n += string(del) + string(v)
  67. } else if v >= 'a' && v <= 'z' {
  68. n += string(v) + string(del)
  69. }
  70. } else if v == ' ' || v == '_' || v == '-' {
  71. // replace spaces/underscores with delimiters
  72. n += string(del)
  73. } else {
  74. n = n + string(v)
  75. }
  76. }
  77. if screaming {
  78. n = strings.ToUpper(n)
  79. } else {
  80. n = strings.ToLower(n)
  81. }
  82. return n
  83. }