forked from lodash/lodash
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbindKey.js
More file actions
66 lines (53 loc) · 1.76 KB
/
bindKey.js
File metadata and controls
66 lines (53 loc) · 1.76 KB
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
import assert from 'assert';
import { slice } from './utils.js';
import bindKey from '../bindKey.js';
describe('bindKey', function() {
it('should work when the target function is overwritten', function() {
var object = {
'user': 'fred',
'greet': function(greeting) {
return this.user + ' says: ' + greeting;
}
};
var bound = bindKey(object, 'greet', 'hi');
assert.strictEqual(bound(), 'fred says: hi');
object.greet = function(greeting) {
return this.user + ' says: ' + greeting + '!';
};
assert.strictEqual(bound(), 'fred says: hi!');
});
it('should support placeholders', function() {
var object = {
'fn': function() {
return slice.call(arguments);
}
};
var ph = bindKey.placeholder,
bound = bindKey(object, 'fn', ph, 'b', ph);
assert.deepStrictEqual(bound('a', 'c'), ['a', 'b', 'c']);
assert.deepStrictEqual(bound('a'), ['a', 'b', undefined]);
assert.deepStrictEqual(bound('a', 'c', 'd'), ['a', 'b', 'c', 'd']);
assert.deepStrictEqual(bound(), [undefined, 'b', undefined]);
});
it('should use `_.placeholder` when set', function() {
var object = {
'fn': function() {
return slice.call(arguments);
}
};
var _ph = _.placeholder = {},
ph = bindKey.placeholder,
bound = bindKey(object, 'fn', _ph, 'b', ph);
assert.deepEqual(bound('a', 'c'), ['a', 'b', ph, 'c']);
delete _.placeholder;
});
it('should ensure `new bound` is an instance of `object[key]`', function() {
function Foo(value) {
return value && object;
}
var object = { 'Foo': Foo },
bound = bindKey(object, 'Foo');
assert.ok(new bound instanceof Foo);
assert.strictEqual(new bound(true), object);
});
});