Skip to content

Commit

Permalink
Add slice mutation utils (#24)
Browse files Browse the repository at this point in the history
  • Loading branch information
aidenwallis authored Apr 6, 2023
1 parent 0207d48 commit a70778a
Show file tree
Hide file tree
Showing 6 changed files with 174 additions and 0 deletions.
11 changes: 11 additions & 0 deletions utils/insert_in_slice.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package utils

// InsertInSlice inserts a value into a slice at a given position
//
// If the position gives is out of bounds then it resorts to inserting to the end of the slice
func InsertInSlice[T any](in []T, valueToAdd T, index int) []T {
if index > len(in) {
index = len(in)
}
return append(in[:index], append([]T{valueToAdd}, in[index:]...)...)
}
50 changes: 50 additions & 0 deletions utils/insert_in_slice_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package utils_test

import (
"log"
"testing"

"github.com/aidenwallis/go-utils/internal/assert"
"github.com/aidenwallis/go-utils/utils"
)

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

testCases := map[string]struct {
in []int
out []int
valueToAdd int
index int
}{
"normal operation": {
in: []int{0, 1, 2, 3, 4},
out: []int{0, 1, 2000, 2, 3, 4},
valueToAdd: 2000,
index: 2,
},

"out of bounds": {
in: []int{0, 1, 2, 3},
out: []int{0, 1, 2, 3, 2000},
valueToAdd: 2000,
index: 2000,
},
}

for name, testCase := range testCases {
testCase := testCase

t.Run(name, func(t *testing.T) {
t.Parallel()

result := utils.InsertInSlice(testCase.in, testCase.valueToAdd, testCase.index)
log.Println(result)

assert.Equal(t, len(testCase.out), len(result))
for i, v := range result {
assert.Equal(t, testCase.out[i], v)
}
})
}
}
12 changes: 12 additions & 0 deletions utils/move_in_slice.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package utils

// MoveInSlice moves a value within the slice to a specified new position
//
// If either the old or new position is out of bounds, the existing slice contents is returned.
func MoveInSlice[T any](in []T, oldPosition, newPosition int) []T {
if oldPosition >= len(in) {
return in
}
value := in[oldPosition]
return InsertInSlice(RemoveFromSlice(in, oldPosition), value, newPosition)
}
48 changes: 48 additions & 0 deletions utils/move_in_slice_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package utils_test

import (
"testing"

"github.com/aidenwallis/go-utils/internal/assert"
"github.com/aidenwallis/go-utils/utils"
)

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

testCases := map[string]struct {
in []int
out []int
oldPosition int
newPosition int
}{
"normal operation": {
in: []int{0, 1, 2, 3, 4},
out: []int{0, 3, 1, 2, 4},
oldPosition: 3,
newPosition: 1,
},

"out of bounds in old position": {
in: []int{0, 1, 2, 3, 4},
out: []int{0, 1, 2, 3, 4},
oldPosition: 5,
newPosition: 0,
},
}

for name, testCase := range testCases {
testCase := testCase

t.Run(name, func(t *testing.T) {
t.Parallel()

result := utils.MoveInSlice(testCase.in, testCase.oldPosition, testCase.newPosition)

assert.Equal(t, len(testCase.out), len(result))
for i, v := range result {
assert.Equal(t, testCase.out[i], v)
}
})
}
}
9 changes: 9 additions & 0 deletions utils/remove_from_slice.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package utils

// RemoveFromSlice removes a value from slice at a given index
func RemoveFromSlice[T any](in []T, index int) []T {
if index >= len(in) {
return in
}
return append(in[:index], in[index+1:]...)
}
44 changes: 44 additions & 0 deletions utils/remove_from_slice_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package utils_test

import (
"testing"

"github.com/aidenwallis/go-utils/internal/assert"
"github.com/aidenwallis/go-utils/utils"
)

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

testCases := map[string]struct {
in []int
out []int
indexToRemove int
}{
"normal operation": {
in: []int{0, 1, 2, 3},
out: []int{0, 1, 3},
indexToRemove: 2,
},

"does not panic when out of bounds": {
in: []int{0, 1, 2},
out: []int{0, 1, 2},
indexToRemove: 4,
},
}

for name, testCase := range testCases {
testCase := testCase

t.Run(name, func(t *testing.T) {
t.Parallel()

result := utils.RemoveFromSlice(testCase.in, testCase.indexToRemove)
assert.Equal(t, len(testCase.out), len(result))
for i, v := range result {
assert.Equal(t, testCase.out[i], v)
}
})
}
}

0 comments on commit a70778a

Please sign in to comment.