main
Last change
on this file was d24f17c, checked in by Aleksandar Panovski <apano77@…>, 15 months ago |
Initial commit
|
-
Property mode
set to
100644
|
File size:
962 bytes
|
Line | |
---|
1 | import { compose, curry, insert, nth, remove } from 'ramda';
|
---|
2 |
|
---|
3 | /**
|
---|
4 | * Returns a new list with the item at the position `fromIdx` moved to the position `toIdx`. If the
|
---|
5 | * `toIdx` is out of the `list` range, the item will be placed at the last position of the `list`.
|
---|
6 | * When negative indices are provided, the behavior of the move is unspecified.
|
---|
7 | *
|
---|
8 | * @func move
|
---|
9 | * @memberOf RA
|
---|
10 | * @since {@link https://char0n.github.io/ramda-adjunct/2.8.0|v2.8.0}
|
---|
11 | * @category List
|
---|
12 | * @sig Number -> Number -> [a] -> [a]
|
---|
13 | * @param {number} fromIdx The position of item to be moved
|
---|
14 | * @param {number} toIdx The position of item after move
|
---|
15 | * @param {Array} list The list containing the item to be moved
|
---|
16 | * @return {Array}
|
---|
17 | * @example
|
---|
18 | *
|
---|
19 | * const list = ['a', 'b', 'c', 'd', 'e'];
|
---|
20 | * RA.move(1, 3, list) //=> ['a', 'c', 'd', 'b', 'e']
|
---|
21 | */
|
---|
22 | const move = curry((fromIdx, toIdx, list) =>
|
---|
23 | compose(insert(toIdx, nth(fromIdx, list)), remove(fromIdx, 1))(list)
|
---|
24 | );
|
---|
25 |
|
---|
26 | export default move;
|
---|
Note:
See
TracBrowser
for help on using the repository browser.