forked from cliftonc/calipso
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUtils.js
58 lines (55 loc) · 1.32 KB
/
Utils.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/**
* General utility methods
*/
exports = module.exports = {
/**
* Basically like getProperty, different return
* @method hasProperty
* @param ns {string} A period delimited string of the namespace to find, sans root object
* @param obj {object} The root object to search
* @return {boolean} true if property exists, false otherwise
*/
hasProperty: function (ns, obj) {
if (!ns) {
return obj;
}
var nsArray = ns.split('.'),
nsLen = nsArray.length,
newNs;
// if nsLen === 0, then obj is just returned
while (nsLen > 0) {
newNs = nsArray.shift();
if (obj[newNs]) {
obj = obj[newNs];
} else {
return false;
}
nsLen = nsArray.length;
}
return true;
},
/**
* Find a namespaced property
* @method getProperty
* @param ns {string} A period delimited string of the namespace to find, sans root object
* @param obj {object} The root object to search
* @return {object} the object, either the namespaced obejct or the root object
*/
getProperty: function (ns, obj) {
if (!ns) {
return obj;
}
var nsArray = ns.split('.'),
nsLen = nsArray.length,
newNs;
// if nsLen === 0, then obj is just returned
while (nsLen > 0) {
newNs = nsArray.shift();
if (obj[newNs]) {
obj = obj[newNs];
}
nsLen = nsArray.length;
}
return obj;
}
}