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:
Gus Caplan
2017-02-01 16:02:16 -06:00
committed by Amish Shah
parent a2d6791cd8
commit 1e94a9e2a4
4 changed files with 39 additions and 23 deletions

View 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;
};