/**
* Gracefully handles fallback configuration values for sandbox usage.
*
* @param {object} config - The configuration object containing values.
* @param {object} defaults - An object containing default values.
* @returns {object} - The configured object with fallback values applied.
*/
function applySandboxConfig(config, defaults) {
const configuredConfig = { ...defaults }; // Start with defaults
for (const key in config) {
if (config.hasOwnProperty(key)) {
const value = config[key];
if (typeof value === 'undefined' || value === null) {
// Fallback to default if value is undefined or null
configuredConfig[key] = defaults[key];
console.warn(`Sandbox: Value for "${key}" is missing. Using default: ${defaults[key]}`);
} else {
configuredConfig[key] = value;
}
}
}
return configuredConfig;
}
// Example Usage (can be removed or adapted)
// const sandboxConfig = {
// apiKey: undefined,
// timeout: null,
// logLevel: 'error'
// };
// const defaultSandboxConfig = {
// apiKey: 'sandbox-key',
// timeout: 1000,
// logLevel: 'info'
// };
// const configuredConfig = applySandboxConfig(sandboxConfig, defaultSandboxConfig);
// console.log(configuredConfig);
Add your comment