parent
32df61715d
commit
252554df6e
@ -0,0 +1,31 @@ |
||||
export const reorderElements = <T>( |
||||
collection: T[], |
||||
oldIndex: number, |
||||
newIndex: number, |
||||
) => { |
||||
if (newIndex < 0) { |
||||
return [ |
||||
...collection.slice(0, oldIndex), |
||||
...collection.slice(oldIndex + 1, collection.length), |
||||
]; |
||||
} |
||||
|
||||
if (oldIndex < newIndex) { |
||||
return [ |
||||
...collection.slice(0, oldIndex), |
||||
...collection.slice(oldIndex + 1, newIndex + 1), |
||||
...collection.slice(oldIndex, oldIndex + 1), |
||||
...collection.slice(newIndex + 1, collection.length), |
||||
]; |
||||
} |
||||
if (oldIndex > newIndex) { |
||||
return [ |
||||
...collection.slice(0, newIndex), |
||||
...collection.slice(oldIndex, oldIndex + 1), |
||||
...collection.slice(newIndex, oldIndex), |
||||
...collection.slice(oldIndex + 1, collection.length), |
||||
]; |
||||
} |
||||
|
||||
return collection; |
||||
}; |
Loading…
Reference in new issue