-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patharray-move.ts
41 lines (37 loc) · 967 Bytes
/
array-move.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
/**
* Please refer to the terms of the license agreement in the root of the project
*
* (c) 2024 Feedzai
*/
/**
* Re-orders an array given 2 indexes:
*
* - The old position;
* - and the new position.
*
* @example
* ```js
* import { arrayMove } from "@feedzai/js-utilities";
*
* const DATA = [
* "Homer",
* "Marge",
* "Lisa",
* "Bart",
* "Maggie"
* ];
*
* const SORTED = arrayMove(DATA, 2, 0);
* // ["Lisa", "Homer", "Marge", "Bart", "Maggie"];
* ```
*/
export function arrayMove<Source>(array: Source[], fromIndex: number, toIndex: number): Source[] {
const orderedArray = [...array];
const startIndex = fromIndex < 0 ? orderedArray.length + fromIndex : fromIndex;
if (startIndex >= 0 && startIndex < orderedArray.length) {
const endIndex = toIndex < 0 ? orderedArray.length + toIndex : toIndex;
const [item] = orderedArray.splice(fromIndex, 1);
orderedArray.splice(endIndex, 0, item);
}
return orderedArray;
}