File tree 2 files changed +33
-5
lines changed
2 files changed +33
-5
lines changed Original file line number Diff line number Diff line change @@ -11,14 +11,17 @@ import (
11
11
)
12
12
13
13
var (
14
- re = regexp .MustCompile (`([A-Z]+)` )
14
+ uncamel = regexp .MustCompile (`([A-Z]+)` )
15
+ clean = regexp .MustCompile (`[^\w]` )
15
16
)
16
17
17
- // CamelRegexp return the given string as space delimeted. Note! it's slow. Use
18
+ // DecamelRegexp return the given string as space delimeted. Note! it's slow. Use
18
19
// Decamel instead.
19
- func CamelRegexp (str string ) string {
20
- str = re .ReplaceAllString (str , ` $1` )
20
+ func DecamelRegexp (str string ) string {
21
+ str = clean .ReplaceAllString (str , " " )
22
+ str = uncamel .ReplaceAllString (str , ` $1` )
21
23
str = strings .Trim (str , " " )
24
+ str = strings .ToLower (str )
22
25
return str
23
26
}
24
27
Original file line number Diff line number Diff line change @@ -13,7 +13,7 @@ const (
13
13
14
14
func BenchmarkCamelRegexp (b * testing.B ) {
15
15
for n := 0 ; n < b .N ; n ++ {
16
- _ = str .CamelRegexp (camelStr )
16
+ _ = str .DecamelRegexp (camelStr )
17
17
}
18
18
}
19
19
@@ -23,6 +23,31 @@ func BenchmarkDecamel(b *testing.B) {
23
23
}
24
24
}
25
25
26
+ func TestCamel (t * testing.T ) {
27
+ t .Parallel ()
28
+ type args struct {
29
+ s string
30
+ }
31
+ tests := []struct {
32
+ name string
33
+ args args
34
+ want string
35
+ }{
36
+ {"simple" , args {"CamelString" }, "camel string" },
37
+ {"number" , args {"CamelString2Testing" }, "camel string2 testing" },
38
+ {"acronym" , args {"ARMCamelString" }, "armcamel string" },
39
+ {"acronym at end" , args {"archIsARM" }, "arch is arm" },
40
+ }
41
+ for _ , ttv := range tests {
42
+ tt := ttv
43
+ t .Run (tt .name , func (t * testing.T ) {
44
+ t .Parallel ()
45
+ got := str .DecamelRegexp (tt .args .s )
46
+ test .RequireEqual (t , got , tt .want )
47
+ })
48
+ }
49
+ }
50
+
26
51
func TestDecamel (t * testing.T ) {
27
52
t .Parallel ()
28
53
type args struct {
You can’t perform that action at this time.
0 commit comments