1 |
|
2 | const assert = require('assert');
|
3 | const { map, filter, reduce } = require('../collections');
|
4 |
|
5 | const incrementBy = value => data => value + data;
|
6 | const identity = x => x;
|
7 | const greaterThan = x => data => data > x;
|
8 | const get = key => data => data[key];
|
9 |
|
10 | describe('Collections', function() {
|
11 | describe('#reduce()', function() {
|
12 | it('should return the sumation of its keys', function() {
|
13 | let a = { a: 1, b: 2 };
|
14 | let b = [1, 2];
|
15 | let sum = (a, b) => a + b;
|
16 |
|
17 | assert.strictEqual(reduce(sum, 0, a), 3);
|
18 | assert.strictEqual(reduce(sum, 0)(b), 3);
|
19 | });
|
20 | });
|
21 | describe('#map()', function() {
|
22 | it('should return same object given identity', function() {
|
23 | let a = { a: 1, b: 2 };
|
24 | let b = [1, 2];
|
25 | assert.deepStrictEqual(map(identity, a), a);
|
26 | assert.deepStrictEqual(map(identity, b), b);
|
27 | });
|
28 | it('should return incrementBy(1) all properties', function() {
|
29 | let a = { a: 1, b: 2 };
|
30 | let b = [1, 2];
|
31 | assert.deepStrictEqual(map(incrementBy(1), a), { a: 2, b: 3 });
|
32 | assert.deepStrictEqual(map(incrementBy(1), b), [2, 3]);
|
33 | });
|
34 | it('should return the first level', function() {
|
35 | let a = { a: { b: 1 }, b: { b: 2 } };
|
36 | let b = [[1], [2]];
|
37 | assert.deepStrictEqual(map(get('b'), a), { a: 1, b: 2 });
|
38 | assert.deepStrictEqual(map(get(0), b), [1, 2]);
|
39 | });
|
40 | it('should return empty object if empty object', function() {
|
41 | let a = {};
|
42 | let b = [];
|
43 | assert.deepStrictEqual(map(incrementBy(1), a), {});
|
44 | assert.deepStrictEqual(map(incrementBy(1), b), []);
|
45 | });
|
46 | });
|
47 | describe('#filter()', function() {
|
48 | it('should return same object given identity', function() {
|
49 | let a = { a: 1, b: 2 };
|
50 | let b = [1, 2];
|
51 | assert.deepStrictEqual(filter(identity, a), a);
|
52 | assert.deepStrictEqual(filter(identity, b), b);
|
53 | });
|
54 | it('should return only properties greaterThan(1)', function() {
|
55 | let a = { a: 1, b: 2 };
|
56 | let b = [1, 2];
|
57 | assert.deepStrictEqual(filter(greaterThan(1), a), { b: 2 });
|
58 | assert.deepStrictEqual(filter(greaterThan(1), b), [2]);
|
59 | });
|
60 | it('should return only properties greaterThan(0)', function() {
|
61 | let a = { a: 1, b: 2 };
|
62 | let b = [1, 2];
|
63 | assert.deepStrictEqual(filter(greaterThan(0), a), a);
|
64 | assert.deepStrictEqual(filter(greaterThan(0), b), b);
|
65 | });
|
66 | it('should return empty object if no match', function() {
|
67 | let a = { a: 1, b: 2 };
|
68 | let b = [1, 2];
|
69 | assert.deepStrictEqual(filter(greaterThan(5), a), {});
|
70 | assert.deepStrictEqual(filter(greaterThan(5), b), []);
|
71 | });
|
72 | });
|
73 | });
|