forked from ramda/ramda
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcond.js
54 lines (46 loc) · 1.46 KB
/
cond.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
49
50
51
52
53
54
var R = require('../source/index.js');
var eq = require('./shared/eq.js');
describe('cond', function() {
it('returns a function', function() {
eq(typeof R.cond([]), 'function');
});
it('returns a conditional function', function() {
var fn = R.cond([
[R.equals(0), R.always('water freezes at 0°C')],
[R.equals(100), R.always('water boils at 100°C')],
[R.T, function(temp) { return 'nothing special happens at ' + temp + '°C'; }]
]);
eq(fn(0), 'water freezes at 0°C');
eq(fn(50), 'nothing special happens at 50°C');
eq(fn(100), 'water boils at 100°C');
});
it('returns a function which returns undefined if none of the predicates matches', function() {
var fn = R.cond([
[R.equals('foo'), R.always(1)],
[R.equals('bar'), R.always(2)]
]);
eq(fn('quux'), undefined);
});
it('predicates are tested in order', function() {
var fn = R.cond([
[R.T, R.always('foo')],
[R.T, R.always('bar')],
[R.T, R.always('baz')]
]);
eq(fn(), 'foo');
});
it('forwards all arguments to predicates and to transformers', function() {
var fn = R.cond([
[function(_, x) { return x === 42; }, function() { return arguments.length; }]
]);
eq(fn(21, 42, 84), 3);
});
it('retains highest predicate arity', function() {
var fn = R.cond([
[R.nAry(2, R.T), R.T],
[R.nAry(3, R.T), R.T],
[R.nAry(1, R.T), R.T]
]);
eq(fn.length, 3);
});
});