Repositorio del curso CCOM4030 el semestre B91 del proyecto Artesanías con el Instituto de Cultura

invoke.js 868B

1234567891011121314151617181920212223242526272829
  1. var isFunction = require('./isFunction.js');
  2. var isArray = require('./isArray.js');
  3. var _deepGet = require('./_deepGet.js');
  4. var restArguments = require('./restArguments.js');
  5. var map = require('./map.js');
  6. // Invoke a method (with arguments) on every item in a collection.
  7. var invoke = restArguments(function(obj, path, args) {
  8. var contextPath, func;
  9. if (isFunction(path)) {
  10. func = path;
  11. } else if (isArray(path)) {
  12. contextPath = path.slice(0, -1);
  13. path = path[path.length - 1];
  14. }
  15. return map(obj, function(context) {
  16. var method = func;
  17. if (!method) {
  18. if (contextPath && contextPath.length) {
  19. context = _deepGet(context, contextPath);
  20. }
  21. if (context == null) return void 0;
  22. method = context[path];
  23. }
  24. return method == null ? method : method.apply(context, args);
  25. });
  26. });
  27. module.exports = invoke;