Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | 7x 1x 7x 7x 7x 7x 7x 7x 7x 7x 7x | import { RelativeIndex } from "./_types";
/**
* Moves elements of an Array from sourceIndex to targetIndex
*
* @mutation
*
* @example
* Move two elements from index `2` to index `0`
* ```ts
* const array = [1, 2, 3, 4];
* move(2, 0, 2)(array);
* console.log(array); // [3, 4, 1, 2]
* ```
*
* @param array
* @param sourceIndex
* @param targetIndex
*/
export const move =
(sourceIndex: RelativeIndex, targetIndex: RelativeIndex, moveCount = 1) =>
(array: unknown[]): void => {
if (process.env.NODE_ENV !== "production") {
console.assert(sourceIndex >= 0);
console.assert(sourceIndex < array.length);
console.assert(targetIndex >= 0);
console.assert(targetIndex < array.length);
console.assert(moveCount >= 0);
console.assert(moveCount <= array.length - sourceIndex);
}
array.splice(
targetIndex - moveCount + 1,
0,
...array.splice(sourceIndex, moveCount)
);
};
|