Skip to content

Commit 3a96390

Browse files
committed
Handy new utility class, now used by Config and Settings.
1 parent 1db02f3 commit 3a96390

1 file changed

Lines changed: 39 additions & 0 deletions

File tree

v3/src/utils/GetObjectValue.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Source object
2+
// The key as a string, or an array of keys, i.e. 'banner', or 'banner.hideBanner'
3+
// The default value to use if the key doesn't exist
4+
5+
var GetObjectValue = function (source, key, defaultValue)
6+
{
7+
if (key.indexOf('.'))
8+
{
9+
keys = key.split('.');
10+
11+
var parent = source;
12+
var value = defaultValue;
13+
14+
// Use for loop here so we can break early
15+
for (var i = 0; i < keys.length; i++)
16+
{
17+
if (parent.hasOwnProperty(keys[i]))
18+
{
19+
// Yes it has a key property, let's carry on down
20+
value = parent[keys[i]];
21+
22+
parent = parent[keys[i]];
23+
}
24+
else
25+
{
26+
break;
27+
}
28+
}
29+
30+
return value;
31+
}
32+
else
33+
{
34+
return (source.hasOwnProperty(key) ? source[key] : defaultValue);
35+
}
36+
37+
}
38+
39+
module.exports = GetObjectValue;

0 commit comments

Comments
 (0)