-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.test.js
627 lines (479 loc) · 14.1 KB
/
index.test.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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
const { Lazy, Producer, operators } = require("./");
const EE = require("events");
describe("Lazy", () => {
it("creates an async iterable out of a generator function", async done => {
const gen = function*() {
yield 1;
yield 2;
};
const lazy = new Lazy(gen);
const values = [];
for await (let v of lazy) {
values.push(v);
}
expect(values).toEqual([1, 2]);
return done();
});
});
describe("Producer", () => {
it("is a Lazy", () => {
const producer = new Producer();
expect(producer instanceof Lazy).toBe(true);
});
it("produces a new value to the iteration when next is called", done => {
const producer = new Producer();
const message = {};
operators.forEach(value => {
expect(value).toBe(message);
return done();
}, producer);
producer.next(message);
});
it("does not produce values after complete is called", done => {
const producer = new Producer();
const message = {};
operators.toArray(producer).then(values => {
expect(values.length).toBe(1);
return done();
});
producer.next(message);
producer.complete();
producer.next(message);
});
it("does not produce values after error is called", done => {
const producer = new Producer();
const message = {};
operators.toArray(producer).then(values => {
expect(values.length).toBe(1);
return done();
});
producer.next(message);
producer.error();
producer.next(message);
});
it("produces an error when error is called", async done => {
const producer = new Producer();
const message = {};
setTimeout(() => producer.error(message), 100);
try {
for await (let v of producer) {
expect(v).not.toBeDefined();
}
} catch (e) {
expect(e).toBe(message);
} finally {
done();
}
});
});
describe("Operators", () => {
describe("map", () => {
it("returns a new Lazy", async done => {
const gen = function*() {
yield 1;
yield 2;
};
const lazy = new Lazy(gen);
const mapped = operators.map(n => n * 2, lazy);
expect(mapped instanceof Lazy).toBe(true);
return done();
});
it("transform each iteration by the given function", async done => {
const gen = function*() {
yield 1;
yield 2;
};
const lazy = new Lazy(gen);
const mapped = operators.map(n => n * 2, lazy);
const values = [];
for await (let v of mapped) {
values.push(v);
}
expect(values).toEqual([2, 4]);
return done();
});
});
describe("filter", () => {
it("returns a new Lazy", async done => {
const gen = function*() {
yield 1;
yield 2;
};
const lazy = new Lazy(gen);
const filtered = operators.filter(n => n > 1, lazy);
expect(filtered instanceof Lazy).toBe(true);
return done();
});
it("returns an iterator of only values that pass the predicate", async done => {
const gen = function*() {
yield 1;
yield 2;
};
const lazy = new Lazy(gen);
const filtered = operators.filter(n => n > 1, lazy);
const values = [];
for await (let v of filtered) {
values.push(v);
}
expect(values).toEqual([2]);
return done();
});
});
describe("tap", () => {
it("returns a new Lazy", async done => {
const gen = function*() {
yield 1;
yield 2;
};
const lazy = new Lazy(gen);
const tapped = operators.tap(n => n > 1, lazy);
expect(tapped instanceof Lazy).toBe(true);
return done();
});
it("calls the function for each iteration", async done => {
const gen = function*() {
yield 1;
yield 2;
};
const fn = jest.fn();
const lazy = new Lazy(gen);
const tapped = operators.tap(fn, lazy);
for await (let _ of tapped) {
}
expect(fn).toHaveBeenCalledTimes(2);
return done();
});
});
describe("take", () => {
it("returns a Lazy", () => {
const gen = function*() {};
const lazy = new Lazy(gen);
const took = operators.take(2, lazy);
expect(took instanceof Lazy).toBe(true);
});
it("returns an iterable that only takes the first n values of the passed in iterable", async done => {
const gen = function*() {
yield 1;
yield 2;
yield 3;
yield 4;
};
const lazy = new Lazy(gen);
const took = operators.take(2, lazy);
const values = [];
for await (let v of took) {
values.push(v);
}
expect(values).toEqual([1, 2]);
return done();
});
it("stops if n is larger than the given iterable", async done => {
const gen = function*() {
yield 1;
yield 2;
};
const lazy = new Lazy(gen);
const took = operators.take(4, lazy);
const values = [];
for await (let v of took) {
values.push(v);
}
expect(values).toEqual([1, 2]);
return done();
});
});
describe("range", () => {
it("returns a Lazy", () => {
const lazy = operators.range(1);
expect(lazy instanceof Lazy).toBe(true);
});
it("defaults to an interable of start -> Infinity", async done => {
const lazy = operators.range(1);
const took = operators.take(1000, lazy);
const values = [];
for await (let v of took) {
values.push(v);
}
expect(values.length).toBe(1000);
return done();
});
it("creates an iterable from start to end inclusive", async done => {
const lazy = operators.range(1, 5);
const values = [];
for await (let v of lazy) {
values.push(v);
}
expect(values).toEqual([1, 2, 3, 4, 5]);
return done();
});
it("maps each item given the transformer", async done => {
const lazy = operators.range(1, 5, n => n * 2);
const values = [];
for await (let v of lazy) {
values.push(v);
}
expect(values).toEqual([2, 4, 6, 8, 10]);
return done();
});
});
describe("reduce", () => {
it("returns a promise", () => {
const lazy = operators.empty();
const reduced = operators.reduce(a => a, {}, lazy);
expect(reduced.then).toBeDefined();
});
it("reduces the async iterator into a single value", async done => {
const lazy = operators.range(1, 5);
const reduced = operators.reduce((a, c) => a + c, 0, lazy);
return reduced.then(value => expect(value).toBe(15)).then(done);
});
});
describe("flatMap", () => {
it("returns a Lazy", () => {
const lazy = operators.range(1, 5);
const flattened = operators.flatMap(num => range(0, num), lazy);
expect(flattened instanceof Lazy).toBe(true);
});
it("maps over an iterator, flattening the return value", async done => {
const lazy = operators.range(1, 5);
const flattened = operators.flatMap(num => operators.range(0, num), lazy);
const values = [];
for await (let v of flattened) {
values.push(v);
}
expect(values).toEqual([
0,
1,
0,
1,
2,
0,
1,
2,
3,
0,
1,
2,
3,
4,
0,
1,
2,
3,
4,
5
]);
return done();
});
});
describe("skip", () => {
it("returns a Lazy", () => {
const lazy = operators.range(1);
const skipped = operators.skip(5, lazy);
expect(skipped instanceof Lazy).toBe(true);
});
it("skips the first n numbers of items", async done => {
const lazy = operators.range(1, 5);
const skipped = operators.skip(1, lazy);
const values = [];
for await (let v of skipped) {
values.push(v);
}
expect(values).toEqual([2, 3, 4, 5]);
return done();
});
it("returns an empty Lazy if skipped too many", async done => {
const lazy = operators.range(1, 2);
const skipped = operators.skip(4, lazy);
const values = [];
for await (let v of skipped) {
values.push(v);
}
expect(values).toEqual([]);
return done();
});
});
describe("fromArray", () => {
it("returns a Lazy", () => {
const lazy = operators.fromArray([]);
expect(lazy instanceof Lazy).toBe(true);
});
it("creates a Lazy from the given array values", async done => {
const arr = [1, 2, 3];
const lazy = operators.fromArray(arr);
const values = [];
for await (let v of lazy) {
values.push(v);
}
expect(values).toEqual(arr);
return done();
});
it("creates a Lazy from a given iterable", async done => {
const arr = new Set([1, 2, 3]);
const lazy = operators.fromArray(arr);
const values = [];
for await (let v of lazy) {
values.push(v);
}
expect(values).toEqual([...arr]);
return done();
});
});
describe("fromPromise", () => {
it("returns a Lazy", () => {
const lazy = operators.fromPromise(Promise.resolve({}));
expect(lazy instanceof Lazy).toBe(true);
});
it("returns a Lazy of the promise", async done => {
const value = {};
const lazy = operators.fromPromise(Promise.resolve(value));
const values = [];
for await (let v of lazy) {
values.push(v);
}
expect(values).toEqual([value]);
return done();
});
});
describe("forEach", () => {
it("returns a Promise", () => {
const lazy = operators.range(1, 5);
const eached = operators.forEach(() => {}, lazy);
expect(eached.then).toBeDefined();
});
it("calls a function for each Lazy item", async done => {
const lazy = operators.range(1, 5);
const fn = jest.fn();
return operators
.forEach(fn, lazy)
.then(() => expect(fn).toHaveBeenCalledTimes(5))
.then(done);
});
});
describe("toArray", () => {
it("returns an array of the Lazy values", async done => {
const range = operators.range(1, 5);
return operators
.toArray(range)
.then(list => expect(list).toEqual([1, 2, 3, 4, 5]))
.then(done);
});
});
describe("empty", () => {
it("returns a Lazy", () => {
const empty = operators.empty();
expect(empty instanceof Lazy).toBe(true);
});
it("returns an empty iterable", done => {
const empty = operators.empty();
operators
.toArray(empty)
.then(list => expect(list).toEqual([]))
.then(done);
});
});
describe("merge", () => {
it("returns a Lazy", () => {
const merged = operators.merge(
(a, b) => a,
operators.range(1, 2),
operators.range(1, 2)
);
expect(merged instanceof Lazy).toBe(true);
});
it("merges the two iterators together by the given function", done => {
const itera = operators.range(1, 2);
const iterb = operators.range(1, 2);
const merged = operators.merge((a, b) => a + b, itera, iterb);
return operators
.toArray(merged)
.then(list => expect(list).toEqual([2, 4]))
.then(done);
});
it("will stop merging once a is done", done => {
const itera = operators.range(1, 2);
const iterb = operators.range(1);
const merged = operators.merge((a, b) => a + b, itera, iterb);
return operators
.toArray(merged)
.then(list => expect(list).toEqual([2, 4]))
.then(done);
});
});
describe("fromEvent", () => {
it("returns a Lazy", () => {
const lazy = operators.fromEvent("event", {});
expect(lazy instanceof Lazy).toBe(true);
});
it("returns an iterator of the events of the given emitter", async done => {
const event = new EE();
const iter = operators.fromEvent("event", event);
const message = {};
operators.forEach(value => {
expect(value).toBe(message);
done();
}, iter);
event.emit("event", message);
});
});
describe("takeUntil", () => {
it("returns a Lazy", () => {
const iter = operators.takeUntil(a => true, operators.empty());
expect(iter instanceof Lazy).toBe(true);
});
it('takes values from the iterator until the predicate returns true', done => {
const pred = (num) => num ? true : false
const base = operators.range(0)
const iter = operators.takeUntil(pred, base)
return operators.toArray(iter)
.then(list => {
expect(list.length).toBe(1)
}).then(done)
})
});
describe("takeWhile", () => {
it("returns a Lazy", () => {
const iter = operators.takeWhile(a => true, operators.empty());
expect(iter instanceof Lazy).toBe(true);
});
it('takes values from the iterator while the predicate returns true', done => {
const pred = (num) => num === 0 ? true : false
const base = operators.range(0)
const iter = operators.takeWhile(pred, base)
return operators.toArray(iter)
.then(list => {
expect(list.length).toBe(1)
}).then(done)
})
});
describe("skipUntil", () => {
it("returns a Lazy", () => {
const iter = operators.skipUntil(a => true, operators.empty());
expect(iter instanceof Lazy).toBe(true);
});
it('skips values until the predicate returns true', done => {
const pred = (num) => num === 0 ? false : true
const base = operators.range(0, 1)
const iter = operators.skipUntil(pred, base)
return operators.toArray(iter)
.then(list => {
expect(list.length).toBe(1)
}).then(done)
})
});
describe("skipWhile", () => {
it("returns a Lazy", () => {
const iter = operators.skipWhile(a => true, operators.empty());
expect(iter instanceof Lazy).toBe(true);
});
it('skips values until the predicate returns false', done => {
const pred = (num) => num === 0 ? true : false
const base = operators.range(0, 1)
const iter = operators.skipWhile(pred, base)
return operators.toArray(iter)
.then(list => {
expect(list.length).toBe(1)
}).then(done)
})
});
});