-
Notifications
You must be signed in to change notification settings - Fork 7.1k
/
MoveAbove.js
58 lines (50 loc) · 1.22 KB
/
MoveAbove.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/**
* @author Richard Davey <rich@phaser.io>
* @copyright 2013-2024 Phaser Studio Inc.
* @license {@link https://opensource.org/licenses/MIT|MIT License}
*/
/**
* Moves the given array element above another one in the array.
* The array is modified in-place.
*
* @function Phaser.Utils.Array.MoveAbove
* @since 3.55.0
*
* @param {array} array - The input array.
* @param {*} item1 - The element to move above base element.
* @param {*} item2 - The base element.
*
*
* @return {array} The input array.
*/
var MoveAbove = function (array, item1, item2)
{
if (item1 === item2)
{
return array;
}
var currentIndex = array.indexOf(item1);
var baseIndex = array.indexOf(item2);
if (currentIndex < 0 || baseIndex < 0)
{
throw new Error('Supplied items must be elements of the same array');
}
if (currentIndex > baseIndex)
{
// item1 is already above item2
return array;
}
// Remove
array.splice(currentIndex, 1);
// Add in new location
if (baseIndex === array.length - 1)
{
array.push(item1);
}
else
{
array.splice(baseIndex, 0, item1);
}
return array;
};
module.exports = MoveAbove;