Skip to content

Commit ba4cdf9

Browse files
authored
fixes issue where ToLowerCamel was outputting screaming replacements at index 0 (#30)
1 parent 01b4883 commit ba4cdf9

File tree

2 files changed

+24
-12
lines changed

2 files changed

+24
-12
lines changed

caps_test.go

+19
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,25 @@ var titleTestCases = testcases{
6464
{"test camel initialisms uuid id guid http https", "Test Camel Initialisms Uuid Id Guid Http Https", Opts{caps.WithReplaceStyleCamel()}},
6565
}
6666

67+
func TestIssue29(t *testing.T) {
68+
tests := testcases{
69+
{"ID", "id", nil},
70+
{"entity id", "entityID", nil},
71+
}
72+
73+
for _, test := range tests {
74+
func(test testcase) {
75+
t.Run(test.input, func(t *testing.T) {
76+
t.Parallel()
77+
output := caps.ToLowerCamel(test.input, test.opts...)
78+
if output != test.expected {
79+
t.Errorf("expected \"%s\", got \"%s\"", test.expected, output)
80+
}
81+
})
82+
}(test)
83+
}
84+
}
85+
6786
func TestToTitle(t *testing.T) {
6887
for _, test := range titleTestCases {
6988
func(test testcase) {

converter.go

+5-12
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,11 @@ func (StdConverter) writeIndexReplacement(b *strings.Builder, style Style, repSt
167167
b.WriteString(rep.Camel)
168168
}
169169
case ReplaceStyleScreaming:
170-
b.WriteString(rep.Screaming)
170+
if b.Len() == 0 && style == StyleLowerCamel {
171+
b.WriteString(rep.Lower)
172+
} else {
173+
b.WriteString(rep.Screaming)
174+
}
171175
case ReplaceStyleLower:
172176
b.WriteString(rep.Lower)
173177
default:
@@ -241,7 +245,6 @@ func (sc StdConverter) Convert(req ConvertRequest) string {
241245
if idx, ok = idx.Match(tok); !ok {
242246
if idx.LastMatch().HasValue() {
243247
// appending the last match
244-
// formatIndexedReplacement(req.Style, req.ReplaceStyle, b.Len(), idx.LastMatch()), req.Join
245248
sc.writeIndexReplacement(b, req.Style, req.ReplaceStyle, req.Join, idx.LastMatch())
246249
}
247250
if idx.HasPartialMatches() {
@@ -285,21 +288,11 @@ func (sc StdConverter) Convert(req ConvertRequest) string {
285288
}
286289
if idx.HasMatched() {
287290
sc.writeIndexReplacement(b, req.Style, req.ReplaceStyle, req.Join, idx.LastMatch())
288-
// parts = append(parts, formatIndexedReplacement(req.Style, req.ReplaceStyle, len(parts), idx.LastMatch()))
289291
}
290292

291293
if idx.HasPartialMatches() {
292294
sc.writeReplaceSplit(b, req.Style, req.Join, idx.PartialMatchRunes())
293295
}
294-
// for _, part := range parts {
295-
// if shouldWriteDelimiter {
296-
// result.WriteString(req.Join)
297-
// }
298-
// result.WriteString(part)
299-
// if !shouldWriteDelimiter {
300-
// shouldWriteDelimiter = len(part) > 0 && len(req.Join) > 0
301-
// }
302-
// }
303296

304297
return b.String()
305298
}

0 commit comments

Comments
 (0)