forked from ramda/ramda
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheither.js
42 lines (36 loc) · 1.38 KB
/
either.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
const { Nothing, Just } = require('sanctuary');
var R = require('../source/index.js');
var eq = require('./shared/eq.js');
describe('either', function() {
it('combines two boolean-returning functions into one', function() {
var even = function(x) {return x % 2 === 0;};
var gt10 = function(x) {return x > 10;};
var f = R.either(even, gt10);
eq(f(8), true);
eq(f(13), true);
eq(f(7), false);
});
it('accepts functions that take multiple parameters', function() {
var between = function(a, b, c) {return a < b && b < c;};
var total20 = function(a, b, c) {return a + b + c === 20;};
var f = R.either(between, total20);
eq(f(4, 5, 8), true);
eq(f(12, 2, 6), true);
eq(f(7, 5, 1), false);
});
it('does not evaluate the second expression if the first one is true', function() {
var T = function() { return true; };
var Z = function() { effect = 'Z got evaluated'; };
var effect = 'not evaluated';
R.either(T, Z)();
eq(effect, 'not evaluated');
});
it('accepts fantasy-land applicative functors', function() {
eq(R.either(Just(true), Just(true)), Just(true));
eq(R.either(Just(true), Just(false)), Just(true));
eq(R.either(Just(false), Just(false)), Just(false));
eq(R.either(Just(true), Nothing), Nothing);
eq(R.either(Nothing, Just(false)), Nothing);
eq(R.either(Nothing, Nothing), Nothing);
});
});