-
Notifications
You must be signed in to change notification settings - Fork 0
/
snake.go
180 lines (166 loc) · 5.56 KB
/
snake.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package scs
import (
"fmt"
"regexp"
"strings"
)
var (
snakeBody = regexp.MustCompile(`_([a-zA-Z0-9])`)
isSnakeCase = regexp.MustCompile("(^[a-z0-9_]+_[a-z0-9_]+$)|(^[a-z0-9]+$)")
)
// StrIsSnake returns true if the string is in snake_case.
//
// Snake case represents words separated by underscores and does
// not have any capital letters.
//
// Example usage:
//
// scs.StrIsSnake("hello_world") // returns true
// scs.StrIsSnake("HelloWorld") // returns false
// scs.StrIsSnake("hello-world") // returns false
func StrIsSnake(s string) bool {
return isSnakeCase.Match([]byte(s))
}
// StrToSnake converts a string to snake_case.
//
// This function first transforms the input string by replacing all
// non-alphanumeric characters with spaces.
//
// It then splits the string into chunks at the spaces, converts
// each chunk to lower case, and finally joins the chunks back together
// with underscores.
//
// Note that this function will convert upper case letters to lower case,
// so the output string will be all lower case even if the input string
// contains upper case letters.
//
// Example usage:
//
// scs.StrToSnake("Hello World") // returns "hello_world"
// scs.StrToSnake("hello world") // returns "hello_world"
// scs.StrToSnake("hello-world") // returns "hello_world"
func StrToSnake(s string) string {
return toSeparate(s, "_")
}
// ToSnake converts a string to snake_case.
//
// Unlike the StrToSnake function, this function attempts to correctly
// handle strings that are already in a certain format (such as CamelCase,
// KebabCase, or PascalCase) and convert them into snake_case.
//
// The function first checks if the string is in CamelCase, KebabCase,
// PascalCase, or SnakeCase format. If it is, it uses the corresponding
// conversion function to convert the string into snake_case.
//
// If the string is not in any recognized format, it defaults to using the
// StrToSnake function to attempt a conversion.
//
// Example usage:
//
// scs.ToSnake("helloWorld") // returns "hello_world"
// scs.ToSnake("HelloWorld") // returns "hello_world"
// scs.ToSnake("hello-world") // returns "hello_world"
// scs.ToSnake("hello_world") // returns "hello_world"
// scs.ToSnake("Hello World") // returns "hello_world"
func ToSnake(s string) string {
switch {
case StrIsCamel(s):
r, _ := CamelToSnake(s)
return r
case StrIsKebab(s):
r, _ := KebabToSnake(s)
return r
case StrIsPascal(s):
r, _ := PascalToSnake(s)
return r
case StrIsSnake(s):
return s
}
return StrToSnake(s)
}
// SnakeToCamel converts a snake_case-style string to camelCase.
//
// This function checks if the input string is in snake_case.
// If not, it returns an error.
//
// If the input string is in snake_case, it replaces all underscores
// with spaces and converts the resulting string to camelCase.
//
// Note that the first word in the output string will be in lower case,
// and the first letter of each subsequent word will be in upper case.
// All other letters will be lower case.
//
// This conversion could fail if the input string is not in snake_case style.
// In that case, an error will be returned along with an empty string.
//
// Example usage:
//
// scs.SnakeToCamel("hello_world") // returns "helloWorld", nil
// scs.SnakeToCamel("HelloWorld") // returns "", error
// scs.SnakeToCamel("hello-world") // returns "", error
func SnakeToCamel(snake string) (string, error) {
if !StrIsSnake(snake) {
return "", fmt.Errorf("value %s isn't snake_case style", snake)
}
result := StrToCamel(
snakeBody.ReplaceAllStringFunc(
snake,
func(s string) string { return strings.Replace(s, "_", " ", -1) },
),
)
return result, nil
}
// SnakeToKebab converts a snake_case-style string to kebab-case.
//
// This function checks if the input string is in snake_case. If it's not,
// it returns an error.
//
// If the input string is in snake_case, it replaces all underscores with
// hyphens, effectively converting the string from snake_case to kebab-case.
//
// Note that this conversion could fail if the input string is not in
// snake_case style. In that case, an error will be returned along with
// an empty string.
//
// Example usage:
//
// scs.SnakeToKebab("hello_world") // returns "hello-world", nil
// scs.SnakeToKebab("HelloWorld") // returns "", error
// scs.SnakeToKebab("helloWorld") // returns "", error
func SnakeToKebab(snake string) (string, error) {
if !StrIsSnake(snake) {
return "", fmt.Errorf("value %s isn't snake_case style", snake)
}
return strings.ReplaceAll(snake, "_", "-"), nil
}
// SnakeToPascal converts a snake_case-style string to PascalCase.
//
// This function checks if the input string is in snake_case. If it's not,
// it returns an error.
//
// If the input string is in snake_case, it replaces all underscores with
// spaces and converts the resulting string to PascalCase. The PascalCase
// format starts with an uppercase letter, and includes an uppercase letter
// at the beginning of each new word.
//
// Note that this conversion could fail if the input string is not in
// snake_case style. In that case, an error will be returned along with
// an empty string.
//
// Example usage:
//
// scs.SnakeToPascal("hello_world") // returns "HelloWorld", nil
// scs.SnakeToPascal("HelloWorld") // returns "", error
// scs.SnakeToPascal("hello-world") // returns "", error
func SnakeToPascal(snake string) (string, error) {
if !StrIsSnake(snake) {
return "", fmt.Errorf("value %s isn't snake_case style", snake)
}
result := StrToPascal(
snakeBody.ReplaceAllStringFunc(
snake,
func(s string) string { return strings.Replace(s, "_", " ", -1) },
),
)
return result, nil
}