merge-config.ts

Overview

The merge-config.ts file provides a utility function mergeConfigs designed to merge two partial configuration objects of type FullConfiguration. It ensures a non-destructive merge, meaning the original configuration objects are not mutated. This file is part of the configuration management aspect of the system, facilitating flexible composition and extension of configuration settings by merging user-provided or default configurations safely.

The main functionality centers on combining two configuration objects with special handling for the use and fallback properties, which may require concatenation or deep merging rather than simple overwriting.


Detailed Explanation

Imports


Function: mergeConfigs

export const mergeConfigs = (
  a: Partial<FullConfiguration>,
  b?: Partial<FullConfiguration>
): Partial<FullConfiguration>

Purpose

Merges two partial configuration objects into one, handling specific merging logic for nested or array properties like use and fallback.

Parameters

Returns

Behavior

  1. Calls mergeObjects(a, b) to create a new object v that combines properties from both a and b. This avoids mutating the original inputs.

  2. If b is provided:

    • It extracts the use and fallback properties from both a and b.

    • If both a and b have a use array, the function concatenates them and assigns the result to v.use.

    • If both a and b have a fallback object, it merges them via mergeObjects and assigns the result to v.fallback.

  3. Returns the merged configuration object v.

Usage Example

import { mergeConfigs } from './merge-config'
import type { FullConfiguration } from '../types'

const configA: Partial<FullConfiguration> = {
  use: ['pluginA'],
  fallback: { key1: 'value1' },
  setting1: true
}

const configB: Partial<FullConfiguration> = {
  use: ['pluginB'],
  fallback: { key2: 'value2' },
  setting2: false
}

const mergedConfig = mergeConfigs(configA, configB)

/*
mergedConfig = {
  use: ['pluginA', 'pluginB'],
  fallback: { key1: 'value1', key2: 'value2' },
  setting1: true,
  setting2: false
}
*/

Important Implementation Details


Interaction with Other Parts of the System


Mermaid Diagram: Flowchart of mergeConfigs Function

flowchart TD
    A[Start: mergeConfigs(a, b?)] --> B{Is b provided?}
    B -- No --> C[Return mergeObjects(a, undefined) as v]
    B -- Yes --> D[Call mergeObjects(a, b) to create v]
    D --> E[Extract use from a (u1) and b (u2)]
    E --> F{Are u1 and u2 arrays?}
    F -- Yes --> G[Concatenate u1 and u2, assign to v.use]
    F -- No --> H[No change to v.use]
    G --> I[Extract fallback from a (f1) and b (f2)]
    I --> J{Are f1 and f2 objects?}
    J -- Yes --> K[Merge f1 and f2 with mergeObjects, assign to v.fallback]
    J -- No --> L[No change to v.fallback]
    K --> M[Return merged config v]
    L --> M
    H --> I
    C --> End[Return merged config v]

Summary

The merge-config.ts file is a focused utility module providing a robust and flexible way to merge configuration objects in the system. It emphasizes immutability, special handling for array and nested object properties, and leverages shared utilities for merging. This ensures that configuration objects can be combined without loss of data or unintended side effects, supporting the overall modular and scalable architecture of the project.