result.js 764 B

123456789101112131415161718192021222324
  1. var isFunction = require('./isFunction.js');
  2. var _toPath = require('./_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. 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. }
  22. module.exports = result;