mirror of
https://github.com/directus/directus.git
synced 2026-01-25 16:07:57 -05:00
* move composables, types and utils to shared * move composables, utils and types to shared * expose utils and composables in extensionsSDK * fix missing dependencies * Sort index.ts exports * Do the thing Co-authored-by: rijkvanzanten <rijkvanzanten@me.com>
29 lines
640 B
TypeScript
29 lines
640 B
TypeScript
export function moveInArray<T = any>(array: T[], fromIndex: number, toIndex: number): T[] {
|
|
const item = array[fromIndex];
|
|
const length = array.length;
|
|
const diff = fromIndex - toIndex;
|
|
|
|
if (item === undefined) return array;
|
|
|
|
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;
|
|
}
|