array.mjs 656 B

123456789101112131415161718192021
  1. function addUniqueItem(arr, item) {
  2. if (arr.indexOf(item) === -1)
  3. arr.push(item);
  4. }
  5. function removeItem(arr, item) {
  6. const index = arr.indexOf(item);
  7. if (index > -1)
  8. arr.splice(index, 1);
  9. }
  10. // Adapted from array-move
  11. function moveItem([...arr], fromIndex, toIndex) {
  12. const startIndex = fromIndex < 0 ? arr.length + fromIndex : fromIndex;
  13. if (startIndex >= 0 && startIndex < arr.length) {
  14. const endIndex = toIndex < 0 ? arr.length + toIndex : toIndex;
  15. const [item] = arr.splice(fromIndex, 1);
  16. arr.splice(endIndex, 0, item);
  17. }
  18. return arr;
  19. }
  20. export { addUniqueItem, moveItem, removeItem };