checkpoint: implemented a less complex looking moveAndShuffle function
All checks were successful
/ test (pull_request) Successful in 38s
/ lint (pull_request) Successful in 41s

This commit is contained in:
Dan Anglin 2024-01-24 18:31:41 +00:00
parent 68dcdae56d
commit fb764ad6dc
Signed by: dananglin
GPG key ID: 0C1D44CFBEE68638

View file

@ -1,48 +1,46 @@
package board package board
type shuffleDirection int
const (
forwards shuffleDirection = iota
backwards
)
// moveAndShuffle creates a new list where the element specified at the index of the current // moveAndShuffle creates a new list where the element specified at the index of the current
// position is moved to the index of the target position. Elements within the range of the // position is moved to the index of the target position. Elements within the range of the
// old and new positions will then shuffle forwards or backwards in the list depending on the // old and new positions will then shuffle forwards or backwards in the list depending on the
// direction of the move. // direction of the move.
// This is currently used to move specified columns forwards or backwards on the Kanban board. // This is currently used to move specified columns forwards or backwards on the Kanban board.
// When a column changes position the other columns shuffle forward or backwards as required. // When a column changes position the other columns shuffle forward or backwards as required.
func moveAndShuffle(input []Status, currentIndex, targetIndex int) []Status { func moveAndShuffle(input []Status, oldIndex, newIndex int) []Status {
var shuffle shuffleDirection if newIndex == oldIndex {
return input
if targetIndex < currentIndex {
// affected elements will need to shuffle backwards if the focused
// element moves towards the beginning of the list...
shuffle = backwards
} else {
// ...or else the elements need to shuffle forwards if the focused
// element moves towards the end of the list.
shuffle = forwards
} }
output := make([]Status, len(input)) output := make([]Status, len(input))
for ind := range input { // copy the original slice and assign the target element to the
switch { // new index.
case (shuffle == backwards) && (ind < targetIndex || ind > currentIndex): copy(output, input)
output[ind] = input[ind]
case (shuffle == backwards) && (ind == currentIndex): output[newIndex] = input[oldIndex]
output[targetIndex] = input[ind]
case (shuffle == backwards): // shuffle the elements in range down the slice if the target element moves
output[ind+1] = input[ind] // up the list.
case (shuffle == forwards) && (ind < currentIndex || ind > targetIndex): if newIndex < oldIndex {
output[ind] = input[ind] for index := newIndex; index <= oldIndex; index++ {
case (shuffle == forwards) && (ind == currentIndex): if index == oldIndex {
output[targetIndex] = input[ind] continue
case (shuffle == forwards):
output[ind-1] = input[ind]
} }
output[index+1] = input[index]
}
return output
}
// shuffle the elements in range up the slice if the target element moves
// down the list.
for index := oldIndex; index <= newIndex; index++ {
if index == oldIndex {
continue
}
output[index-1] = input[index]
} }
return output return output