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