Skip to content

Commit 04d0661

Browse files
committed
fix toCamelCase function
1 parent 83a0a99 commit 04d0661

File tree

3 files changed

+64
-11
lines changed

3 files changed

+64
-11
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,5 @@
1212

1313
# Project-local glide cache, RE: https://github.com/Masterminds/glide/issues/736
1414
.glide/
15+
16+
.idea/

core.go

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ var (
1717
// ToCamelCase converts string to CamelCase. All space symbols, `_`, `.` and `-` will be removed. First symbol will be capital.
1818
ToCamelCase = toCamelCase(true, unicode.ToUpper)
1919
// ToCamelCase converts string to CamelCase. All space symbols, `_`, `.` and `-` will be removed. First symbol will be capital.
20-
ToCamelCaseLowerFirst = toCamelCase(false, unicode.ToUpper)
20+
ToCamelCaseLowerFirst = toCamelCaseLower()
2121
// ToNoCase returns input's string. Should be used for mocking, tests, etc.
2222
ToNoCase = func(s string) string { return s }
2323
)
@@ -66,6 +66,43 @@ func toCamelCase(upperFirst bool, runeConv func(rune) rune) func(string) string
6666
}
6767
}
6868

69+
func toCamelCaseLower() func(string) string {
70+
return func(s string) string {
71+
in := []rune(s)
72+
var runes []rune
73+
max, m := len(in), 0
74+
for i, r := range in {
75+
if !isExtendedSpace(r) && (i+1 < max && unicode.IsUpper(in[i+1])) {
76+
runes = append(runes, unicode.ToLower(r))
77+
continue
78+
}
79+
m = i
80+
break
81+
}
82+
if len(in[m:]) == 1 {
83+
return string(append(runes, unicode.ToLower(in[m])))
84+
}
85+
for i, r := range in[m:] {
86+
if isExtendedSpace(r) {
87+
continue
88+
}
89+
if unicode.IsUpper(r) {
90+
if i+m > 0 {
91+
runes = append(runes, r)
92+
} else {
93+
runes = append(runes, unicode.ToLower(r))
94+
}
95+
continue
96+
}
97+
if i+m > 0 && isExtendedSpace(in[i+m-1]) && unicode.IsLower(r) {
98+
r = unicode.ToUpper(r)
99+
}
100+
runes = append(runes, r)
101+
}
102+
return string(runes)
103+
}
104+
}
105+
69106
func isExtendedSpace(r rune) bool {
70107
return unicode.IsSpace(r) || r == '_' || r == '-' || r == '.'
71108
}

core_test.go

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ var testCases = []string{
2121
"ТестКириллицы",
2222
"Ещё-Тест Кириллицы",
2323
"и-ещё-тест.Кириллицы",
24+
"MQ",
25+
"MTT",
2426
}
2527

2628
func Test_ToSnakeCase(t *testing.T) {
@@ -40,6 +42,8 @@ func Test_ToSnakeCase(t *testing.T) {
4042
"тест_кириллицы",
4143
"ещё_тест_кириллицы",
4244
"и_ещё_тест_кириллицы",
45+
"mq",
46+
"mtt",
4347
}
4448
if len(testCases) != len(answers) {
4549
t.Fatal("different amount of test cases and expected answers")
@@ -70,6 +74,8 @@ func Test_ToUrlSnakeCase(t *testing.T) {
7074
"тест-кириллицы",
7175
"ещё-тест-кириллицы",
7276
"и-ещё-тест-кириллицы",
77+
"mq",
78+
"mtt",
7379
}
7480
if len(testCases) != len(answers) {
7581
t.Fatal("different amount of test cases and expected answers")
@@ -100,6 +106,8 @@ func Test_ToDotSnakeCase(t *testing.T) {
100106
"тест.кириллицы",
101107
"ещё.тест.кириллицы",
102108
"и.ещё.тест.кириллицы",
109+
"mq",
110+
"mtt",
103111
}
104112
if len(testCases) != len(answers) {
105113
t.Fatal("different amount of test cases and expected answers")
@@ -130,6 +138,8 @@ func Test_ToCamelCase(t *testing.T) {
130138
"ТестКириллицы",
131139
"ЕщёТестКириллицы",
132140
"ИЕщёТестКириллицы",
141+
"MQ",
142+
"MTT",
133143
}
134144
if len(testCases) != len(answers) {
135145
t.Fatal("different amount of test cases and expected answers")
@@ -146,20 +156,22 @@ func Test_ToCamelCase(t *testing.T) {
146156
func Test_ToCamelCaseLowerFirst(t *testing.T) {
147157
answers := []string{
148158
"stringService",
149-
"StringService",
150-
"Stringservice",
151159
"stringService",
152-
"StringService",
160+
"stringservice",
161+
"stringService",
162+
"stringService",
153163
"stringService",
154-
"JSONService",
155164
"jsonService",
156-
"JSONServiceV2",
157-
"JSONServiceV2",
158-
"StringService",
159-
"StringService",
160-
"ТестКириллицы",
161-
"ЕщёТестКириллицы",
165+
"jsonService",
166+
"jsonServiceV2",
167+
"jsonServiceV2",
168+
"stringService",
169+
"stringService",
170+
"тестКириллицы",
171+
"ещёТестКириллицы",
162172
"иЕщёТестКириллицы",
173+
"mq",
174+
"mtt",
163175
}
164176
if len(testCases) != len(answers) {
165177
t.Fatal("different amount of test cases and expected answers")
@@ -190,6 +202,8 @@ func Test_ToNoCase(t *testing.T) {
190202
"ТестКириллицы",
191203
"Ещё-Тест Кириллицы",
192204
"и-ещё-тест.Кириллицы",
205+
"MQ",
206+
"MTT",
193207
}
194208
if len(testCases) != len(answers) {
195209
t.Fatal("different amount of test cases and expected answers")

0 commit comments

Comments
 (0)