baseGet.js 750 B

1234567891011121314151617181920212223242526272829
  1. var toObject = require('./toObject');
  2. /**
  3. * The base implementation of `get` without support for string paths
  4. * and default values.
  5. *
  6. * @private
  7. * @param {Object} object The object to query.
  8. * @param {Array} path The path of the property to get.
  9. * @param {string} [pathKey] The key representation of path.
  10. * @returns {*} Returns the resolved value.
  11. */
  12. function baseGet(object, path, pathKey) {
  13. if (object == null) {
  14. return;
  15. }
  16. if (pathKey !== undefined && pathKey in toObject(object)) {
  17. path = [pathKey];
  18. }
  19. var index = 0,
  20. length = path.length;
  21. while (object != null && index < length) {
  22. object = object[path[index++]];
  23. }
  24. return (index && index == length) ? object : undefined;
  25. }
  26. module.exports = baseGet;