-
-
Notifications
You must be signed in to change notification settings - Fork 85
/
cata.js
125 lines (101 loc) · 4.01 KB
/
cata.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import monet from 'monet';
import folktale from 'folktale';
import ramdaFantasy from 'ramda-fantasy';
import * as R from 'ramda';
import { assert } from 'chai';
import * as RA from '../src';
describe('cata', function() {
context('monet support', function() {
specify('should support Either type', function() {
const eitherR = monet.Either.Right(1);
const eitherL = monet.Either.Left(2);
assert.strictEqual(RA.cata(null, R.identity, eitherR), 1);
assert.strictEqual(RA.cata(R.identity, null, eitherL), 2);
});
specify('should support Maybe type', function() {
const maybeR = monet.Maybe.Just(1);
const maybeL = monet.Maybe.Nothing();
assert.strictEqual(RA.cata(null, R.identity, maybeR), 1);
assert.isUndefined(RA.cata(v => v, null, maybeL));
});
specify('should support Validation type', function() {
const validationR = monet.Validation.Success(1);
const validationL = monet.Validation.Fail(2);
assert.strictEqual(RA.cata(null, R.identity, validationR), 1);
assert.strictEqual(RA.cata(R.identity, null, validationL), 2);
});
});
context('folktale support', function() {
specify('should support Maybe type', function() {
const maybeR = folktale.maybe.Just(1);
const maybeL = folktale.maybe.Nothing();
assert.strictEqual(RA.cata(null, R.identity, maybeR), 1);
assert.isUndefined(RA.cata(v => v, null, maybeL));
});
specify('should support Result type', function() {
const resultR = folktale.result.Ok(1);
const resultL = folktale.result.Error(2);
assert.strictEqual(RA.cata(null, R.identity, resultR), 1);
assert.strictEqual(
RA.cata(v => v, null, resultL),
2
);
});
specify('should support Validation type', function() {
const validationR = folktale.validation.Success(1);
const validationL = folktale.validation.Failure(2);
assert.strictEqual(RA.cata(null, R.identity, validationR), 1);
assert.strictEqual(
RA.cata(v => v, null, validationL),
2
);
});
});
context('ramda-fantasy support', function() {
specify('should support Either type', function() {
const eitherR = ramdaFantasy.Either.Right(1);
const eitherL = ramdaFantasy.Either.Left(2);
assert.strictEqual(RA.cata(null, R.identity, eitherR), 1);
assert.strictEqual(RA.cata(R.identity, null, eitherL), 2);
});
specify('should support Maybe type', function() {
const maybeR = ramdaFantasy.Maybe.Just(1);
const maybeL = ramdaFantasy.Maybe.Nothing();
assert.strictEqual(RA.cata(null, R.identity, maybeR), 1);
assert.isUndefined(RA.cata(v => v, null, maybeL));
});
});
context('given catamorphism without right function', function() {
specify('should return value from Left', function() {
const eitherL = monet.Either.Left(2);
assert.throws(RA.cata.bind(null, null, R.identity, eitherL), TypeError);
assert.strictEqual(RA.cata(R.identity, null, eitherL), 2);
});
});
context('given catamorphism without left function', function() {
specify('should return value from Right', function() {
const eitherR = monet.Either.Right(1);
assert.throws(RA.cata.bind(null, R.identity, null, eitherR), TypeError);
assert.strictEqual(RA.cata(null, R.identity, eitherR), 1);
});
});
context('given monad without catamorphic behavior', function() {
it('should throw Error', function() {
assert.throws(RA.cata.bind(null, R.identity, R.identity, {}), TypeError);
assert.throws(
RA.cata.bind(null, R.identity, R.identity, null),
TypeError
);
assert.throws(
RA.cata.bind(null, R.identity, R.identity, undefined),
TypeError
);
});
});
it('should curry', function() {
const eitherR = monet.Either.Right(1);
const eitherL = monet.Either.Left(2);
assert.strictEqual(RA.cata(R.identity)(R.identity)(eitherR), 1);
assert.strictEqual(RA.cata(R.identity, R.identity)(eitherL), 2);
});
});