mirror of
https://github.com/directus/directus.git
synced 2026-02-03 09:55:03 -05:00
27 lines
605 B
TypeScript
27 lines
605 B
TypeScript
export default function moveInArray(array: readonly any[], fromIndex: number, toIndex: number) {
|
|
const item = array[fromIndex];
|
|
const length = array.length;
|
|
const diff = fromIndex - toIndex;
|
|
|
|
if (diff > 0) {
|
|
// move left
|
|
return [
|
|
...array.slice(0, toIndex),
|
|
item,
|
|
...array.slice(toIndex, fromIndex),
|
|
...array.slice(fromIndex + 1, length),
|
|
];
|
|
} else if (diff < 0) {
|
|
// move right
|
|
const targetIndex = toIndex + 1;
|
|
return [
|
|
...array.slice(0, fromIndex),
|
|
...array.slice(fromIndex + 1, targetIndex),
|
|
item,
|
|
...array.slice(targetIndex, length),
|
|
];
|
|
}
|
|
|
|
return array;
|
|
}
|