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.

47 lines
1.1 KiB

4 years ago
  1. import _curry2 from "./internal/_curry2.js";
  2. import _has from "./internal/_has.js";
  3. import isNil from "./isNil.js";
  4. /**
  5. * Returns whether or not a path exists in an object. Only the object's
  6. * own properties are checked.
  7. *
  8. * @func
  9. * @memberOf R
  10. * @since v0.26.0
  11. * @category Object
  12. * @typedefn Idx = String | Int
  13. * @sig [Idx] -> {a} -> Boolean
  14. * @param {Array} path The path to use.
  15. * @param {Object} obj The object to check the path in.
  16. * @return {Boolean} Whether the path exists.
  17. * @see R.has
  18. * @example
  19. *
  20. * R.hasPath(['a', 'b'], {a: {b: 2}}); // => true
  21. * R.hasPath(['a', 'b'], {a: {b: undefined}}); // => true
  22. * R.hasPath(['a', 'b'], {a: {c: 2}}); // => false
  23. * R.hasPath(['a', 'b'], {}); // => false
  24. */
  25. var hasPath =
  26. /*#__PURE__*/
  27. _curry2(function hasPath(_path, obj) {
  28. if (_path.length === 0 || isNil(obj)) {
  29. return false;
  30. }
  31. var val = obj;
  32. var idx = 0;
  33. while (idx < _path.length) {
  34. if (!isNil(val) && _has(_path[idx], val)) {
  35. val = val[_path[idx]];
  36. idx += 1;
  37. } else {
  38. return false;
  39. }
  40. }
  41. return true;
  42. });
  43. export default hasPath;