forked from ramda/ramda
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtryCatch.js
67 lines (52 loc) · 1.77 KB
/
tryCatch.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
55
56
57
58
59
60
61
62
63
64
65
66
67
var R = require('../source/index.js');
var eq = require('./shared/eq.js');
describe('tryCatch', function() {
function headX(ls) {
return ls[0].x;
}
function catcher() {
return 10101;
}
it('takes two functions and return a function', function() {
var mayThrow = R.tryCatch(headX, catcher);
eq(typeof mayThrow, 'function');
});
it('returns a function with the same arity as the `tryer` function', function() {
function a1(a) { return a; }
function a2(a, b) { return b; }
function a3(a, b, c) { return c; }
function a4(a, b, c, d) { return d; }
eq(R.tryCatch(a1, catcher).length, a1.length);
eq(R.tryCatch(a2, catcher).length, a2.length);
eq(R.tryCatch(a3, catcher).length, a3.length);
eq(R.tryCatch(a4, catcher).length, a4.length);
});
it('returns the value of the first function if it does not throw', function() {
var mayThrow = R.tryCatch(headX, catcher);
eq(mayThrow([{x: 10}, {x: 20}, {x: 30}]), 10);
});
it('returns the value of the second function if the first function throws', function() {
function throw10() {
throw new Error(10);
}
function eCatcher(e) {
return Number(e.message);
}
var mayThrow = R.tryCatch(headX, catcher);
eq(mayThrow([]), 10101);
var willThrow = R.tryCatch(throw10, eCatcher);
eq(willThrow([]), 10);
eq(willThrow([{}, {}, {}]), 10);
});
it('the second function gets passed the error object and the same arguments as the first function', function() {
function thrower(a, b, c) {
void c;
throw new Error('throwerError');
}
function catch3(e, a, b, c) {
return [e.message, a, b, c].join(' ');
}
var mayThrow = R.tryCatch(thrower, catch3);
eq(mayThrow('A', 'B', 'C'), 'throwerError A B C');
});
});