Skip to content

Commit 3d1047d

Browse files
Fix UpperIndicator delimiter behavior consistency
Refactor `WordsToFormattedCase` to calculate the effective delimiter early, ensuring `OptionUpperIndicator` consistently overrides or modifies the delimiter for both mixed-case splitting and final string joining. Updated `edge_cases_test.go` to remove outdated comments and verify consistency. Co-authored-by: arran4 <111667+arran4@users.noreply.github.com>
1 parent d6c51f4 commit 3d1047d

2 files changed

Lines changed: 29 additions & 10 deletions

File tree

edge_cases_test.go

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,13 +86,32 @@ func TestEdgeCases(t *testing.T) {
8686
}
8787

8888
// Case B: Indicator != Delimiter
89-
// With proposed fix, this should use the indicator as delimiter
89+
// This should use the indicator as delimiter
9090
res = ToFormattedCase(input, OptionDelimiter("-"), OptionUpperIndicator("="))
9191
if res != "hello=world" {
9292
t.Errorf("UpperIndicator Override: got %q, want %q", res, "hello=world")
9393
}
9494
})
9595

96+
// 5.1 UpperIndicator with MixCaseSupport (Consistency Check)
97+
t.Run("UpperIndicator MixCase Consistency", func(t *testing.T) {
98+
input := []Word{ExactCaseWord("helloWorld"), SingleCaseWord("foo")}
99+
100+
// Case A: Override behavior
101+
// Expectation: hello=World=foo (UpperIndicator "=" overrides Delimiter "-")
102+
res := ToFormattedCase(input, OptionDelimiter("-"), OptionUpperIndicator("="), OptionMixCaseSupport())
103+
if res != "hello=World=foo" {
104+
t.Errorf("UpperIndicator MixCase Override: got %q, want %q", res, "hello=World=foo")
105+
}
106+
107+
// Case B: Double Delimiter behavior
108+
// Expectation: hello--World--foo (UpperIndicator "-" matches Delimiter "-", so double delimiter)
109+
res = ToFormattedCase(input, OptionDelimiter("-"), OptionUpperIndicator("-"), OptionMixCaseSupport())
110+
if res != "hello--World--foo" {
111+
t.Errorf("UpperIndicator MixCase Double: got %q, want %q", res, "hello--World--foo")
112+
}
113+
})
114+
96115
// 6. Empty and Nil Input
97116
t.Run("Empty and Nil Input", func(t *testing.T) {
98117
res := ToFormattedCase(nil)

types.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,14 @@ func WordsToFormattedCase(words []Word, opts ...any) (string, error) {
205205
}
206206
}
207207

208+
if cfg.upperIndicator != "" {
209+
if cfg.upperIndicator == cfg.delimiter {
210+
cfg.delimiter = cfg.delimiter + cfg.delimiter
211+
} else {
212+
cfg.delimiter = cfg.upperIndicator
213+
}
214+
}
215+
208216
switch cfg.caseMode {
209217
case CMScreaming:
210218
cfg.screaming = true
@@ -280,15 +288,7 @@ func WordsToFormattedCase(words []Word, opts ...any) (string, error) {
280288
result = append(result, w)
281289
}
282290

283-
delimiter := cfg.delimiter
284-
if cfg.upperIndicator != "" {
285-
if cfg.upperIndicator == cfg.delimiter {
286-
delimiter = cfg.delimiter + cfg.delimiter
287-
} else {
288-
delimiter = cfg.upperIndicator
289-
}
290-
}
291-
final := strings.Join(result, delimiter)
291+
final := strings.Join(result, cfg.delimiter)
292292

293293
if cfg.firstUpper {
294294
final = UpperCaseFirst(final)

0 commit comments

Comments
 (0)