result.js 745 B

12345678910111213141516171819202122
  1. import isFunction from './isFunction.js';
  2. import toPath from './_toPath.js';
  3. // Traverses the children of `obj` along `path`. If a child is a function, it
  4. // is invoked with its parent as context. Returns the value of the final
  5. // child, or `fallback` if any child is undefined.
  6. export default function result(obj, path, fallback) {
  7. path = toPath(path);
  8. var length = path.length;
  9. if (!length) {
  10. return isFunction(fallback) ? fallback.call(obj) : fallback;
  11. }
  12. for (var i = 0; i < length; i++) {
  13. var prop = obj == null ? void 0 : obj[path[i]];
  14. if (prop === void 0) {
  15. prop = fallback;
  16. i = length; // Ensure we don't continue iterating.
  17. }
  18. obj = isFunction(prop) ? prop.call(obj) : prop;
  19. }
  20. return obj;
  21. }