Handy new utility class, now used by Config and Settings.

This commit is contained in:
photonstorm 2016-11-29 13:00:48 +00:00
parent 1db02f3d3a
commit 3a963905b2

View file

@ -0,0 +1,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;