Skip to content

Commit

Permalink
The Array.MoveAbove function didn't recalculate the baseIndex after…
Browse files Browse the repository at this point in the history
… the splice, meaning the item would end up in the wrong location.
  • Loading branch information
photonstorm committed Oct 4, 2024
1 parent af31ca2 commit 5747601
Showing 1 changed file with 6 additions and 10 deletions.
16 changes: 6 additions & 10 deletions src/utils/array/MoveAbove.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,19 +41,15 @@ var MoveAbove = function (array, item1, item2)
return array;
}

// Remove
// Remove item1 from its current position
array.splice(currentIndex, 1);

// Add in new location
if (baseIndex === array.length - 1)
{
array.push(item1);
}
else
{
array.splice(baseIndex, 0, item1);
}
// Recalculate baseIndex after removal
baseIndex = array.indexOf(item2);

// Insert item1 immediately after item2
array.splice(baseIndex + 1, 0, item1);

return array;
};

Expand Down

0 comments on commit 5747601

Please sign in to comment.