-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcasing.go
143 lines (105 loc) · 2.82 KB
/
casing.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
package presilo
import (
"bytes"
"unicode"
)
/*
Converts the given target string to CamelCase; e.g.
"something" becomes "Something"
*/
func ToCamelCase(target string) string {
return iterateRunes(target, unicode.ToUpper)
}
/*
Converts the given target string to javaCase, e.g.
"SomethingElse" becomes "somethingElse"
*/
func ToJavaCase(target string) string {
return iterateRunes(target, unicode.ToLower)
}
/*
Same as ToCamelCase, except that any non-alphanumeric character is stripped from the returned value.
*/
func ToStrictCamelCase(target string) string {
return ToCamelCase(removeInvalidCharacters(target))
}
/*
Same as ToJavaCase, except that any non-alphanumeric character is stripped from the returned value.
*/
func ToStrictJavaCase(target string) string {
return ToJavaCase(removeInvalidCharacters(target))
}
/*
Converts the given target string to snake_case, e.g.
"somethingQuiteFine" becomes "something_quite_fine"
*/
func ToSnakeCase(target string) string {
var ret bytes.Buffer
var channel chan rune
var convertedCharacter string
var character rune
var previous bool
channel = make(chan rune)
go getCharacterChannel(target, channel)
character =<- channel
convertedCharacter = removeInvalidCharacters(string(unicode.ToLower(character)))
ret.Write([]byte(convertedCharacter))
previous = false
for character = range channel {
if unicode.IsUpper(character) {
if(!previous) {
ret.WriteRune('_')
previous = true
}
ret.WriteRune(unicode.ToLower(character))
} else {
ret.WriteRune(character)
previous = false
}
}
return ret.String()
}
/*
Same as "ToSnakeCase", except that any non-alphanumeric / underscore character is stripped from the returned value.
*/
func ToStrictSnakeCase(target string) string {
return ToSnakeCase(removeInvalidCharacters(target))
}
func iterateRunes(target string, transformer func(rune) rune) string {
var ret bytes.Buffer
var channel chan rune
var character rune
channel = make(chan rune)
go getCharacterChannel(target, channel)
character = <-channel
character = transformer(character)
ret.WriteString(string(character))
for character = range channel {
ret.WriteString(string(character))
}
return ret.String()
}
func removeInvalidCharacters(target string) string {
var ret bytes.Buffer
var channel chan rune
var previousInvalid bool
channel = make(chan rune)
previousInvalid = false
go getCharacterChannel(target, channel)
for character := range channel {
if previousInvalid {
character = unicode.ToUpper(character)
}
previousInvalid = !unicode.IsLetter(character) && !unicode.IsDigit(character)
if !previousInvalid {
ret.WriteRune(character)
}
}
return ret.String()
}
func getCharacterChannel(source string, channel chan rune) {
defer close(channel)
for _, rune := range source {
channel <- rune
}
}