Add _.method and _.methodOf.

This commit is contained in:
Justin Ridgewell
2015-03-29 00:28:01 -04:00
committed by jdalton
parent fe1e27fd40
commit d7a4524e70
2 changed files with 150 additions and 1 deletions

View File

@@ -2614,6 +2614,19 @@
}
}
function baseMethod(object, path, args) {
if (object == null) {
return undefined;
}
if (!(isKey(path) || (path in toObject(object)))) {
path = toPath(path);
object = baseGet(object, baseSlice(path, 0, -1));
path = last(path);
}
var func = object[path];
return isFunction(func) ? func.apply(object, args) : undefined;
}
/**
* The base implementation of `_.property` without support for deep paths.
*
@@ -11227,6 +11240,21 @@
return baseMatchesProperty(path, baseClone(value, true));
}
var method = restParam(function(path, args) {
return function(object) {
return baseMethod(object, path, args);
}
});
var methodOf = restParam(function(object, args) {
if (object == null) {
return constant(undefined);
}
return function(path) {
return baseMethod(object, path, args);
};
});
/**
* Adds all own enumerable function properties of a source object to the
* destination object. If `object` is a function then methods are added to
@@ -11784,6 +11812,8 @@
lodash.matchesProperty = matchesProperty;
lodash.memoize = memoize;
lodash.merge = merge;
lodash.method = method;
lodash.methodOf = methodOf;
lodash.mixin = mixin;
lodash.negate = negate;
lodash.omit = omit;