20 lines
667 B
JavaScript
20 lines
667 B
JavaScript
|
|
export function moveInArray(items, fromIndex, toIndex) {
|
||
|
|
if (!Array.isArray(items)) return [];
|
||
|
|
if (fromIndex === toIndex || fromIndex < 0 || toIndex < 0 || fromIndex >= items.length || toIndex >= items.length) {
|
||
|
|
return items;
|
||
|
|
}
|
||
|
|
const next = [...items];
|
||
|
|
const [moved] = next.splice(fromIndex, 1);
|
||
|
|
next.splice(toIndex, 0, moved);
|
||
|
|
return next;
|
||
|
|
}
|
||
|
|
|
||
|
|
export function reorderPayload(items) {
|
||
|
|
return Object.fromEntries((items || []).map((item, index) => [item.id, index]));
|
||
|
|
}
|
||
|
|
|
||
|
|
export function movedItemId(before, after) {
|
||
|
|
const moved = (after || []).find((item, index) => item.id !== before?.[index]?.id);
|
||
|
|
return moved?.id || after?.[0]?.id || null;
|
||
|
|
}
|