mirror of
https://github.com/samber/lo.git
synced 2025-10-19 22:34:49 +08:00
feat: add FromSlicePtr (#217)
* feat: add FromSlicePtr * Update README.md --------- Co-authored-by: Samuel Berthe <dev@samuel-berthe.fr>
This commit is contained in:
19
README.md
19
README.md
@@ -254,6 +254,7 @@ Type manipulation helpers:
|
|||||||
- [FromPtr](#fromptr)
|
- [FromPtr](#fromptr)
|
||||||
- [FromPtrOr](#fromptror)
|
- [FromPtrOr](#fromptror)
|
||||||
- [ToSlicePtr](#tosliceptr)
|
- [ToSlicePtr](#tosliceptr)
|
||||||
|
- [FromSlicePtr](#fromsliceptr)
|
||||||
- [ToAnySlice](#toanyslice)
|
- [ToAnySlice](#toanyslice)
|
||||||
- [FromAnySlice](#fromanyslice)
|
- [FromAnySlice](#fromanyslice)
|
||||||
- [Empty](#empty)
|
- [Empty](#empty)
|
||||||
@@ -2681,6 +2682,24 @@ ptr := lo.ToSlicePtr([]string{"hello", "world"})
|
|||||||
// []*string{"hello", "world"}
|
// []*string{"hello", "world"}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### FromSlicePtr
|
||||||
|
|
||||||
|
Returns a slice with the pointer values.
|
||||||
|
Returns a zero value in case of a nil pointer element.
|
||||||
|
|
||||||
|
```go
|
||||||
|
str1 := "hello"
|
||||||
|
str2 := "world"
|
||||||
|
|
||||||
|
ptr := lo.FromSlicePtr[string]([]*string{&str1, &str2, nil})
|
||||||
|
// []string{"hello", "world", ""}
|
||||||
|
|
||||||
|
ptr := lo.Compact(
|
||||||
|
lo.FromSlicePtr[string]([]*string{&str1, &str2, nil}),
|
||||||
|
)
|
||||||
|
// []string{"hello", "world"}
|
||||||
|
```
|
||||||
|
|
||||||
### ToAnySlice
|
### ToAnySlice
|
||||||
|
|
||||||
Returns a slice with all elements mapped to `any` type.
|
Returns a slice with all elements mapped to `any` type.
|
||||||
|
@@ -58,6 +58,17 @@ func ToSlicePtr[T any](collection []T) []*T {
|
|||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// FromSlicePtr returns a slice with the pointer values.
|
||||||
|
// Returns a zero value in case of a nil pointer element.
|
||||||
|
func FromSlicePtr[T any](collection []*T) []T {
|
||||||
|
return Map(collection, func(x *T, _ int) T {
|
||||||
|
if x == nil {
|
||||||
|
return Empty[T]()
|
||||||
|
}
|
||||||
|
return *x
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
// ToAnySlice returns a slice with all elements mapped to `any` type
|
// ToAnySlice returns a slice with all elements mapped to `any` type
|
||||||
func ToAnySlice[T any](collection []T) []any {
|
func ToAnySlice[T any](collection []T) []any {
|
||||||
result := make([]any, len(collection))
|
result := make([]any, len(collection))
|
||||||
|
@@ -121,6 +121,16 @@ func TestToSlicePtr(t *testing.T) {
|
|||||||
is.Equal(result1, []*string{&str1, &str2})
|
is.Equal(result1, []*string{&str1, &str2})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestFromSlicePtr(t *testing.T) {
|
||||||
|
is := assert.New(t)
|
||||||
|
|
||||||
|
str1 := "foo"
|
||||||
|
str2 := "bar"
|
||||||
|
result1 := FromSlicePtr([]*string{&str1, &str2, nil})
|
||||||
|
|
||||||
|
is.Equal(result1, []string{str1, str2, ""})
|
||||||
|
}
|
||||||
|
|
||||||
func TestToAnySlice(t *testing.T) {
|
func TestToAnySlice(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
is := assert.New(t)
|
is := assert.New(t)
|
||||||
|
Reference in New Issue
Block a user