35 lines
1.2 KiB
JavaScript
35 lines
1.2 KiB
JavaScript
|
var _curry3 =
|
||
|
/*#__PURE__*/
|
||
|
require("./internal/_curry3");
|
||
|
/**
|
||
|
* Move an item, at index `from`, to index `to`, in a list of elements.
|
||
|
* A new list will be created containing the new elements order.
|
||
|
*
|
||
|
* @func
|
||
|
* @memberOf R
|
||
|
* @since v0.27.0
|
||
|
* @category List
|
||
|
* @sig Number -> Number -> [a] -> [a]
|
||
|
* @param {Number} from The source index
|
||
|
* @param {Number} to The destination index
|
||
|
* @param {Array} list The list which will serve to realise the move
|
||
|
* @return {Array} The new list reordered
|
||
|
* @example
|
||
|
*
|
||
|
* R.move(0, 2, ['a', 'b', 'c', 'd', 'e', 'f']); //=> ['b', 'c', 'a', 'd', 'e', 'f']
|
||
|
* R.move(-1, 0, ['a', 'b', 'c', 'd', 'e', 'f']); //=> ['f', 'a', 'b', 'c', 'd', 'e'] list rotation
|
||
|
*/
|
||
|
|
||
|
|
||
|
var move =
|
||
|
/*#__PURE__*/
|
||
|
_curry3(function (from, to, list) {
|
||
|
var length = list.length;
|
||
|
var result = list.slice();
|
||
|
var positiveFrom = from < 0 ? length + from : from;
|
||
|
var positiveTo = to < 0 ? length + to : to;
|
||
|
var item = result.splice(positiveFrom, 1);
|
||
|
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));
|
||
|
});
|
||
|
|
||
|
module.exports = move;
|