Welcome to mirror list, hosted at ThFree Co, Russian Federation.

array_utility.js « utils « lib « javascripts « assets « app - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 197e7790ed7eedc1dc77175d914d54f7b8238b53 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/**
 * Return a shallow copy of an array with two items swapped.
 *
 * @param {Array} array - The source array
 * @param {Number} leftIndex - Index of the first item
 * @param {Number} rightIndex - Index of the second item
 * @returns {Array} new array with the left and right items swapped
 */
export const swapArrayItems = (array, leftIndex = 0, rightIndex = 0) => {
  const copy = array.slice();

  if (leftIndex >= array.length || leftIndex < 0 || rightIndex >= array.length || rightIndex < 0) {
    return copy;
  }

  const temp = copy[leftIndex];
  copy[leftIndex] = copy[rightIndex];
  copy[rightIndex] = temp;
  return copy;
};