UNPKG

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