-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patheither.js
354 lines (298 loc) · 10.1 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
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
'use strict'
const expect = require('chai').expect
const LazyEither = require('../index')
const FL = require('fantasy-land')
, R = require('ramda')
, S = require('sanctuary')
describe('Constructors', function() {
it('should construct a Lazy Left value', function(done) {
LazyEither.Left('bad').value(res => {
expect(res).to.deep.equal(S.Left('bad'))
done()
})
})
it('should construct a Lazy Right value', function(done) {
LazyEither.Right('good').value(res => {
expect(res).to.deep.equal(S.Right('good'))
done()
})
})
it('should construct using LazyEither["fantasy-land/of"]', function(done) {
S.of(LazyEither, 'good').value(res => {
expect(res).to.deep.equal(S.Right('good'))
done()
})
})
it('should handle async delayed values', function(done) {
let startTime = Date.now()
LazyEither(resolve => {
setTimeout(() => {
return resolve(S.Right('hello'))
}, 1000)
}).value(res => {
let endTime = Date.now()
expect(res).to.deep.equal(S.Right('hello'))
expect(endTime - startTime).to.be.closeTo(1000, 100)
done()
})
})
})
describe('Chain', function() {
before(function() {
this.delayed = t => LazyEither(resolve => {
if (t > 1000)
return resolve(S.Left('Delay too long'))
setTimeout(() => {
return resolve(S.Right('hello'))
}, t)
})
})
it('should not execute the rest of the chain if it fails and should propagate the Left value', function(done) {
let startTime = Date.now()
LazyEither.Left('bad')[FL.chain](this.delayed).value(res => {
let endTime = Date.now()
expect(res).to.deep.equal(S.Left('bad'))
expect(endTime - startTime).to.be.closeTo(0, 100)
done()
})
})
it('should pass the result to the next item in the chain (1)', function(done) {
let startTime = Date.now()
LazyEither.Right(1500)[FL.chain](this.delayed).value(res => {
let endTime = Date.now()
expect(res).to.deep.equal(S.Left('Delay too long'))
expect(endTime - startTime).to.be.closeTo(0, 100)
done()
})
})
it('should pass the result to the next item in the chain (2)', function(done) {
let startTime = Date.now()
LazyEither.Right(800)[FL.chain](this.delayed).value(res => {
let endTime = Date.now()
expect(res).to.deep.equal(S.Right('hello'))
expect(endTime - startTime).to.be.closeTo(800, 100)
done()
})
})
it('should pass the result to the next item in the chain (3)', function(done) {
let startTime = Date.now()
LazyEither.Right(1001)[FL.chain](this.delayed)[FL.chain](LazyEither.lift(R.identity)).value(res => {
let endTime = Date.now()
expect(res).to.deep.equal(S.Left('Delay too long'))
expect(endTime - startTime).to.be.closeTo(0, 100)
done()
})
})
it('should pass the result to the next item in the chain (4)', function(done) {
let startTime = Date.now()
LazyEither.Right(1001)[FL.chain](LazyEither.lift(a => a - 1))[FL.chain](this.delayed).value(res => {
let endTime = Date.now()
expect(res).to.deep.equal(S.Right('hello'))
expect(endTime - startTime).to.be.closeTo(1000, 100)
done()
})
})
it('should pass the result to the next item in the chain (5)', function(done) {
let startTime = Date.now()
LazyEither.Right(200)[FL.chain](this.delayed)[FL.chain](LazyEither.lift(hi => `${hi} world`)).value(res => {
let endTime = Date.now()
expect(res).to.deep.equal(S.Right('hello world'))
expect(endTime - startTime).to.be.closeTo(200, 100)
done()
})
})
it('should satisfy associativity', function(done) {
let val1, val2
let add3 = LazyEither.lift(R.add(3))
, mult2 = LazyEither.lift(R.multiply(2))
let resolveIfDone = _ => { if (val1 && val2) done() }
LazyEither.Right(5)[FL.chain](add3)[FL.chain](mult2).value(res => {
val1 = res
expect(res).to.deep.equal(S.Right(16))
resolveIfDone()
})
LazyEither.Right(5)[FL.chain](x => add3(x)[FL.chain](mult2)).value(res => {
val2 = res
expect(res).to.deep.equal(S.Right(16))
resolveIfDone()
})
})
})
describe('Functor', function() {
it('should apply the map to a Right value (1)', function(done) {
LazyEither.Right(1)[FL.map](R.add(7)).value(res => {
expect(res).to.deep.equal(S.Right(8))
done()
})
})
it('should apply the map to a Right value (2)', function(done) {
LazyEither.Right('foo')[FL.map](a => `${a} bar`)[FL.map](a => `${a} baz`).value(res => {
expect(res).to.deep.equal(S.Right('foo bar baz'))
done()
})
})
it('should not apply the map function to a Left value (1)', function(done) {
LazyEither.Left('bad')[FL.map](a => `${a} bar`)[FL.map](a => `${a} baz`).value(res => {
expect(res).to.deep.equal(S.Left('bad'))
done()
})
})
it('should satisfy identity (1)', function(done) {
LazyEither.Right('good')[FL.map](R.identity).value(res => {
expect(res).to.deep.equal(S.Right('good'))
done()
})
})
it('should satisfy identity (2)', function(done) {
LazyEither.Left('bad')[FL.map](R.identity).value(res => {
expect(res).to.deep.equal(S.Left('bad'))
done()
})
})
it('should satisfy composition', function(done) {
let val1, val2
let resolveIfDone = _ => { if (val1 && val2) done() }
LazyEither.Right(5)[FL.map](R.add(3))[FL.map](R.multiply(2)).value(res => {
val1 = res
expect(res).to.deep.equal(S.Right(16))
resolveIfDone()
})
LazyEither.Right(5)[FL.map](x => R.multiply(2)(R.add(3, x))).value(res => {
val2 = res
expect(res).to.deep.equal(S.Right(16))
resolveIfDone()
})
})
})
describe('Applicative', function() {
it('should be apply the ap function', function(done) {
LazyEither.Right(4)['fantasy-land/ap'](LazyEither.Right(R.multiply(3))).value(res => {
expect(res).to.deep.equal(S.Right(12))
done()
})
})
it('should propagate a Left value (1)', function(done) {
LazyEither.Right(2)[FL.ap](LazyEither.Left('bad')).value(res => {
expect(res).to.deep.equal(S.Left('bad'))
done()
})
})
it('should propagate a Left value (2)', function(done) {
LazyEither.Left('bad')[FL.ap](LazyEither.Right(R.add(5))).value(res => {
expect(res).to.deep.equal(S.Left('bad'))
done()
})
})
it('should be able to compose multiple ap functions', function(done) {
LazyEither.Right(5)[FL.ap](LazyEither.Right(R.add(3)))[FL.ap](LazyEither.Right(R.multiply(2))).value(res => {
expect(res).to.deep.equal(S.Right(16))
done()
})
})
it('should satisfy identity', function(done) {
LazyEither.Right('good')[FL.ap](LazyEither.Right(R.identity)).value(res => {
expect(res).to.deep.equal(S.Right('good'))
done()
})
})
it('should satisfy homomorphism', function(done) {
LazyEither.Right(5)[FL.ap](LazyEither.Right(R.add(3))).value(res => {
LazyEither.Right(R.add(3, 5)).value(res2 => {
expect(res).to.deep.equal(res2)
done()
})
})
})
it('should satisfy interchange', function(done) {
LazyEither.Right(5)[FL.ap](LazyEither.Right(R.add(3))).value(res => {
LazyEither.Right(R.add(3))[FL.ap](LazyEither.Right(f => f(5))).value(res2 => {
expect(res).to.deep.equal(S.Right(8))
expect(res).to.deep.equal(res2)
done()
})
})
})
})
describe('Setoid (contitional)', function() {
it('should return true if a and b are equal (reflexivity) (1)', function(done) {
LazyEither.Right('good')[FL.equals](LazyEither.Right('good'), res => {
expect(res).to.be.true
done()
})
})
it('should return true if a and b are equal (reflexivity) (2)', function(done) {
LazyEither.Left('bad')[FL.equals](LazyEither.Left('bad'), res => {
expect(res).to.be.true
done()
})
})
it('should return false if a and b are not equal (1)', function(done) {
LazyEither.Right(1)[FL.equals](LazyEither.Right(2), res => {
expect(res).to.be.false
done()
})
})
it('should return false if a and b are not equal (2)', function(done) {
LazyEither.Left(1)[FL.equals](LazyEither.Left(2), res => {
expect(res).to.be.false
done()
})
})
it('should return false if a and b are equal but Left and Right', function(done) {
LazyEither.Left(1)[FL.equals](LazyEither.Right(1), res => {
expect(res).to.be.false
done()
})
})
it('should satisfy symmetry', function(done) {
LazyEither.Right(7 + 5)[FL.equals](LazyEither.Right(3 * 4), res => {
LazyEither.Right(3 * 4)[FL.equals](LazyEither.Right(7 + 5), res2 => {
expect(res).to.deep.equal(res2)
done()
})
})
})
it('should satisfy transitivity', function(done) {
LazyEither.Right(7 + 5)[FL.equals](LazyEither.Right(3 * 4), res => {
LazyEither.Right(3 * 4)[FL.equals](LazyEither.Right(72 / 6), res2 => {
LazyEither.Right(7 + 5)[FL.equals](LazyEither.Right(72 / 6), res2 => {
expect(res).to.deep.equal(res2)
done()
})
})
})
})
})
describe('Lift', function() {
it('should lift a function of arity 1', function(done) {
let lifted = LazyEither.lift(R.multiply(3))
LazyEither.Right(3)[FL.chain](lifted).value(res => {
expect(res).to.deep.equal(S.Right(9))
done()
})
})
it('should lift a function of arity n', function(done) {
let add = R.curry((a, b, c, d, e) => a + b + c + d + e)
, lifted = LazyEither.liftN(5, add)
LazyEither.Right(3)[FL.chain](lifted(4, 5, 6, 7)).value(res => {
expect(res).to.deep.equal(S.Right(25))
done()
})
})
})
describe('Promote', function() {
it('should promote an Either type to a LazyEither type (Left)', function(done) {
LazyEither.promote(S.Left('bad')).value(either => {
expect(either.isLeft).to.be.true
expect(either.value).to.equal('bad')
done()
})
})
it('should promote an Either type to a LazyEither type (Right)', function(done) {
LazyEither.promote(S.Right('good')).value(either => {
expect(either.isRight).to.be.true
expect(either.value).to.equal('good')
done()
})
})
})