blob: 838e87442503e96b465c0ed9b3ce3e4b2d645a7d (
plain)
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
|
export function copy_move<T>(
array: T[],
oldIndex: number,
newIndex: number,
): T[] {
if (oldIndex < 0 || oldIndex >= array.length) {
throw new Error("Old index out of range.");
}
if (newIndex < 0) {
newIndex = 0;
}
if (newIndex >= array.length) {
newIndex = array.length - 1;
}
const result = array.slice();
const [element] = result.splice(oldIndex, 1);
result.splice(newIndex, 0, element);
return result;
}
export function copy_insert<T>(array: T[], index: number, element: T): T[] {
const result = array.slice();
result.splice(index, 0, element);
return result;
}
export function copy_push<T>(array: T[], element: T): T[] {
const result = array.slice();
result.push(element);
return result;
}
export function copy_delete<T>(array: T[], index: number): T[] {
const result = array.slice();
result.splice(index, 1);
return array;
}
|