Ramda Adjunct 3.0.0

move.js

  1. import { compose, curry, insert, nth, remove } from 'ramda';
  2. /**
  3. * Returns a new list with the item at the position `fromIdx` moved to the position `toIdx`. If the
  4. * `toIdx` is out of the `list` range, the item will be placed at the last position of the `list`.
  5. * When negative indices are provided, the behavior of the move is unspecified.
  6. *
  7. * @func move
  8. * @memberOf RA
  9. * @since {@link https://char0n.github.io/ramda-adjunct/2.8.0|v2.8.0}
  10. * @category List
  11. * @sig Number -> Number -> [a] -> [a]
  12. * @param {number} fromIdx The position of item to be moved
  13. * @param {number} toIdx The position of item after move
  14. * @param {Array} list The list containing the item to be moved
  15. * @return {Array}
  16. * @example
  17. *
  18. * const list = ['a', 'b', 'c', 'd', 'e'];
  19. * RA.move(1, 3, list) //=> ['a', 'c', 'd', 'b', 'e']
  20. */
  21. const move = curry((fromIdx, toIdx, list) =>
  22. compose(insert(toIdx, nth(fromIdx, list)), remove(fromIdx, 1))(list)
  23. );
  24. export default move;