Skip to content

⬅️ Back to Table of Contents

📄 deepMerge.ts

📊 Analysis Summary

Metric Count
🔧 Functions 2
📊 Variables & Constants 6
📑 Type Aliases 1

📚 Table of Contents

🛠️ File Location:

📂 packages/utils/src/eslint-utils/deepMerge.ts

Variables & Constants

Name Type Kind Value Exported
keys Set<string> const new Set([...Object.keys(first), ...Object.keys(second)])
firstHasKey boolean const key in first
secondHasKey boolean const key in second
firstValue unknown const first[key]
secondValue unknown const second[key]
value any let/var *not shown*

Functions

isObjectNotArray(obj: unknown): obj is ObjectLike

Code
export function isObjectNotArray(obj: unknown): obj is ObjectLike {
  return typeof obj === 'object' && obj != null && !Array.isArray(obj);
}
  • JSDoc:

    /**
     * Check if the variable contains an object strictly rejecting arrays
     * @returns `true` if obj is an object
     */
    

  • Parameters:

  • obj: unknown
  • Return Type: obj is ObjectLike
  • Calls:
  • Array.isArray

deepMerge(first: ObjectLike, second: ObjectLike): Record<string, unknown>

Code
export function deepMerge(
  first: ObjectLike = {},
  second: ObjectLike = {},
): Record<string, unknown> {
  // get the unique set of keys across both objects
  const keys = new Set([...Object.keys(first), ...Object.keys(second)]);

  return Object.fromEntries(
    [...keys].map(key => {
      const firstHasKey = key in first;
      const secondHasKey = key in second;
      const firstValue = first[key];
      const secondValue = second[key];

      let value;
      if (firstHasKey && secondHasKey) {
        if (isObjectNotArray(firstValue) && isObjectNotArray(secondValue)) {
          // object type
          value = deepMerge(firstValue, secondValue);
        } else {
          // value type
          value = secondValue;
        }
      } else if (firstHasKey) {
        value = firstValue;
      } else {
        value = secondValue;
      }
      return [key, value];
    }),
  );
}
  • JSDoc:

    /**
     * Pure function - doesn't mutate either parameter!
     * Merges two objects together deeply, overwriting the properties in first with the properties in second
     * @param first The first object
     * @param second The second object
     * @returns a new object
     */
    

  • Parameters:

  • first: ObjectLike
  • second: ObjectLike
  • Return Type: Record<string, unknown>
  • Calls:
  • Object.keys
  • Object.fromEntries
  • [...keys].map
  • isObjectNotArray
  • deepMerge
  • Internal Comments:
    // get the unique set of keys across both objects (x2)
    // object type (x3)
    // value type (x3)
    

Type Aliases

ObjectLike<T = unknown = unknown>

type ObjectLike<T = unknown = unknown> = Record<string, T>;