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.
 
 
 

36 lines
1.1 KiB

export const reorderElements = <T>(
collection: T[],
oldIndex: number,
newIndex: number,
) => {
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;
};
export const toPairs = <T>(collection: T[]): [T, T][] =>
collection.flatMap((element, index, collection) => {
if (!index) {
return [];
}
const previousElement = collection[index - 1];
// We just checked that index of the previous element is inside array bounds
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
return [[previousElement!, element]];
});