forked from ramda/ramda
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbind.js
86 lines (73 loc) · 1.92 KB
/
bind.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
74
75
76
77
78
79
80
81
82
83
84
85
86
var R = require('../source/index.js');
var eq = require('./shared/eq.js');
describe('bind', function() {
function Foo(x) {
this.x = x;
}
function add(x) {
return this.x + x;
}
function Bar(x, y) {
this.x = x;
this.y = y;
}
Bar.prototype = new Foo();
Bar.prototype.getX = function() {
return 'prototype getX';
};
it('returns a function', function() {
eq(typeof R.bind(add, Foo), 'function');
});
it('returns a function bound to the specified context object', function() {
var f = new Foo(12);
function isFoo() {
return this instanceof Foo;
}
var isFooBound = R.bind(isFoo, f);
eq(isFoo(), false);
eq(isFooBound(), true);
});
it('works with built-in types', function() {
var abc = R.bind(String.prototype.toLowerCase, 'ABCDEFG');
eq(typeof abc, 'function');
eq(abc(), 'abcdefg');
});
it('works with user-defined types', function() {
var f = new Foo(12);
function getX() {
return this.x;
}
var getXFooBound = R.bind(getX, f);
eq(getXFooBound(), 12);
});
it('works with plain objects', function() {
var pojso = {
x: 100
};
function incThis() {
return this.x + 1;
}
var incPojso = R.bind(incThis, pojso);
eq(typeof incPojso, 'function');
eq(incPojso(), 101);
});
it('does not interfere with existing object methods', function() {
var b = new Bar('a', 'b');
function getX() {
return this.x;
}
var getXBarBound = R.bind(getX, b);
eq(b.getX(), 'prototype getX');
eq(getXBarBound(), 'a');
});
it('preserves arity', function() {
var f0 = function() { return 0; };
var f1 = function(a) { return a; };
var f2 = function(a, b) { return a + b; };
var f3 = function(a, b, c) { return a + b + c; };
eq(R.bind(f0, {}).length, 0);
eq(R.bind(f1, {}).length, 1);
eq(R.bind(f2, {}).length, 2);
eq(R.bind(f3, {}).length, 3);
});
});