1 | /**
|
2 | * @license
|
3 | * Copyright Google LLC All Rights Reserved.
|
4 | *
|
5 | * Use of this source code is governed by an MIT-style license that can be
|
6 | * found in the LICENSE file at https://angular.io/license
|
7 | */
|
8 | /**
|
9 | * Moves an item one index in an array to another.
|
10 | * @param array Array in which to move the item.
|
11 | * @param fromIndex Starting index of the item.
|
12 | * @param toIndex Index to which the item should be moved.
|
13 | */
|
14 | export function moveItemInArray(array, fromIndex, toIndex) {
|
15 | const from = clamp(fromIndex, array.length - 1);
|
16 | const to = clamp(toIndex, array.length - 1);
|
17 | if (from === to) {
|
18 | return;
|
19 | }
|
20 | const target = array[from];
|
21 | const delta = to < from ? -1 : 1;
|
22 | for (let i = from; i !== to; i += delta) {
|
23 | array[i] = array[i + delta];
|
24 | }
|
25 | array[to] = target;
|
26 | }
|
27 | /**
|
28 | * Moves an item from one array to another.
|
29 | * @param currentArray Array from which to transfer the item.
|
30 | * @param targetArray Array into which to put the item.
|
31 | * @param currentIndex Index of the item in its current array.
|
32 | * @param targetIndex Index at which to insert the item.
|
33 | */
|
34 | export function transferArrayItem(currentArray, targetArray, currentIndex, targetIndex) {
|
35 | const from = clamp(currentIndex, currentArray.length - 1);
|
36 | const to = clamp(targetIndex, targetArray.length);
|
37 | if (currentArray.length) {
|
38 | targetArray.splice(to, 0, currentArray.splice(from, 1)[0]);
|
39 | }
|
40 | }
|
41 | /**
|
42 | * Copies an item from one array to another, leaving it in its
|
43 | * original position in current array.
|
44 | * @param currentArray Array from which to copy the item.
|
45 | * @param targetArray Array into which is copy the item.
|
46 | * @param currentIndex Index of the item in its current array.
|
47 | * @param targetIndex Index at which to insert the item.
|
48 | *
|
49 | */
|
50 | export function copyArrayItem(currentArray, targetArray, currentIndex, targetIndex) {
|
51 | const to = clamp(targetIndex, targetArray.length);
|
52 | if (currentArray.length) {
|
53 | targetArray.splice(to, 0, currentArray[currentIndex]);
|
54 | }
|
55 | }
|
56 | /** Clamps a number between zero and a maximum. */
|
57 | function clamp(value, max) {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