forked from gobuffalo/flect
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcamelize.go
48 lines (45 loc) · 915 Bytes
/
camelize.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
package flect
import (
"strings"
"unicode"
)
// Camelize returns a camelize version of a string
// bob dylan = bobDylan
// widget_id = widgetID
// WidgetID = widgetID
func Camelize(s string) string {
return New(s).Camelize().String()
}
// Camelize returns a camelize version of a string
// bob dylan = bobDylan
// widget_id = widgetID
// WidgetID = widgetID
func (i Ident) Camelize() Ident {
var out []string
for i, part := range i.Parts {
var x string
var capped bool
if strings.ToLower(part) == "id" {
out = append(out, "ID")
continue
}
for _, c := range part {
if unicode.IsLetter(c) || unicode.IsDigit(c) {
if i == 0 {
x += string(unicode.ToLower(c))
continue
}
if !capped {
capped = true
x += string(unicode.ToUpper(c))
continue
}
x += string(c)
}
}
if x != "" {
out = append(out, x)
}
}
return New(strings.Join(out, ""))
}