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) {
|
---|
58 | return Math.max(0, Math.min(max, value));
|
---|
59 | }
|
---|
60 | //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiZHJhZy11dGlscy5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uLy4uLy4uLy4uLy4uLy4uL3NyYy9jZGsvZHJhZy1kcm9wL2RyYWctdXRpbHMudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBQUE7Ozs7OztHQU1HO0FBRUg7Ozs7O0dBS0c7QUFDSCxNQUFNLFVBQVUsZUFBZSxDQUFVLEtBQVUsRUFBRSxTQUFpQixFQUFFLE9BQWU7SUFDckYsTUFBTSxJQUFJLEdBQUcsS0FBSyxDQUFDLFNBQVMsRUFBRSxLQUFLLENBQUMsTUFBTSxHQUFHLENBQUMsQ0FBQyxDQUFDO0lBQ2hELE1BQU0sRUFBRSxHQUFHLEtBQUssQ0FBQyxPQUFPLEVBQUUsS0FBSyxDQUFDLE1BQU0sR0FBRyxDQUFDLENBQUMsQ0FBQztJQUU1QyxJQUFJLElBQUksS0FBSyxFQUFFLEVBQUU7UUFDZixPQUFPO0tBQ1I7SUFFRCxNQUFNLE1BQU0sR0FBRyxLQUFLLENBQUMsSUFBSSxDQUFDLENBQUM7SUFDM0IsTUFBTSxLQUFLLEdBQUcsRUFBRSxHQUFHLElBQUksQ0FBQyxDQUFDLENBQUMsQ0FBQyxDQUFDLENBQUMsQ0FBQyxDQUFDLENBQUMsQ0FBQztJQUVqQyxLQUFLLElBQUksQ0FBQyxHQUFHLElBQUksRUFBRSxDQUFDLEtBQUssRUFBRSxFQUFFLENBQUM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