No Description

invoke.js 832B

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