Ensure _.method aligns with _.invoke.

This commit is contained in:
jdalton
2015-04-07 00:18:17 -07:00
parent cc0b8e9d10
commit a129a2509f
2 changed files with 8 additions and 11 deletions

View File

@@ -2621,16 +2621,13 @@
}
function baseMethod(object, path, args) {
if (object == null) {
return undefined;
}
if (!(isKey(path) || (path in toObject(object)))) {
if (!isKey(path, object)) {
path = toPath(path);
object = baseGet(object, baseSlice(path, 0, -1));
object = path.length == 1 ? object : baseGet(object, baseSlice(path, 0, -1));
path = last(path);
}
var func = object[path];
return isFunction(func) ? func.apply(object, args) : undefined;
var func = object == null ? object : object[path];
return func == null ? undefined : func.apply(object, args);
}
/**
@@ -6755,8 +6752,8 @@
result = isLength(length) ? Array(length) : [];
baseEach(collection, function(value) {
var func = isFunc ? methodName : (value != null && value[methodName]);
result[++index] = func ? func.apply(value, args) : undefined;
var func = isFunc ? methodName : (value == null ? value : value[methodName]);
result[++index] = func == null ? undefined : func.apply(value, args);
});
return result;
});
@@ -11251,7 +11248,7 @@
var methodOf = restParam(function(object, args) {
return function(path) {
return object == null ? undefined : baseMethod(object, path, args);
return baseMethod(object, path, args);
};
});

View File

@@ -6882,7 +6882,7 @@
});
test('should not error on elements with missing properties', 1, function() {
var objects = _.map(falsey.concat(_.constant(1)), function(value) {
var objects = _.map([null, undefined, _.constant(1)], function(value) {
return { 'a': value };
});