refactor: extract utility to merge two arrays

This commit is contained in:
Carlos Valente
2025-05-16 20:03:41 +02:00
committed by arc-alex
parent 5c1694d802
commit c7d99073f6
2 changed files with 19 additions and 1 deletions
+1 -1
View File
@@ -61,7 +61,7 @@ export { customFieldLabelToKey, customKeyFromLabel } from './src/customField-uti
export { deepmerge } from './src/externals/deepmerge.js';
// array utils
export { deleteAtIndex, insertAtIndex, reorderArray } from './src/common/arrayUtils.js';
export { deleteAtIndex, insertAtIndex, mergeAtIndex, reorderArray } from './src/common/arrayUtils.js';
// object utils
export { getPropertyFromPath, isObjectEmpty } from './src/common/objectUtils.js';
+18
View File
@@ -25,6 +25,24 @@ export function insertAtIndex<T>(index: number, item: T, array: T[]): T[] {
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
* @param index