1 | const assert = require('assert');
|
2 | const {map,filter} = require('../collections');
|
3 |
|
4 | const incrementBy = value => data => value + data;
|
5 | const identity = x => x;
|
6 | const greaterThan = x => data => data > x;
|
7 | const get = key => data => data[key];
|
8 |
|
9 | describe('Collections', function() {
|
10 | describe('#map()', function() {
|
11 | it('should return same object given identity', function(){
|
12 | let a = {a:1, b:2};
|
13 | assert.deepEqual(a.map(identity),{a:1,b:2});
|
14 | assert.deepEqual(map(identity,a),{a:1,b:2});
|
15 | });
|
16 | it('should return incrementBy(1) all properties', function(){
|
17 | let a = {a:1, b:2};
|
18 | assert.deepEqual(a.map(incrementBy(1)),{a:2,b:3});
|
19 | assert.deepEqual(map(incrementBy(1),a),{a:2,b:3});
|
20 | });
|
21 | it('should return the first level', function(){
|
22 | let a = {a:{b:1},b:{b:2}};
|
23 | assert.deepEqual(a.map(get('b')),{a:1,b:2});
|
24 | assert.deepEqual(map(get('b'),a),{a:1,b:2});
|
25 | });
|
26 | it('should return empty object if empty object', function(){
|
27 | let a = {};
|
28 | assert.deepEqual(a.map(incrementBy(1)),{});
|
29 | assert.deepEqual(map(incrementBy(1),a),{});
|
30 | });
|
31 | });
|
32 | describe('#filter()', function() {
|
33 | it('should return same object given identity', function(){
|
34 | let a = {a:1, b:2};
|
35 | assert.deepEqual(a.filter(identity),{a:1,b:2});
|
36 | assert.deepEqual(filter(identity,a),{a:1,b:2});
|
37 | });
|
38 | it('should return only properties greaterThan(1)', function(){
|
39 | let a = {a:1, b:2};
|
40 | assert.deepEqual(a.filter(greaterThan(1)),{b:2});
|
41 | assert.deepEqual(filter(greaterThan(1),a),{b:2});
|
42 | });
|
43 | it('should return only properties greaterThan(0)', function(){
|
44 | let a = {a:1, b:2};
|
45 | assert.deepEqual(a.filter(greaterThan(0)),{a:1,b:2});
|
46 | assert.deepEqual(filter(greaterThan(0),a),{a:1,b:2});
|
47 | });
|
48 | it('should return empty object if no match', function(){
|
49 | let a = {a:1, b:2};
|
50 | assert.deepEqual(a.filter(greaterThan(5)),{});
|
51 | assert.deepEqual(filter(greaterThan(5),a),{});
|
52 | });
|
53 | });
|
54 | });
|