-
Notifications
You must be signed in to change notification settings - Fork 0
/
xcase.go
55 lines (43 loc) · 1.1 KB
/
xcase.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
package scs
import (
"regexp"
"strings"
)
var chunkLayout = regexp.MustCompile(`[^0-9a-zA-Z ]+`)
// The getChunks clears the string of special characters and splits the
// string by whitespace and returns a list of words ignoring empty elements.
func getChunks(s string) []string {
return strings.FieldsFunc(
chunkLayout.ReplaceAllString(strings.ToLower(s), " "),
func(c rune) bool { return c == ' ' },
)
}
// The toUnited converts a string to a format similar to camel or PascaCase.
func toUnited(s string, firstWordIsLower bool) string {
var result string
for i, chunk := range getChunks(s) {
// Chunk is always lowercase.
if i == 0 && firstWordIsLower {
result = chunk
continue
}
if v, ok := abbreviations[chunk]; ok {
result += v
continue
}
result += strings.Title(chunk)
}
return result
}
// The toSeparate converts a string to a format similar to snake or kebab-case.
func toSeparate(s, delimiter string) string {
var result string
for i, chunk := range getChunks(s) {
if i == 0 {
result = chunk
continue
}
result += delimiter + chunk
}
return result
}