or run

tessl search
Log in

Version

Files

tile.json

task.mdevals/scenario-5/

Configuration Merger with Array Preservation

Build a configuration merger utility that combines multiple configuration objects while properly handling array values. The merger should deeply merge nested configuration objects but preserve array values without merging them.

Requirements

Create a module that exports a mergeConfigs function that:

  1. Accepts multiple configuration objects as arguments
  2. Returns a new merged configuration object without modifying the input objects
  3. Deeply merges nested object properties from all configurations
  4. Preserves array values as complete units (does not merge array elements)
  5. Later configurations override earlier ones for conflicting properties

Implementation Details

The function should handle:

  • Multiple levels of nested configuration objects
  • Array properties at any nesting level (should replace, not merge)
  • Multiple configuration sources with the rightmost taking precedence
  • Empty or undefined configuration objects

Test Cases

Test Case 1: Basic array replacement @test

const config1 = {
  servers: ['server1.com', 'server2.com'],
  port: 8080
};
const config2 = {
  servers: ['server3.com'],
  timeout: 5000
};
const result = mergeConfigs(config1, config2);
// Expected:
// {
//   servers: ['server3.com'],  // Replaced, not merged
//   port: 8080,
//   timeout: 5000
// }

Test Case 2: Nested arrays in deep objects @test

const config1 = {
  database: {
    hosts: ['db1.com', 'db2.com'],
    options: { poolSize: 10 }
  }
};
const config2 = {
  database: {
    hosts: ['db3.com'],
    options: { timeout: 3000 }
  }
};
const result = mergeConfigs(config1, config2);
// Expected:
// {
//   database: {
//     hosts: ['db3.com'],  // Array replaced
//     options: { poolSize: 10, timeout: 3000 }  // Objects merged
//   }
// }

Test Case 3: Complex nested arrays @test

const config1 = {
  features: {
    enabled: ['feature1', 'feature2'],
    settings: { mode: 'auto' }
  }
};
const config2 = {
  features: {
    enabled: ['feature3'],
    settings: { level: 5 }
  }
};
const config3 = {
  features: {
    settings: { mode: 'manual' }
  }
};
const result = mergeConfigs(config1, config2, config3);
// Expected:
// {
//   features: {
//     enabled: ['feature3'],  // From config2 (config3 doesn't override)
//     settings: { mode: 'manual', level: 5 }  // Deeply merged
//   }
// }

File Structure

src/
  merger.js          # Main implementation
  merger.test.js     # Test file with the test cases above

Dependencies { .dependencies }

mixin-deep { .dependency }

Provides deep object merging capabilities.