arcade-physics
Version:
Use Arcade Physics without Phaser.
48 lines • 1.37 kB
JavaScript
;
/**
* @author Richard Davey <rich@photonstorm.com>
* @copyright 2020 Photon Storm Ltd.
* @license {@link https://opensource.org/licenses/MIT|MIT License}
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.MoveAbove = void 0;
/**
* 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.
*/
const MoveAbove = (array, item1, item2) => {
if (item1 === item2) {
return array;
}
const currentIndex = array.indexOf(item1);
const 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;
};
exports.MoveAbove = MoveAbove;
//# sourceMappingURL=MoveAbove.js.map