MCPcopy
hub / github.com/directus/directus / moveInArray

Function moveInArray

packages/utils/shared/move-in-array.ts:1–28  ·  view source on GitHub ↗
(array: T[], fromIndex: number, toIndex: number)

Source from the content-addressed store, hash-verified

1export function moveInArray<T = any>(array: T[], fromIndex: number, toIndex: number): T[] {
2 const item = array[fromIndex];
3 const length = array.length;
4 const diff = fromIndex - toIndex;
5
6 if (item === undefined) return array;
7
8 if (diff > 0) {
9 // move left
10 return [
11 ...array.slice(0, toIndex),
12 item,
13 ...array.slice(toIndex, fromIndex),
14 ...array.slice(fromIndex + 1, length),
15 ];
16 } else if (diff < 0) {
17 // move right
18 const targetIndex = toIndex + 1;
19 return [
20 ...array.slice(0, fromIndex),
21 ...array.slice(fromIndex + 1, targetIndex),
22 item,
23 ...array.slice(targetIndex, length),
24 ];
25 }
26
27 return array;
28}

Callers 3

changeManualSortFunction · 0.90
changeGroupSortFunction · 0.90

Calls 1

sliceMethod · 0.80

Tested by

no test coverage detected