forked from ramda/ramda
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmap.js
73 lines (58 loc) · 2 KB
/
map.js
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
63
64
65
66
67
68
69
70
71
72
73
var listXf = require('./helpers/listXf.js');
var R = require('../source/index.js');
var assert = require('assert');
var eq = require('./shared/eq.js');
var Id = require('./shared/Id.js');
describe('map', function() {
var times2 = function(x) {return x * 2;};
var add1 = function(x) {return x + 1;};
var dec = function(x) { return x - 1; };
it('maps simple functions over arrays', function() {
eq(R.map(times2, [1, 2, 3, 4]), [2, 4, 6, 8]);
});
it('maps over objects', function() {
eq(R.map(dec, {}), {});
eq(R.map(dec, {x: 4, y: 5, z: 6}), {x: 3, y: 4, z: 5});
});
it('interprets ((->) r) as a functor', function() {
var f = function(a) { return a - 1; };
var g = function(b) { return b * 2; };
var h = R.map(f, g);
eq(h(10), (10 * 2) - 1);
});
it('dispatches to objects that implement `map`', function() {
var obj = {x: 100, map: function(f) { return f(this.x); }};
eq(R.map(add1, obj), 101);
});
it('dispatches to transformer objects', function() {
eq(R.map(add1, listXf), {
f: add1,
xf: listXf
});
});
it('throws a TypeError on null and undefined', function() {
assert.throws(function() { return R.map(times2, null); }, TypeError);
assert.throws(function() { return R.map(times2, undefined); }, TypeError);
});
it('composes', function() {
var mdouble = R.map(times2);
var mdec = R.map(dec);
eq(mdec(mdouble([10, 20, 30])), [19, 39, 59]);
});
it('can compose transducer-style', function() {
var mdouble = R.map(times2);
var mdec = R.map(dec);
var xcomp = mdec(mdouble(listXf));
eq(xcomp.xf, {xf: listXf, f: times2});
eq(xcomp.f, dec);
});
it('correctly uses fantasy-land implementations', function() {
var m1 = Id(1);
var m2 = R.map(R.add(1), m1);
eq(m1.value + 1, m2.value);
});
it('can act as a transducer', function() {
eq(R.into([], R.map(times2), [1, 2, 3, 4]), [2, 4, 6, 8]);
eq(R.transduce(R.map(times2), R.flip(R.append), [], [1, 2, 3, 4]), [2, 4, 6, 8]);
});
});