mirror of
https://github.com/cpvalente/ontime.git
synced 2026-08-21 06:59:09 +00:00
refactor: extract utility to merge two arrays
This commit is contained in:
@@ -61,7 +61,7 @@ export { customFieldLabelToKey, customKeyFromLabel } from './src/customField-uti
|
|||||||
export { deepmerge } from './src/externals/deepmerge.js';
|
export { deepmerge } from './src/externals/deepmerge.js';
|
||||||
|
|
||||||
// array utils
|
// array utils
|
||||||
export { deleteAtIndex, insertAtIndex, reorderArray } from './src/common/arrayUtils.js';
|
export { deleteAtIndex, insertAtIndex, mergeAtIndex, reorderArray } from './src/common/arrayUtils.js';
|
||||||
// object utils
|
// object utils
|
||||||
export { getPropertyFromPath, isObjectEmpty } from './src/common/objectUtils.js';
|
export { getPropertyFromPath, isObjectEmpty } from './src/common/objectUtils.js';
|
||||||
|
|
||||||
|
|||||||
@@ -25,6 +25,24 @@ export function insertAtIndex<T>(index: number, item: T, array: T[]): T[] {
|
|||||||
return modifiedArray;
|
return modifiedArray;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Inserts an array into another one of the same type at a given index
|
||||||
|
*/
|
||||||
|
export function mergeAtIndex<T>(index: number, newArray: T[], currentArray: T[]): T[] {
|
||||||
|
// Insert at beginning
|
||||||
|
if (index === 0) {
|
||||||
|
return [...newArray, ...currentArray];
|
||||||
|
}
|
||||||
|
|
||||||
|
// insert at end
|
||||||
|
else if (index >= currentArray.length) {
|
||||||
|
return [...currentArray, ...newArray];
|
||||||
|
}
|
||||||
|
|
||||||
|
// insert in the middle
|
||||||
|
return currentArray.toSpliced(index, 0, ...newArray);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Deletes array element at a given index
|
* Deletes array element at a given index
|
||||||
* @param index
|
* @param index
|
||||||
|
|||||||
Reference in New Issue
Block a user