Skip to content

Commit

Permalink
Adds slice utils and default value getter (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
aidenwallis authored Aug 30, 2022
1 parent 120b323 commit ae8fff4
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 0 deletions.
10 changes: 10 additions & 0 deletions utils/index_in_slice.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package utils

// IndexInSlice returns whether an index exists within a given slice
func IndexInSlice[T any](v []T, index int) bool {
if index < 0 {
// slices cannot be less than 0 in size
return false
}
return len(v) > index
}
19 changes: 19 additions & 0 deletions utils/index_in_slice_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package utils_test

import (
"testing"

"github.com/aidenwallis/go-utils/utils"
"github.com/stretchr/testify/assert"
)

func TestIndexInSlice(t *testing.T) {
t.Parallel()
v := []int{0, 1, 2, 3}
assert.False(t, utils.IndexInSlice(v, -1))
assert.True(t, utils.IndexInSlice(v, 0))
assert.True(t, utils.IndexInSlice(v, 1))
assert.True(t, utils.IndexInSlice(v, 2))
assert.True(t, utils.IndexInSlice(v, 3))
assert.False(t, utils.IndexInSlice(v, 4))
}
11 changes: 11 additions & 0 deletions utils/value_at_index.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package utils

import "github.com/aidenwallis/go-utils/val"

// ValueAtIndex returns the value at a given index in a slice
func ValueAtIndex[T any](v []T, index int) (T, bool) {
if IndexInSlice(v, index) {
return v[index], true
}
return val.Default[T](), false
}
59 changes: 59 additions & 0 deletions utils/value_at_index_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package utils_test

import (
"testing"

"github.com/aidenwallis/go-utils/utils"
"github.com/aidenwallis/go-utils/val"
"github.com/stretchr/testify/assert"
)

func TestValueAtIndex(t *testing.T) {
t.Parallel()

// handles normal slices
{
v := []int{0, 1, 2}

{
r, ok := utils.ValueAtIndex(v, 0)
assert.Equal(t, 0, r)
assert.True(t, ok)
}

{
r, ok := utils.ValueAtIndex(v, 1)
assert.Equal(t, 1, r)
assert.True(t, ok)
}

{
r, ok := utils.ValueAtIndex(v, 2)
assert.Equal(t, 2, r)
assert.True(t, ok)
}

{
r, ok := utils.ValueAtIndex(v, 3)
assert.Equal(t, 0, r)
assert.False(t, ok)
}
}

// handles pointer slices
{
v := []*int{val.Pointer(0)}

{
r, ok := utils.ValueAtIndex(v, 0)
assert.Equal(t, 0, *r)
assert.True(t, ok)
}

{
r, ok := utils.ValueAtIndex(v, 1)
assert.Nil(t, r)
assert.False(t, ok)
}
}
}
7 changes: 7 additions & 0 deletions val/default.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package val

// Default returns the default value of any type (if pointer, it is nil).
func Default[T any]() T {
var r T
return r
}
14 changes: 14 additions & 0 deletions val/default_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package val_test

import (
"testing"

"github.com/aidenwallis/go-utils/val"
"github.com/stretchr/testify/assert"
)

func TestDefault(t *testing.T) {
t.Parallel()
assert.Equal(t, 0, val.Default[int]())
assert.Nil(t, val.Default[*int]())
}

0 comments on commit ae8fff4

Please sign in to comment.