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