mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-10 08:33:30 +01:00
serious role position stuff (#1159)
* serious role position stuff * kill meh * Update Role.js * Update Guild.js * Update Role.js
This commit is contained in:
17
src/util/MoveElementInArray.js
Normal file
17
src/util/MoveElementInArray.js
Normal file
@@ -0,0 +1,17 @@
|
||||
/**
|
||||
* 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;
|
||||
};
|
||||
Reference in New Issue
Block a user