mirror of
https://github.com/discordjs/discord.js.git
synced 2026-06-02 00:50:09 +00:00
* serious role position stuff * kill meh * Update Role.js * Update Guild.js * Update Role.js
18 lines
664 B
JavaScript
18 lines
664 B
JavaScript
/**
|
|
* Moves an element in an array *in place*
|
|
* @param {Array} array Array to modify
|
|
* @param {*} element Element to move
|
|
* @param {number} newIndex Index or offset to move the element to
|
|
* @param {boolean} [offset=false] Move the element by an offset amount rather than to a set index
|
|
* @returns {Array}
|
|
*/
|
|
module.exports = function moveElementInArray(array, element, newIndex, offset = false) {
|
|
const index = array.indexOf(element);
|
|
newIndex = (offset ? index : 0) + newIndex;
|
|
if (newIndex > -1 && newIndex < array.length) {
|
|
const removedElement = array.splice(index, 1)[0];
|
|
array.splice(newIndex, 0, removedElement);
|
|
}
|
|
return array;
|
|
};
|