Small preact-based (like React.js) project
https://inga-lovinde.github.io/static/komoot-demo/
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
31 lines
888 B
31 lines
888 B
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;
|
|
};
|
|
|