From afb0ae90cf74393573767fb6d8315fca0f1d0d2b Mon Sep 17 00:00:00 2001 From: Andros Fenollosa Date: Sat, 19 Feb 2022 17:27:10 +0100 Subject: [PATCH] Update index.ts --- src/index.ts | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/src/index.ts b/src/index.ts index ec1181b..7313d28 100644 --- a/src/index.ts +++ b/src/index.ts @@ -214,6 +214,29 @@ export function arraySwapIndex(firstIndex: number, secondIndex: number, list: Ar return tempList; } +/** + * Returns a copy of the list where an index has been moved to another position. + * @param {number} indexFrom + * @param {number} indexTo + * @param {Array} list + * @example + * + * arrayMoveIndex(2, 0, ['a', 'b', 'c', 'd']) + * // => ['c', 'a', 'b', 'd'] + */ +function arrayMoveIndex(indexFrom: number, indexTo: number, list: Array): Array { + // Save value to move + const moveValue = list[indexFrom]; + // Deletes value to be moved + const listNotValue = list.filter( + (currentValue, currentIndex) => currentIndex != indexFrom + ); + // Concat all fragments: start to position + moveValue + rest array + return listNotValue + .slice(0, indexTo) + .concat(moveValue, listNotValue.slice(indexTo)); +} + /** * Returns a File in text.