Reduce code around _.bind and _.partial, and add _.lateBind.

Former-commit-id: 4c962d066ecfa54882cee2216a7310ab34b3b5a3
This commit is contained in:
John-David Dalton
2012-09-13 00:04:00 -07:00
parent 569caa0bf2
commit c0d7dbf639
4 changed files with 36 additions and 22 deletions

View File

@@ -134,6 +134,7 @@
'debounce',
'defer',
'delay',
'lateBind',
'memoize',
'once',
'partial',
@@ -241,6 +242,7 @@
'forIn',
'forOwn',
'invert',
'lateBind',
'merge',
'object',
'omit',
@@ -370,14 +372,16 @@
else if (functionsMethods.indexOf(methodName) > -1) {
if (methodName == 'after') {
func(1, noop);
} else if (methodName == 'bindAll') {
func({ 'noop': noop });
} else if (methodName == 'lateBind') {
func(lodash, 'identity', array, string);
} else if (/^(?:bind|partial)$/.test(methodName)) {
func(noop, object, array, string);
} else if (/^(?:compose|memoize|wrap)$/.test(methodName)) {
func(noop, noop);
} else if (/^(?:debounce|throttle)$/.test(methodName)) {
func(noop, 100);
} else if (methodName == 'bindAll') {
func({ 'noop': noop });
} else {
func(noop);
}

View File

@@ -167,23 +167,6 @@
bound(['b'], 'c');
deepEqual(args, ['a', ['b'], 'c']);
});
test('supports lazy bind', function() {
var object = {
'name': 'moe',
'greet': function(greeting) {
return greeting + ': ' + this.name;
}
};
var func = _.bind(object, 'greet', 'hi');
equal(func(), 'hi: moe');
object.greet = function(greeting) {
return greeting + ' ' + this.name + '!';
};
equal(func(), 'hi moe!');
});
}());
/*--------------------------------------------------------------------------*/
@@ -914,6 +897,29 @@
/*--------------------------------------------------------------------------*/
QUnit.module('lodash.lateBind');
(function() {
test('should work when the target function is overwritten', function() {
var object = {
'name': 'moe',
'greet': function(greeting) {
return greeting + ': ' + this.name;
}
};
var func = _.lateBind(object, 'greet', 'hi');
equal(func(), 'hi: moe');
object.greet = function(greeting) {
return greeting + ' ' + this.name + '!';
};
equal(func(), 'hi moe!');
});
}());
/*--------------------------------------------------------------------------*/
QUnit.module('lodash.merge');
(function() {