From 3a963905b27b49cc0180fb553a273449058373e3 Mon Sep 17 00:00:00 2001 From: photonstorm Date: Tue, 29 Nov 2016 13:00:48 +0000 Subject: [PATCH] Handy new utility class, now used by Config and Settings. --- v3/src/utils/GetObjectValue.js | 39 ++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 v3/src/utils/GetObjectValue.js diff --git a/v3/src/utils/GetObjectValue.js b/v3/src/utils/GetObjectValue.js new file mode 100644 index 000000000..fb1ea31ea --- /dev/null +++ b/v3/src/utils/GetObjectValue.js @@ -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;