README
array-adjust
Move an array item to a different position
Origin
This tool is based off of array-move. It was being used in multiple places in a large app I work on, and I wanted to avoid breaking changes, so I simply recreated it to avoid such.
Install
$ npm install array-adjust
Usage
import { arrayAdjustImmutable } from "array-adjust";
const input = ["a", "b", "c"];
const array1 = arrayAdjustImmutable(input, 1, 2);
console.log(array1);
//=> ['a', 'c', 'b']
const array2 = arrayAdjustImmutable(input, -1, 0);
console.log(array2);
//=> ['c', 'a', 'b']
const array3 = arrayAdjustImmutable(input, -2, -3);
console.log(array3);
//=> ['b', 'a', 'c']
NOTE:
The default export is arrayAdjustMutable
API
arrayAdjustImmutable(array, fromIndex, toIndex)
Clones the given array
, moves the item to a new position in the new array, and then returns the new array. The given array
is not mutated.
arrayAdjustMutable(array, fromIndex, toIndex)
Moves the item to the new position in the array
array. Useful for huge arrays where absolute performance is needed.
array
Type: Array
fromIndex
Type: number
The index of item to move.
If negative, it will begin that many elements from the end.
toIndex
Type: number
The index of where to move the item.
If negative, it will begin that many elements from the end.