All files / util deepMerge.ts

94.44% Statements 17/18
80% Branches 4/5
100% Functions 4/4
100% Lines 13/13

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 181x   1x 13x 13x 14x 7x 5x 11x 5x 8x 18x   8x   5x    
import { object } from "ts-matches"
 
export function deepMerge(...args: unknown[]): unknown {
  const lastItem = (args as any)[args.length - 1]
  if (!object.test(lastItem)) return lastItem
  const objects = args.filter(object.test).filter((x) => !Array.isArray(x))
  if (objects.length === 0) return lastItem as any
  Iif (objects.length === 1) objects.unshift({})
  const allKeys = new Set(objects.flatMap((x) => Object.keys(x)))
  for (const key of allKeys) {
    const filteredValues = objects.flatMap((x) =>
      key in x ? [(x as any)[key]] : [],
    )
    ;(objects as any)[0][key] = deepMerge(...filteredValues)
  }
  return objects[0] as any
}