forked from angus-c/just
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
64 lines (52 loc) · 1.3 KB
/
Copy pathindex.js
File metadata and controls
64 lines (52 loc) · 1.3 KB
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
module.exports = has;
/*
const obj = {a: {aa: {aaa: 2}}, b: 4};
has(obj, 'a.aa.aaa'); // true
has(obj, ['a', 'aa', 'aaa']); // true
has(obj, 'b.bb.bbb'); // false
has(obj, ['b', 'bb', 'bbb']); // false
has(obj.a, 'aa.aaa'); // true
has(obj.a, ['aa', 'aaa']); // true
has(obj.b, 'bb.bbb'); // false
has(obj.b, ['bb', 'bbb']); // false
has(null, 'a'); // false
has(undefined, ['a']); // false
const obj = {a: {}};
const sym = Symbol();
obj.a[sym] = 4;
has(obj.a, sym); // true
*/
function has(obj, propsArg) {
if (!obj) {
return false;
}
var props, prop;
if (Array.isArray(propsArg)) {
props = propsArg.slice(0);
}
if (typeof propsArg == 'string') {
props = propsArg.split('.');
}
if (typeof propsArg == 'symbol') {
props = [propsArg];
}
if (!Array.isArray(props)) {
throw new Error('props arg must be an array, a string or a symbol');
}
while (props.length) {
prop = props.shift();
// if we are recursing, but met a nullish value, we cannot
// access it via .hasOwnProperty and should return negatively
if (obj == null) {
return false;
}
if (!Object.prototype.hasOwnProperty.call(obj, prop)) {
return false;
}
if (props.length === 0) {
return true;
}
obj = obj[prop];
}
return false;
}