invokePath.js 852 B

1234567891011121314151617181920212223242526
  1. var baseGet = require('./baseGet'),
  2. baseSlice = require('./baseSlice'),
  3. isKey = require('./isKey'),
  4. last = require('../array/last'),
  5. toPath = require('./toPath');
  6. /**
  7. * Invokes the method at `path` on `object`.
  8. *
  9. * @private
  10. * @param {Object} object The object to query.
  11. * @param {Array|string} path The path of the method to invoke.
  12. * @param {Array} args The arguments to invoke the method with.
  13. * @returns {*} Returns the result of the invoked method.
  14. */
  15. function invokePath(object, path, args) {
  16. if (object != null && !isKey(path, object)) {
  17. path = toPath(path);
  18. object = path.length == 1 ? object : baseGet(object, baseSlice(path, 0, -1));
  19. path = last(path);
  20. }
  21. var func = object == null ? object : object[path];
  22. return func == null ? undefined : func.apply(object, args);
  23. }
  24. module.exports = invokePath;