1. /**
  2. * Gracefully handles fallback configuration values for sandbox usage.
  3. *
  4. * @param {object} config - The configuration object containing values.
  5. * @param {object} defaults - An object containing default values.
  6. * @returns {object} - The configured object with fallback values applied.
  7. */
  8. function applySandboxConfig(config, defaults) {
  9. const configuredConfig = { ...defaults }; // Start with defaults
  10. for (const key in config) {
  11. if (config.hasOwnProperty(key)) {
  12. const value = config[key];
  13. if (typeof value === 'undefined' || value === null) {
  14. // Fallback to default if value is undefined or null
  15. configuredConfig[key] = defaults[key];
  16. console.warn(`Sandbox: Value for "${key}" is missing. Using default: ${defaults[key]}`);
  17. } else {
  18. configuredConfig[key] = value;
  19. }
  20. }
  21. }
  22. return configuredConfig;
  23. }
  24. // Example Usage (can be removed or adapted)
  25. // const sandboxConfig = {
  26. // apiKey: undefined,
  27. // timeout: null,
  28. // logLevel: 'error'
  29. // };
  30. // const defaultSandboxConfig = {
  31. // apiKey: 'sandbox-key',
  32. // timeout: 1000,
  33. // logLevel: 'info'
  34. // };
  35. // const configuredConfig = applySandboxConfig(sandboxConfig, defaultSandboxConfig);
  36. // console.log(configuredConfig);

Add your comment