Skip to content

Commit

Permalink
ptr.ToSlice
Browse files Browse the repository at this point in the history
  • Loading branch information
mikhalytch committed Nov 29, 2023
1 parent 34b9afa commit 8e831ca
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
10 changes: 10 additions & 0 deletions ptr/to_slice.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package ptr

// ToSlice returns unary slices of optional value, otherwise the empty slice.
func ToSlice[T any](t *T) []T {
if t == nil {
return nil
}

return []T{*t}
}
14 changes: 14 additions & 0 deletions ptr/to_slice_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package ptr_test

import (
"testing"

"github.com/mikhalytch/eggs/ptr"

"github.com/stretchr/testify/require"
)

func TestToSlice(t *testing.T) {
require.Empty(t, ptr.ToSlice[int](nil))
require.Equal(t, []string{"abc"}, ptr.ToSlice[string](ptr.Of("abc")))
}

0 comments on commit 8e831ca

Please sign in to comment.