Skip to content

Commit d838154

Browse files
committed
Add func FilterRange
1 parent 0ad3399 commit d838154

File tree

3 files changed

+17
-1
lines changed

3 files changed

+17
-1
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ go get github.com/tiendc/gofn
5757
- [FilterNE](#filter)
5858
- [FilterLT / FilterLTE](#filter)
5959
- [FilterGT / FilterGTE](#filter)
60+
- [FilterRange](#filter)
6061
- [FilterIN / FilterNIN](#filter)
6162
- [FilterLIKE / FilterILIKE](#filter)
6263

@@ -500,13 +501,14 @@ FilterLT([]int{1, 2, 3, 4}, 3) // []int{1, 2}
500501
FilterLTE([]int{1, 2, 3, 4}, 3) // []int{1, 2, 3}
501502
FilterGT([]int{1, 2, 3, 4}, 3) // []int{4}
502503
FilterGTE([]int{1, 2, 3, 4}, 3) // []int{3, 4}
504+
FilterRange([]int{1, 2, -2, 4}, 0, 3) // []int{1, 2}
503505
FilterNE([]int{1, 2, 3, 4}, 3) // []int{1, 2, 4}
504506
FilterIN([]int{1, 2, 3, 4}, 3, 2, 7) // []int{2, 3}
505507
FilterNIN([]int{1, 2, 3, 4}, 3, 2, 7) // []int{1, 4}
506508
FilterLIKE([]string{"*Abc*", "*abc*", "abc*", "*abc"}, "Abc") // []string{"*Abc*"}
507509
FilterILIKE([]string{"*Abc*", "*abc*", "abc*", "*abc"}, "Abc") // []string{"*Abc*", "*abc*", "abc*", "*abc"}
508510
```
509-
511+
510512
### Slice iteration
511513
---
512514

slice_filter.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,11 @@ func FilterGTE[T NumberExt | StringExt, S ~[]T](s S, v T) S {
4848
return Filter(s, func(t T) bool { return t >= v })
4949
}
5050

51+
// FilterRange returns all values which are in the specified range (lower and upper bound inclusive)
52+
func FilterRange[T NumberExt | StringExt, S ~[]T](s S, min, max T) S {
53+
return Filter(s, func(t T) bool { return t >= min && t <= max })
54+
}
55+
5156
// FilterNE returns all values which are not equal to the specified value
5257
func FilterNE[T comparable, S ~[]T](s S, v T) S {
5358
return Filter(s, func(t T) bool { return t != v })

slice_filter_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,15 @@ func Test_FilterGTE(t *testing.T) {
6868
assert.Equal(t, []string{"cd", "a"}, FilterGTE([]string{"Ab", "cd", "a"}, "a"))
6969
}
7070

71+
func Test_FilterRange(t *testing.T) {
72+
// Nil/Empty slices
73+
assert.Equal(t, []int{}, FilterRange[int]([]int(nil), 0, 0))
74+
assert.Equal(t, []int{}, FilterRange([]int{}, 0, 0))
75+
76+
assert.Equal(t, []int{1, 2}, FilterRange([]int{0, -10, 1, 2, -3, 0, 7}, 1, 5))
77+
assert.Equal(t, []string{"a", "b", "c"}, FilterRange([]string{"Ab", "cd", "a", "b", "", "c"}, "a", "c"))
78+
}
79+
7180
func Test_FilterNE(t *testing.T) {
7281
// Nil/Empty slices
7382
assert.Equal(t, []int{}, FilterNE[int]([]int(nil), 0))

0 commit comments

Comments
 (0)