feat:(slice add AppendIfAbsent function) (#52)

Co-authored-by: george.zheng <george.zheng@ambergroup.io>
This commit is contained in:
郑一诺她爸andgeorge.zheng authored and GitHub committed 2022-07-25 20:52:33 +08:00
1 parent c1b7500bcb
commit 70e213b3f7
2 files changed
+16

No files matched your search

+8
View File
@@ -851,3 +851,11 @@ func ToSlice[T any](value ...T) []T {
} }
return out return out
} }
// AppendIfAbsent only absent append the value
func AppendIfAbsent[T comparable](slices []T, value T) []T {
if !Contain(slices, value) {
slices = append(slices, value)
}
return slices
}
+8
View File
@@ -609,3 +609,11 @@ func TestToSlicePointer(t *testing.T) {
assert.Equal([]*string{&str1}, ToSlicePointer(str1)) assert.Equal([]*string{&str1}, ToSlicePointer(str1))
assert.Equal([]*string{&str1, &str2}, ToSlicePointer(str1, str2)) assert.Equal([]*string{&str1, &str2}, ToSlicePointer(str1, str2))
} }
func TestToAppendIfAbsent(t *testing.T) {
assert := internal.NewAssert(t, "TestToAppendIfAbsent")
str1 := []string{"a", "b"}
assert.Equal([]string{"a", "b"}, AppendIfAbsent(str1, "a"))
assert.Equal([]string{"a", "b", "c"}, AppendIfAbsent(str1, "c"))
}