method.js 907 B

123456789101112131415161718192021222324252627282930313233
  1. var invokePath = require('../internal/invokePath'),
  2. restParam = require('../function/restParam');
  3. /**
  4. * Creates a function that invokes the method at `path` on a given object.
  5. * Any additional arguments are provided to the invoked method.
  6. *
  7. * @static
  8. * @memberOf _
  9. * @category Utility
  10. * @param {Array|string} path The path of the method to invoke.
  11. * @param {...*} [args] The arguments to invoke the method with.
  12. * @returns {Function} Returns the new function.
  13. * @example
  14. *
  15. * var objects = [
  16. * { 'a': { 'b': { 'c': _.constant(2) } } },
  17. * { 'a': { 'b': { 'c': _.constant(1) } } }
  18. * ];
  19. *
  20. * _.map(objects, _.method('a.b.c'));
  21. * // => [2, 1]
  22. *
  23. * _.invoke(_.sortBy(objects, _.method(['a', 'b', 'c'])), 'a.b.c');
  24. * // => [1, 2]
  25. */
  26. var method = restParam(function(path, args) {
  27. return function(object) {
  28. return invokePath(object, path, args);
  29. };
  30. });
  31. module.exports = method;