1
0
mirror of https://github.com/duke-git/lancet.git synced 2026-02-11 00:02:28 +08:00

feat: add UniqueByField

This commit is contained in:
dudaodong
2024-06-24 19:36:02 +08:00
parent ca373b00a7
commit 95b516e278
3 changed files with 91 additions and 1 deletions

View File

@@ -2,11 +2,12 @@ package slice
import (
"fmt"
"github.com/duke-git/lancet/v2/internal"
"math"
"reflect"
"strconv"
"testing"
"github.com/duke-git/lancet/v2/internal"
)
func TestContain(t *testing.T) {
@@ -735,6 +736,33 @@ func TestUniqueBy(t *testing.T) {
assert.Equal([]int{1, 2, 3, 0}, actual)
}
func TestUniqueByField(t *testing.T) {
t.Parallel()
assert := internal.NewAssert(t, "TestUniqueByField")
type User struct {
ID int `json:"id"`
Name string `json:"name"`
}
users := []User{
{ID: 1, Name: "a"},
{ID: 2, Name: "b"},
{ID: 1, Name: "c"},
}
uniqueUsers, err := UniqueByField(users, "ID")
if err != nil {
t.Error(err)
}
assert.Equal([]User{
{ID: 1, Name: "a"},
{ID: 2, Name: "b"},
}, uniqueUsers)
}
func TestUnion(t *testing.T) {
t.Parallel()