TehShrike / deepmerge

A library for deep (recursive) merging of Javascript objects
MIT License
2.75k stars 216 forks source link

[Feature request] Pass parent values to `customMerge()` #263

Open broofa opened 7 months ago

broofa commented 7 months ago

Problem: We'd like to require certain merge paths to always be defined.

For example: If attempting to merge objects a and b, and b.properties is defined, we'd like to throw if `a.properties is not defined.

Current solution: Per this comment, this is accomplished with a customMerge function that looks something like this:

// merge options
customMerge(key) {
  if (key === 'properties') {
    return function (a, b) {
      if (!a) throw Error('properties is not defined');
    }
  }
}

This is awkward. It's just weird that the test for property name and value(s) is split across two different functions.

Proposed solution: Extend the customMerge() method signature to take key, object_a, object_b. This would allow the code above to be simplified:

// merge options
customMerge(key, a, b) {
  if (key === 'properties' && !a) {
    throw Error('properties is not defined');
  }
}