You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

39 lines
1.1 KiB

4 years ago
  1. var castPath = require('./_castPath'),
  2. isArguments = require('./isArguments'),
  3. isArray = require('./isArray'),
  4. isIndex = require('./_isIndex'),
  5. isLength = require('./isLength'),
  6. toKey = require('./_toKey');
  7. /**
  8. * Checks if `path` exists on `object`.
  9. *
  10. * @private
  11. * @param {Object} object The object to query.
  12. * @param {Array|string} path The path to check.
  13. * @param {Function} hasFunc The function to check properties.
  14. * @returns {boolean} Returns `true` if `path` exists, else `false`.
  15. */
  16. function hasPath(object, path, hasFunc) {
  17. path = castPath(path, object);
  18. var index = -1,
  19. length = path.length,
  20. result = false;
  21. while (++index < length) {
  22. var key = toKey(path[index]);
  23. if (!(result = object != null && hasFunc(object, key))) {
  24. break;
  25. }
  26. object = object[key];
  27. }
  28. if (result || ++index != length) {
  29. return result;
  30. }
  31. length = object == null ? 0 : object.length;
  32. return !!length && isLength(length) && isIndex(key, length) &&
  33. (isArray(object) || isArguments(object));
  34. }
  35. module.exports = hasPath;