mirror of
https://github.com/cpvalente/ontime.git
synced 2026-08-05 15:33:59 +00:00
3603b836f6
* refactor: typescript migration * chore: remove prop-types package * refactor: file structure * refactor: migrate ontime table to tanstack table 8 * refactor: rundown controller uses service as data source * refactor: convert to typescript * feat: caching store * refactor: add delay values to rundown * feat: toggle past visibility * chore: update tests * refactor: add extra fields to CSV * style: show skipped events * chore: add route to navigation menu * style: allow jumping to bottom * chore: add tests
51 lines
1.1 KiB
TypeScript
51 lines
1.1 KiB
TypeScript
/**
|
|
* Inserts an item in an array at a given index
|
|
* @param index
|
|
* @param item
|
|
* @param array
|
|
*/
|
|
export function insertAtIndex<T>(index: number, item: T, array: T[]): T[] {
|
|
const modifiedArray = [...array];
|
|
|
|
// Insert at beginning
|
|
if (index === 0) {
|
|
modifiedArray.unshift(item);
|
|
}
|
|
|
|
// insert at end
|
|
else if (index >= modifiedArray.length) {
|
|
modifiedArray.push(item);
|
|
}
|
|
|
|
// insert in the middle
|
|
else {
|
|
modifiedArray.splice(index, 0, item);
|
|
}
|
|
|
|
return modifiedArray;
|
|
}
|
|
|
|
/**
|
|
* Deletes array element at a given index
|
|
* @param index
|
|
* @param array
|
|
*/
|
|
export function deleteAtIndex<T>(index: number, array: T[]) {
|
|
return array.filter((_, i) => i !== index);
|
|
}
|
|
|
|
export function reorderArray<T>(array: T[], fromIndex: number, toIndex: number) {
|
|
if (fromIndex === toIndex) {
|
|
return array; // No change needed, return the original array
|
|
}
|
|
|
|
const modifiedArray = [...array];
|
|
|
|
// delete in from
|
|
const [reorderedItem] = modifiedArray.splice(fromIndex, 1);
|
|
|
|
// reinsert item at to
|
|
modifiedArray.splice(toIndex, 0, reorderedItem);
|
|
return modifiedArray;
|
|
}
|