invoke.js 852 B

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