UNPKG

1.2 kBJavaScriptView Raw
1import _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.0
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
21var move =
22/*#__PURE__*/
23_curry3(function (from, to, list) {
24 var length = list.length;
25 var result = list.slice();
26 var positiveFrom = from < 0 ? length + from : from;
27 var positiveTo = to < 0 ? length + to : to;
28 var item = result.splice(positiveFrom, 1);
29 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));
30});
31
32export default move;
\No newline at end of file