Ensure _.callback doesn't error if provided a nullish func argument and a thisArg.

This commit is contained in:
John-David Dalton
2014-05-01 01:12:40 -07:00
parent d07e53e9a0
commit 1a03ee8b9b
2 changed files with 13 additions and 1 deletions

View File

@@ -7790,7 +7790,7 @@
function createCallback(func, thisArg, argCount) {
var type = typeof func;
if (type == 'function' || func == null) {
return (typeof thisArg == 'undefined' || !('prototype' in func)) &&
return (typeof thisArg == 'undefined' || !(func && 'prototype' in func)) &&
func || baseCreateCallback(func, thisArg, argCount);
}
// handle "_.pluck" and "_.where" style callback shorthands

View File

@@ -1663,6 +1663,18 @@
});
});
test('should not error when `func` is nullish and a `thisArg` is provided', 2, function() {
var object = {};
_.each([null, undefined], function(value) {
try {
var callback = _.callback(value, {});
strictEqual(callback(object), object);
} catch(e) {
ok(false);
}
});
});
test('should return a callback created by `_.matches` when `func` is an object', 2, function() {
var callback = _.callback({ 'a': 1 });
strictEqual(callback({ 'a': 1, 'b': 2 }), true);