move.js 1.2 KB

1234567891011121314151617181920212223242526272829303132
  1. import _curry3 from "./internal/_curry3.js";
  2. /**
  3. * Move an item, at index `from`, to index `to`, in a list of elements.
  4. * A new list will be created containing the new elements order.
  5. *
  6. * @func
  7. * @memberOf R
  8. * @since v0.27.1
  9. * @category List
  10. * @sig Number -> Number -> [a] -> [a]
  11. * @param {Number} from The source index
  12. * @param {Number} to The destination index
  13. * @param {Array} list The list which will serve to realise the move
  14. * @return {Array} The new list reordered
  15. * @example
  16. *
  17. * R.move(0, 2, ['a', 'b', 'c', 'd', 'e', 'f']); //=> ['b', 'c', 'a', 'd', 'e', 'f']
  18. * R.move(-1, 0, ['a', 'b', 'c', 'd', 'e', 'f']); //=> ['f', 'a', 'b', 'c', 'd', 'e'] list rotation
  19. */
  20. var move =
  21. /*#__PURE__*/
  22. _curry3(function (from, to, list) {
  23. var length = list.length;
  24. var result = list.slice();
  25. var positiveFrom = from < 0 ? length + from : from;
  26. var positiveTo = to < 0 ? length + to : to;
  27. var item = result.splice(positiveFrom, 1);
  28. return positiveFrom < 0 || positiveFrom >= list.length || positiveTo < 0 || positiveTo >= list.length ? list : [].concat(result.slice(0, positiveTo)).concat(item).concat(result.slice(positiveTo, list.length));
  29. });
  30. export default move;