forked from phaserjs/phaser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGetObjectValue.js
More file actions
39 lines (32 loc) · 919 Bytes
/
Copy pathGetObjectValue.js
File metadata and controls
39 lines (32 loc) · 919 Bytes
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
// Source object
// The key as a string, or an array of keys, i.e. 'banner', or 'banner.hideBanner'
// The default value to use if the key doesn't exist
var GetObjectValue = function (source, key, defaultValue)
{
if (key.indexOf('.'))
{
keys = key.split('.');
var parent = source;
var value = defaultValue;
// Use for loop here so we can break early
for (var i = 0; i < keys.length; i++)
{
if (parent.hasOwnProperty(keys[i]))
{
// Yes it has a key property, let's carry on down
value = parent[keys[i]];
parent = parent[keys[i]];
}
else
{
break;
}
}
return value;
}
else
{
return (source.hasOwnProperty(key) ? source[key] : defaultValue);
}
}
module.exports = GetObjectValue;