-
Notifications
You must be signed in to change notification settings - Fork 0
/
functional-util.test.mjs
61 lines (43 loc) · 1.9 KB
/
functional-util.test.mjs
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
import { expect } from 'chai';
import _ from './functional-util.js';
describe('functional utilities', function () {
describe('composition', function() {
it('should thread a value through functions right-to-left', function () {
const addThenMult = _.compose(x => x * 2, x => x + 1);
expect(addThenMult(5)).to.equal(12);
});
it('should apply a function when only one function given', function () {
const inc = _.compose(x => x + 1);
expect(inc(5)).to.equal(6);
});
it('should be the identity function when no functions given', function () {
expect(_.compose()(5)).to.equal(5);
});
});
describe('flow (reverse composition)', function() {
it('should thread a value through functions left-to-right', function () {
const multThenAdd = _.flow(x => x * 2, x => x + 1);
expect(multThenAdd(5)).to.equal(11);
});
it('should apply a function when only one function given', function () {
const inc = _.flow(x => x + 1);
expect(inc(5)).to.equal(6);
});
it('should be the identity function when no functions given', function () {
expect(_.flow()(5)).to.equal(5);
});
});
describe('flatMap', function () {
it('should flatten array result from mapping function into resulting array', function() {
const selfAndNext = x => ([ x, x + 1 ]);
expect(_.flatMap(selfAndNext, [10, 20, 30])).to.eql([10, 11, 20, 21, 30, 31]);
});
it('should flatten other iterable result from mapping function into resulting array', function() {
function* prevAndSelfGenerator(x) {
yield x - 1;
yield x;
};
expect(_.flatMap(prevAndSelfGenerator, [10, 20, 30])).to.eql([9, 10, 19, 20, 29, 30]);
});
});
});