/
MoveUp.js
36 lines (31 loc) · 894 Bytes
/
MoveUp.js
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
/**
* @author Richard Davey <rich@photonstorm.com>
* @copyright 2020 Photon Storm Ltd.
* @license {@link https://opensource.org/licenses/MIT|MIT License}
*/
/**
* Moves the given array element up one place in the array.
* The array is modified in-place.
*
* @function Phaser.Utils.Array.MoveUp
* @since 3.4.0
*
* @param {array} array - The input array.
* @param {*} item - The element to move up the array.
*
* @return {array} The input array.
*/
var MoveUp = function (array, item)
{
var currentIndex = array.indexOf(item);
if (currentIndex !== -1 && currentIndex < array.length - 1)
{
// The element one above `item` in the array
var item2 = array[currentIndex + 1];
var index2 = array.indexOf(item2);
array[currentIndex] = item2;
array[index2] = item;
}
return array;
};
module.exports = MoveUp;