Add Filter function to garray (#2541)

This commit is contained in:
wsy998
2023-04-12 10:15:11 +08:00
committed by GitHub
parent d788b7ff5e
commit c1325ef9a3
16 changed files with 318 additions and 0 deletions

View File

@ -795,6 +795,24 @@ func (a *Array) UnmarshalValue(value interface{}) error {
return nil
}
// Filter `filter func(value interface{}, index int) bool` filter array, value
// means the value of the current element, the index of the current original
// color of value, when the custom function returns True, the element will be
// filtered, otherwise it will not be filtered, `Filter` function returns a new
// array, will not modify the original array.
func (a *Array) Filter(filter func(index int, value interface{}) bool) *Array {
a.mu.Lock()
defer a.mu.Unlock()
for i := 0; i < len(a.array); {
if filter(i, a.array[i]) {
a.array = append(a.array[:i], a.array[i+1:]...)
} else {
i++
}
}
return a
}
// FilterNil removes all nil value of the array.
func (a *Array) FilterNil() *Array {
a.mu.Lock()

View File

@ -788,6 +788,24 @@ func (a *IntArray) UnmarshalValue(value interface{}) error {
return nil
}
// Filter `filter func(value int, index int) bool` filter array, value
// means the value of the current element, the index of the current original
// color of value, when the custom function returns True, the element will be
// filtered, otherwise it will not be filtered, `Filter` function returns a new
// array, will not modify the original array.
func (a *IntArray) Filter(filter func(index int, value int) bool) *IntArray {
a.mu.Lock()
defer a.mu.Unlock()
for i := 0; i < len(a.array); {
if filter(i, a.array[i]) {
a.array = append(a.array[:i], a.array[i+1:]...)
} else {
i++
}
}
return a
}
// FilterEmpty removes all zero value of the array.
func (a *IntArray) FilterEmpty() *IntArray {
a.mu.Lock()

View File

@ -799,6 +799,24 @@ func (a *StrArray) UnmarshalValue(value interface{}) error {
return nil
}
// Filter `filter func(value string, index int) bool` filter array, value
// means the value of the current element, the index of the current original
// color of value, when the custom function returns True, the element will be
// filtered, otherwise it will not be filtered, `Filter` function returns a new
// array, will not modify the original array.
func (a *StrArray) Filter(filter func(index int, value string) bool) *StrArray {
a.mu.Lock()
defer a.mu.Unlock()
for i := 0; i < len(a.array); {
if filter(i, a.array[i]) {
a.array = append(a.array[:i], a.array[i+1:]...)
} else {
i++
}
}
return a
}
// FilterEmpty removes all empty string value of the array.
func (a *StrArray) FilterEmpty() *StrArray {
a.mu.Lock()

View File

@ -761,6 +761,24 @@ func (a *SortedArray) FilterNil() *SortedArray {
return a
}
// Filter `filter func(value interface{}, index int) bool` filter array, value
// means the value of the current element, the index of the current original
// color of value, when the custom function returns True, the element will be
// filtered, otherwise it will not be filtered, `Filter` function returns a new
// array, will not modify the original array.
func (a *SortedArray) Filter(filter func(index int, value interface{}) bool) *SortedArray {
a.mu.Lock()
defer a.mu.Unlock()
for i := 0; i < len(a.array); {
if filter(i, a.array[i]) {
a.array = append(a.array[:i], a.array[i+1:]...)
} else {
i++
}
}
return a
}
// FilterEmpty removes all empty value of the array.
// Values like: 0, nil, false, "", len(slice/map/chan) == 0 are considered empty.
func (a *SortedArray) FilterEmpty() *SortedArray {

View File

@ -709,6 +709,24 @@ func (a *SortedIntArray) UnmarshalValue(value interface{}) (err error) {
return err
}
// Filter `filter func(value int, index int) bool` filter array, value
// means the value of the current element, the index of the current original
// color of value, when the custom function returns True, the element will be
// filtered, otherwise it will not be filtered, `Filter` function returns a new
// array, will not modify the original array.
func (a *SortedIntArray) Filter(filter func(index int, value int) bool) *SortedIntArray {
a.mu.Lock()
defer a.mu.Unlock()
for i := 0; i < len(a.array); {
if filter(i, a.array[i]) {
a.array = append(a.array[:i], a.array[i+1:]...)
} else {
i++
}
}
return a
}
// FilterEmpty removes all zero value of the array.
func (a *SortedIntArray) FilterEmpty() *SortedIntArray {
a.mu.Lock()

View File

@ -722,6 +722,24 @@ func (a *SortedStrArray) UnmarshalValue(value interface{}) (err error) {
return err
}
// Filter `filter func(value string, index int) bool` filter array, value
// means the value of the current element, the index of the current original
// color of value, when the custom function returns True, the element will be
// filtered, otherwise it will not be filtered, `Filter` function returns a new
// array, will not modify the original array.
func (a *SortedStrArray) Filter(filter func(index int, value string) bool) *SortedStrArray {
a.mu.Lock()
defer a.mu.Unlock()
for i := 0; i < len(a.array); {
if filter(i, a.array[i]) {
a.array = append(a.array[:i], a.array[i+1:]...)
} else {
i++
}
}
return a
}
// FilterEmpty removes all empty string value of the array.
func (a *SortedStrArray) FilterEmpty() *SortedStrArray {
a.mu.Lock()

View File

@ -9,6 +9,8 @@ package garray_test
import (
"fmt"
"github.com/gogf/gf/v2/internal/empty"
"github.com/gogf/gf/v2/container/garray"
"github.com/gogf/gf/v2/frame/g"
)
@ -261,6 +263,21 @@ func ExampleArray_Merge() {
// [1 2 1 2 3 4 5 6 7 8 9 0]
}
func ExampleArray_Filter() {
array1 := garray.NewFrom(g.Slice{0, 1, 2, nil, "", g.Slice{}, "john"})
array2 := garray.NewFrom(g.Slice{0, 1, 2, nil, "", g.Slice{}, "john"})
fmt.Printf("%#v\n", array1.Filter(func(index int, value interface{}) bool {
return empty.IsNil(value)
}).Slice())
fmt.Printf("%#v\n", array2.Filter(func(index int, value interface{}) bool {
return empty.IsEmpty(value)
}).Slice())
// Output:
// []interface {}{0, 1, 2, "", []interface {}{}, "john"}
// []interface {}{1, 2, "john"}
}
func ExampleArray_FilterEmpty() {
array1 := garray.NewFrom(g.Slice{0, 1, 2, nil, "", g.Slice{}, "john"})
array2 := garray.NewFrom(g.Slice{0, 1, 2, nil, "", g.Slice{}, "john"})

View File

@ -9,6 +9,8 @@ package garray_test
import (
"fmt"
"github.com/gogf/gf/v2/internal/empty"
"github.com/gogf/gf/v2/container/garray"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/internal/json"
@ -699,6 +701,25 @@ func ExampleIntArray_UnmarshalValue() {
// &{john [96,98,97]}
}
func ExampleIntArray_Filter() {
array1 := garray.NewIntArrayFrom(g.SliceInt{10, 40, 50, 0, 0, 0, 60})
array2 := garray.NewIntArrayFrom(g.SliceInt{10, 4, 51, 5, 45, 50, 56})
fmt.Println(array1.Filter(func(index int, value int) bool {
return empty.IsEmpty(value)
}))
fmt.Println(array2.Filter(func(index int, value int) bool {
return value%2 == 0
}))
fmt.Println(array2.Filter(func(index int, value int) bool {
return value%2 == 1
}))
// Output:
// [10,40,50,60]
// [51,5,45]
// []
}
func ExampleIntArray_FilterEmpty() {
s := garray.NewIntArrayFrom(g.SliceInt{10, 40, 50, 0, 0, 0, 60})
fmt.Println(s)

View File

@ -8,6 +8,9 @@ package garray_test
import (
"fmt"
"strings"
"github.com/gogf/gf/v2/internal/empty"
"github.com/gogf/gf/v2/container/garray"
"github.com/gogf/gf/v2/frame/g"
@ -622,6 +625,22 @@ func ExampleStrArray_UnmarshalValue() {
// &{john ["Math","English","Sport"]}
}
func ExampleStrArray_Filter() {
s := garray.NewStrArrayFrom(g.SliceStr{"Math", "English", "Sport"})
s1 := garray.NewStrArrayFrom(g.SliceStr{"a", "b", "", "c", "", "", "d"})
fmt.Println(s1.Filter(func(index int, value string) bool {
return empty.IsEmpty(value)
}))
fmt.Println(s.Filter(func(index int, value string) bool {
return strings.Contains(value, "h")
}))
// Output:
// ["a","b","c","d"]
// ["Sport"]
}
func ExampleStrArray_FilterEmpty() {
s := garray.NewStrArrayFrom(g.SliceStr{"a", "b", "", "c", "", "", "d"})
fmt.Println(s.FilterEmpty())

View File

@ -9,6 +9,8 @@ package garray_test
import (
"fmt"
"github.com/gogf/gf/v2/internal/empty"
"github.com/gogf/gf/v2/container/garray"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/internal/json"
@ -552,6 +554,18 @@ func ExampleSortedStrArray_UnmarshalValue() {
// &{john ["Math","English","Sport"]}
}
func ExampleSortedStrArray_Filter() {
s := garray.NewSortedStrArrayFrom(g.SliceStr{"b", "a", "", "c", "", "", "d"})
fmt.Println(s)
fmt.Println(s.Filter(func(index int, value string) bool {
return empty.IsEmpty(value)
}))
// Output:
// ["","","","a","b","c","d"]
// ["a","b","c","d"]
}
func ExampleSortedStrArray_FilterEmpty() {
s := garray.NewSortedStrArrayFrom(g.SliceStr{"b", "a", "", "c", "", "", "d"})
fmt.Println(s)

View File

@ -12,6 +12,8 @@ import (
"testing"
"time"
"github.com/gogf/gf/v2/internal/empty"
"github.com/gogf/gf/v2/container/garray"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/internal/json"
@ -800,6 +802,36 @@ func TestArray_FilterNil(t *testing.T) {
})
}
func TestArray_Filter(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
values := g.Slice{0, 1, 2, 3, 4, "", g.Slice{}}
array := garray.NewArrayFromCopy(values)
t.Assert(array.Filter(func(index int, value interface{}) bool {
return empty.IsNil(value)
}).Slice(), values)
})
gtest.C(t, func(t *gtest.T) {
array := garray.NewArrayFromCopy(g.Slice{nil, 1, 2, 3, 4, nil})
t.Assert(array.Filter(func(index int, value interface{}) bool {
return empty.IsNil(value)
}), g.Slice{1, 2, 3, 4})
})
gtest.C(t, func(t *gtest.T) {
array := garray.NewArrayFrom(g.Slice{0, 1, 2, 3, 4, "", g.Slice{}})
t.Assert(array.Filter(func(index int, value interface{}) bool {
return empty.IsEmpty(value)
}), g.Slice{1, 2, 3, 4})
})
gtest.C(t, func(t *gtest.T) {
array := garray.NewArrayFrom(g.Slice{1, 2, 3, 4})
t.Assert(array.Filter(func(index int, value interface{}) bool {
return empty.IsEmpty(value)
}), g.Slice{1, 2, 3, 4})
})
}
func TestArray_FilterEmpty(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
array := garray.NewArrayFrom(g.Slice{0, 1, 2, 3, 4, "", g.Slice{}})

View File

@ -12,6 +12,8 @@ import (
"testing"
"time"
"github.com/gogf/gf/v2/internal/empty"
"github.com/gogf/gf/v2/container/garray"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/internal/json"
@ -803,6 +805,34 @@ func TestIntArray_UnmarshalValue(t *testing.T) {
// })
}
func TestIntArray_Filter(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
array := garray.NewIntArrayFrom(g.SliceInt{0, 1, 2, 3, 4, 0})
t.Assert(array.Filter(func(index int, value int) bool {
return empty.IsEmpty(value)
}), g.SliceInt{1, 2, 3, 4})
})
gtest.C(t, func(t *gtest.T) {
array := garray.NewIntArrayFrom(g.SliceInt{1, 2, 3, 4})
t.Assert(array.Filter(func(index int, value int) bool {
return empty.IsEmpty(value)
}), g.SliceInt{1, 2, 3, 4})
})
gtest.C(t, func(t *gtest.T) {
array := garray.NewIntArrayFrom(g.SliceInt{1, 2, 3, 4})
t.Assert(array.Filter(func(index int, value int) bool {
return value%2 == 0
}), g.SliceInt{1, 3})
})
gtest.C(t, func(t *gtest.T) {
array := garray.NewIntArrayFrom(g.SliceInt{1, 2, 3, 4})
t.Assert(array.Filter(func(index int, value int) bool {
return value%2 == 1
}), g.SliceInt{2, 4})
})
}
func TestIntArray_FilterEmpty(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
array := garray.NewIntArrayFrom(g.SliceInt{0, 1, 2, 3, 4, 0})

View File

@ -13,6 +13,8 @@ import (
"testing"
"time"
"github.com/gogf/gf/v2/internal/empty"
"github.com/gogf/gf/v2/container/garray"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/internal/json"
@ -813,6 +815,20 @@ func TestStrArray_UnmarshalValue(t *testing.T) {
t.Assert(v.Array.Slice(), g.SliceStr{"1", "2", "3"})
})
}
func TestStrArray_Filter(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
array := garray.NewStrArrayFrom(g.SliceStr{"", "1", "2", "0"})
t.Assert(array.Filter(func(index int, value string) bool {
return empty.IsEmpty(value)
}), g.SliceStr{"1", "2", "0"})
})
gtest.C(t, func(t *gtest.T) {
array := garray.NewStrArrayFrom(g.SliceStr{"1", "2"})
t.Assert(array.Filter(func(index int, value string) bool {
return empty.IsEmpty(value)
}), g.SliceStr{"1", "2"})
})
}
func TestStrArray_FilterEmpty(t *testing.T) {
gtest.C(t, func(t *gtest.T) {

View File

@ -13,6 +13,8 @@ import (
"testing"
"time"
"github.com/gogf/gf/v2/internal/empty"
"github.com/gogf/gf/v2/container/garray"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/internal/json"
@ -906,6 +908,33 @@ func TestSortedArray_UnmarshalValue(t *testing.T) {
t.Assert(v.Array.Slice(), g.Slice{1, 2, 3})
})
}
func TestSortedArray_Filter(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
values := g.Slice{0, 1, 2, 3, 4, "", g.Slice{}}
array := garray.NewSortedArrayFromCopy(values, gutil.ComparatorInt)
t.Assert(array.Filter(func(index int, value interface{}) bool {
return empty.IsNil(value)
}).Slice(), g.Slice{0, "", g.Slice{}, 1, 2, 3, 4})
})
gtest.C(t, func(t *gtest.T) {
array := garray.NewSortedArrayFromCopy(g.Slice{nil, 1, 2, 3, 4, nil}, gutil.ComparatorInt)
t.Assert(array.Filter(func(index int, value interface{}) bool {
return empty.IsNil(value)
}), g.Slice{1, 2, 3, 4})
})
gtest.C(t, func(t *gtest.T) {
array := garray.NewSortedArrayFrom(g.Slice{0, 1, 2, 3, 4, "", g.Slice{}}, gutil.ComparatorInt)
t.Assert(array.Filter(func(index int, value interface{}) bool {
return empty.IsEmpty(value)
}), g.Slice{1, 2, 3, 4})
})
gtest.C(t, func(t *gtest.T) {
array := garray.NewSortedArrayFrom(g.Slice{1, 2, 3, 4}, gutil.ComparatorInt)
t.Assert(array.Filter(func(index int, value interface{}) bool {
return empty.IsEmpty(value)
}), g.Slice{1, 2, 3, 4})
})
}
func TestSortedArray_FilterNil(t *testing.T) {
gtest.C(t, func(t *gtest.T) {

View File

@ -12,6 +12,8 @@ import (
"testing"
"time"
"github.com/gogf/gf/v2/internal/empty"
"github.com/gogf/gf/v2/container/garray"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/internal/json"
@ -774,6 +776,20 @@ func TestSortedIntArray_UnmarshalValue(t *testing.T) {
t.Assert(v.Array.Slice(), g.Slice{1, 2, 3})
})
}
func TestSortedIntArray_Filter(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
array := garray.NewSortedIntArrayFrom(g.SliceInt{0, 1, 2, 3, 4, 0})
t.Assert(array.Filter(func(index int, value int) bool {
return empty.IsEmpty(value)
}), g.SliceInt{1, 2, 3, 4})
})
gtest.C(t, func(t *gtest.T) {
array := garray.NewSortedIntArrayFrom(g.SliceInt{1, 2, 3, 4})
t.Assert(array.Filter(func(index int, value int) bool {
return empty.IsEmpty(value)
}), g.SliceInt{1, 2, 3, 4})
})
}
func TestSortedIntArray_FilterEmpty(t *testing.T) {
gtest.C(t, func(t *gtest.T) {

View File

@ -12,6 +12,8 @@ import (
"testing"
"time"
"github.com/gogf/gf/v2/internal/empty"
"github.com/gogf/gf/v2/container/garray"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/internal/json"
@ -786,6 +788,20 @@ func TestSortedStrArray_UnmarshalValue(t *testing.T) {
t.Assert(v.Array.Slice(), g.SliceStr{"1", "2", "3"})
})
}
func TestSortedStrArray_Filter(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
array := garray.NewSortedStrArrayFrom(g.SliceStr{"", "1", "2", "0"})
t.Assert(array.Filter(func(index int, value string) bool {
return empty.IsEmpty(value)
}), g.SliceStr{"0", "1", "2"})
})
gtest.C(t, func(t *gtest.T) {
array := garray.NewSortedStrArrayFrom(g.SliceStr{"1", "2"})
t.Assert(array.Filter(func(index int, value string) bool {
return empty.IsEmpty(value)
}), g.SliceStr{"1", "2"})
})
}
func TestSortedStrArray_FilterEmpty(t *testing.T) {
gtest.C(t, func(t *gtest.T) {