mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-08 10:17:48 +00:00
Ensure _.callback doesn't error if provided a nullish func argument and a thisArg.
This commit is contained in:
@@ -7790,7 +7790,7 @@
|
|||||||
function createCallback(func, thisArg, argCount) {
|
function createCallback(func, thisArg, argCount) {
|
||||||
var type = typeof func;
|
var type = typeof func;
|
||||||
if (type == 'function' || func == null) {
|
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);
|
func || baseCreateCallback(func, thisArg, argCount);
|
||||||
}
|
}
|
||||||
// handle "_.pluck" and "_.where" style callback shorthands
|
// handle "_.pluck" and "_.where" style callback shorthands
|
||||||
|
|||||||
12
test/test.js
12
test/test.js
@@ -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() {
|
test('should return a callback created by `_.matches` when `func` is an object', 2, function() {
|
||||||
var callback = _.callback({ 'a': 1 });
|
var callback = _.callback({ 'a': 1 });
|
||||||
strictEqual(callback({ 'a': 1, 'b': 2 }), true);
|
strictEqual(callback({ 'a': 1, 'b': 2 }), true);
|
||||||
|
|||||||
Reference in New Issue
Block a user