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
|
import b from './b';
// curry
const add = (x, y) => x + y;
const curriedAdd = b.curry(add);
const addThree = (x, y, z) => x + y + z;
const curriedAddThree = b.curry(addThree);
const addTen = b.curry(add)(10);
console.assert(curriedAdd(3)(4) === 7, 'Unexpected result from curried function');
console.assert(addThree(1, 2, 3) === 6, 'Unexpected result from addThree(1, 2, 3)');
console.assert(curriedAddThree(1)(2)(3) === 6, 'Unexpected result from curried function, curriedAddThree(1)(2)(3)');
console.assert(curriedAddThree(1, 2, 3) === 6, 'Unexpected result from curried function, curriedAddThree(1, 2, 3)');
console.assert(addTen(5) === 15, 'Unexpected result from curried function, addTen(5)');
// pipe
const addOne = (x) => x + 1;
const double = (x) => x * 2;
console.assert(b.pipe(addOne, double)(3) === 8, 'Unexpected result from pipe');
// compose
console.assert(b.compose(double, addOne)(3) === 8, 'Unexpected result from compose');
const addTwo = (x) => x + 2;
const half = (x) => x / 2;
const composed = b.compose(addTwo, half);
const piped = b.pipe(addTwo, half);
console.assert(composed(1) === 2.5, 'Unexpected result from compose, composed');
console.assert(piped(1) === 1.5, 'Unexpected result from pipe, piped');
// identity
console.assert(b.identity(5) === 5, 'Unexpected result from identity');
// match
const matchDigits = b.match()(/\d+/g);
console.assert(JSON.stringify(matchDigits('a1b2c3')) === JSON.stringify(['1', '2', '3']), 'Unexpected result from match');
// replace
const censor = b.replace()(/badword/g, '****');
console.assert(censor('This is a badword in a sentence.') === 'This is a **** in a sentence.', 'Unexpected result from replace');
// filter
const isEven = (x) => x % 2 === 0;
const filterEvens = b.filter()(isEven);
console.assert(JSON.stringify(filterEvens([1, 2, 3, 4])) === JSON.stringify([2, 4]), 'Unexpected result from filter');
// map
const mapDoubles = b.map()(double);
console.assert(JSON.stringify(mapDoubles([1, 2, 3])) === JSON.stringify([2, 4, 6]), 'Unexpected result from map');
// deepMap
const nestedArray = [1, [2, [3, 4]], [5, 6]];
const nestedArray2 = [1, [2, [3, 4]], [5, 6, [7, 8, [9, 10]]]];
console.assert(JSON.stringify(b.deepMap()(double, nestedArray)) === JSON.stringify([2, [4, [6, 8]], [10, 12]]), 'Unexpected result from deepMap, nestedArray');
console.assert(JSON.stringify(b.deepMap()(double, nestedArray2)) === JSON.stringify([2, [4, [6, 8]], [10, 12, [14, 16, [18, 20]]]]), 'Unexpected result from deepMap, nestedArray2');
const nestedArray3 = [1, [2, [3, 4]], [5, 6, [7, 8, [9, 10]]]];
const filterLessThanFive = (x) => x < 5;
const deepMapFilterLessThanFive = b.deepMap()(filterLessThanFive, nestedArray3);
console.assert(JSON.stringify(deepMapFilterLessThanFive) === JSON.stringify([true, [true, [true, true]], [false, false, [false, false, [false, false]]]]), 'Unexpected result from deepMap, nestedArray3');
|