UNPKG

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