forked from ramda/ramda
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathswap.js
48 lines (39 loc) · 1.55 KB
/
swap.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
var R = require('../source/index.js');
var eq = require('./shared/eq.js');
var list = ['a', 'b', 'c', 'd', 'e', 'f'];
describe('swap', function() {
it('swaps an element from one index to the other', function() {
eq(R.swap(0, 1, list), ['b', 'a', 'c', 'd', 'e', 'f']);
eq(R.swap(2, 1, list), ['a', 'c', 'b', 'd', 'e', 'f']);
eq(R.swap(-1, 0, list), ['f', 'b', 'c', 'd', 'e', 'a']);
eq(R.swap(4, 1, list), ['a', 'e', 'c', 'd', 'b', 'f']);
});
it('does nothing when indexes are outside the list boundaries', function() {
eq(R.swap(-20, 2, list), list);
eq(R.swap(20, 2, list), list);
eq(R.swap(2, 20, list), list);
eq(R.swap(2, -20, list), list);
eq(R.swap(20, 20, list), list);
eq(R.swap(-20, -20, list), list);
});
it('does nothing when indexes are equal', function() {
eq(R.swap(0, 0, list), list);
});
it('should be the same when swapping index order', function() {
eq(R.swap(0, 1, list), R.swap(1, 0, list));
});
it('works with lists of arrays', function() {
eq(R.swap(0, -1, [['a', 'A'], ['b', 'B']]), [['b', 'B'], ['a', 'A']]);
});
it('swaps property values from one property to another', function() {
eq(R.swap('a', 'b', {a: 1, b: 2}), {a: 2, b: 1});
eq(R.swap('b', 'a', {a: 1, b: 2}), {a: 2, b: 1});
});
it('does nothing when property names are not defined', function() {
eq(R.swap('a', 'b', {a: 1}), {a: 1});
eq(R.swap('a', 'b', {b: 2}), {b: 2});
});
it('swaps characters in string from one index to another', function() {
eq(R.swap(0, 2, 'foo'), 'oof');
});
});