Files
ontime/apps/server/src/utils/arrayUtils.ts
T
Carlos Valente 3603b836f6 refactor: cuesheet v2 (#435)
* 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
2023-07-22 09:32:40 +02:00

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;
}