Skip to content

Commit 7e01051

Browse files
authored
fix: collapse consecutive dashes in NormalizeTag (#17)
"V. Gabriel" normalized to "v--gabriel" because the period converts to a dash (step 5) and the space also converts to a dash (step 4), leaving two consecutive dashes. Add a final step to collapse any run of dashes into a single dash so the output is always "v-gabriel".
1 parent bc87bb7 commit 7e01051

2 files changed

Lines changed: 13 additions & 3 deletions

File tree

tagfilter.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,16 @@ import (
2222
)
2323

2424
var (
25-
reColonSpacing = regexp.MustCompile(`\s*:\s*`)
26-
reSpecialChars = regexp.MustCompile(`[^a-z0-9:,+\-]`)
25+
reColonSpacing = regexp.MustCompile(`\s*:\s*`)
26+
reSpecialChars = regexp.MustCompile(`[^a-z0-9:,+\-]`)
27+
reConsecutiveDash = regexp.MustCompile(`-{2,}`)
2728
)
2829

2930
// NormalizeTag normalizes a tag string for consistent matching.
3031
// Applied to BOTH type and value parts separately.
3132
// Rules: trim whitespace, normalize colon spacing, lowercase, spaces→dashes,
32-
// periods→dashes, and remove special chars (except colon, dash, and comma).
33+
// periods→dashes, remove special chars (except colon, dash, and comma),
34+
// and collapse consecutive dashes into one.
3335
func NormalizeTag(s string) string {
3436
// 1. Trim whitespace
3537
s = strings.TrimSpace(s)
@@ -50,6 +52,9 @@ func NormalizeTag(s string) string {
5052
// Keep: a-z, 0-9, dash, colon, comma
5153
s = reSpecialChars.ReplaceAllString(s, "")
5254

55+
// 7. Collapse consecutive dashes into one (e.g. "V. Gabriel" → "v--gabriel" → "v-gabriel")
56+
s = reConsecutiveDash.ReplaceAllString(s, "-")
57+
5358
return s
5459
}
5560

tagfilter_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,11 @@ func TestNormalizeTag(t *testing.T) {
6464
input: "my-tag:value,other",
6565
want: "my-tag:value,other",
6666
},
67+
{
68+
name: "consecutive dashes collapsed",
69+
input: "V. Gabriel",
70+
want: "v-gabriel",
71+
},
6772
}
6873

6974
for _, tt := range tests {

0 commit comments

Comments
 (0)