Skip to content

Commit 6ba89ec

Browse files
committed
chore: make MatchItem case insensitive
1 parent 6b5bea6 commit 6ba89ec

4 files changed

Lines changed: 66 additions & 11 deletions

File tree

collections/slice.go

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -209,24 +209,31 @@ func MatchItems(item string, patterns ...string) bool {
209209
}
210210

211211
func matchPattern(item, pattern string) bool {
212-
if pattern == "*" || item == pattern {
212+
if pattern == "*" {
213213
return true
214214
}
215215

216-
if strings.HasPrefix(pattern, "*") && strings.HasSuffix(pattern, "*") {
217-
if strings.Contains(item, strings.TrimPrefix(strings.TrimSuffix(pattern, "*"), "*")) {
216+
itemLower := strings.ToLower(item)
217+
patternLower := strings.ToLower(pattern)
218+
219+
if itemLower == patternLower {
220+
return true
221+
}
222+
223+
if strings.HasPrefix(patternLower, "*") && strings.HasSuffix(patternLower, "*") {
224+
if strings.Contains(itemLower, strings.TrimPrefix(strings.TrimSuffix(patternLower, "*"), "*")) {
218225
return true
219226
}
220227
}
221228

222-
if strings.HasPrefix(pattern, "*") {
223-
if strings.HasSuffix(item, strings.TrimPrefix(pattern, "*")) {
229+
if strings.HasPrefix(patternLower, "*") {
230+
if strings.HasSuffix(itemLower, strings.TrimPrefix(patternLower, "*")) {
224231
return true
225232
}
226233
}
227234

228-
if strings.HasSuffix(pattern, "*") {
229-
if strings.HasPrefix(item, strings.TrimSuffix(pattern, "*")) {
235+
if strings.HasSuffix(patternLower, "*") {
236+
if strings.HasPrefix(itemLower, strings.TrimSuffix(patternLower, "*")) {
230237
return true
231238
}
232239
}

collections/slice_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,12 @@ var _ = Describe("MatchItems", func() {
3535
Entry("URL encoded pattern matches", "hello ", []string{"hello%20"}, true),
3636
Entry("URL encoded pattern does not match", "hello", []string{"hello%20"}, false),
3737
Entry("malformed URL encoding", "apple", []string{"%zzapple"}, false),
38+
Entry("case insensitive exact match", "Apple", []string{"apple"}, true),
39+
Entry("case insensitive exact match reverse", "apple", []string{"Apple"}, true),
40+
Entry("case insensitive prefix wildcard", "Apple", []string{"appl*"}, true),
41+
Entry("case insensitive suffix wildcard", "Apple", []string{"*PLE"}, true),
42+
Entry("case insensitive glob", "Apple", []string{"*PPL*"}, true),
43+
Entry("case insensitive exclusion", "Apple", []string{"!apple"}, false),
3844
)
3945
})
4046

duration/duration.go

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,8 @@ func (d Duration) String() string {
8383
d = d - d%Duration(time.Minute)
8484
} else if d.Minutes() > 2 {
8585
d = d - d%Duration(time.Second)
86-
} else if d.Seconds() > 2 {
87-
d = d - d%Duration(time.Millisecond)
8886
} else if d.Nanoseconds() > 2*1000*1000 {
89-
d = d - d%Duration(time.Microsecond)
87+
d = d - d%Duration(time.Millisecond)
9088
}
9189
// Largest time is 2540400h10m10.000000000s
9290
var buf [32]byte
@@ -213,7 +211,22 @@ func (d Duration) String() string {
213211
buf[w] = '-'
214212
}
215213

216-
return strings.ReplaceAll(strings.ReplaceAll(string(buf[w:]), "0s", ""), "0m", "")
214+
result := string(buf[w:])
215+
// Strip standalone zero-valued units "0m" (minutes) and "0s" (seconds).
216+
// Must not match inside sub-second units like "320ms", "500µs", "100ns".
217+
218+
// Strip "0m" only if NOT followed by "s" (which would make it "0ms" = milliseconds)
219+
if i := strings.Index(result, "0m"); i >= 0 {
220+
if i+2 >= len(result) || result[i+2] != 's' {
221+
result = result[:i] + result[i+2:]
222+
}
223+
}
224+
// Strip "0s" only at the very end, and only if the result contains
225+
// larger units (meaning "0s" is a trailing zero seconds, not "0s" standalone)
226+
if strings.HasSuffix(result, "0s") && len(result) > 2 {
227+
result = result[:len(result)-2]
228+
}
229+
return result
217230
}
218231

219232
// fmtFrac formats the fraction of v/10**prec (e.g., ".12345") into the

duration/manual_test.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package duration
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
"time"
7+
)
8+
9+
func TestMillisecondFormatting(t *testing.T) {
10+
tests := []struct {
11+
d time.Duration
12+
expected string
13+
}{
14+
{320 * time.Millisecond, "320ms"},
15+
{150 * time.Millisecond, "150ms"},
16+
{5250 * time.Millisecond, "5.25s"},
17+
{1250 * time.Millisecond, "1.25s"},
18+
{999 * time.Millisecond, "999ms"},
19+
{1001 * time.Millisecond, "1.001s"},
20+
{50 * time.Millisecond, "50ms"},
21+
}
22+
for _, tc := range tests {
23+
got := Duration(tc.d).String()
24+
if got != tc.expected {
25+
t.Errorf("%v: got %q, want %q", tc.d, got, tc.expected)
26+
}
27+
fmt.Printf("%v -> %s (want %s)\n", tc.d, got, tc.expected)
28+
}
29+
}

0 commit comments

Comments
 (0)