Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2694 from MadDeveloper/add-move-function
feat(List): add move function
- Loading branch information
Showing
3 changed files
with
59 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import _curry3 from './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 | ||
* @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 = _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)); | ||
}); | ||
|
||
export default move; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
var R = require('../source'); | ||
var eq = require('./shared/eq'); | ||
|
||
var list = ['a', 'b', 'c', 'd', 'e', 'f']; | ||
|
||
describe.only('move', function() { | ||
it('moves an element from an index to another', function() { | ||
eq(R.move(0, 1, list), ['b', 'a', 'c', 'd', 'e', 'f']); | ||
eq(R.move(2, 1, list), ['a', 'c', 'b', 'd', 'e', 'f']); | ||
eq(R.move(-1, 0, list), ['f', 'a', 'b', 'c', 'd', 'e']); | ||
eq(R.move(0, -1, list), ['b', 'c', 'd', 'e', 'f', 'a']); | ||
}); | ||
|
||
it('does nothing when indexes are outside the list outbounds', function() { | ||
eq(R.move(-20, 2, list), list); | ||
eq(R.move(20, 2, list), list); | ||
eq(R.move(2, 20, list), list); | ||
eq(R.move(2, -20, list), list); | ||
eq(R.move(20, 20, list), list); | ||
eq(R.move(-20, -20, list), list); | ||
}); | ||
}); |