TehShrike / deepmerge

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

customMerge does not iterate through all attributes #139

Closed phof closed 5 years ago

phof commented 5 years ago

customMerge seems to be iterating only through attributes of type array or object

Steps to reproduce:

const merge = require('deepmerge')

const alex = {
  object: {
    first: 'Alex',
    last: 'Alexson'
  },
  array: ['Cat', 'Parrot'],
  string: 'hello',
  number: 1,
  bool: true
}

const tony = {
  object: {
    first: 'Tony',
    last: 'Tonison'
  },
  array: ['Dog'],
  string: 'world',
  number: 2,
  bool: false
}

const options = {
  customMerge: (key) => {
    console.log(key)
  }
}

merge(alex, tony, options)

This logs to console object and array, but not string, number, bool

I'd imagine this is because of https://github.com/TehShrike/deepmerge/blob/master/index.js#L35 but unclear if this is expected/WAD.

phof commented 5 years ago

cc/ @OliverRadini

TehShrike commented 5 years ago

This is expected because of how this library uses the term "merge" to refer to combining two data structures.

By default: if both the source and the target are objects, or they are both arrays, they can be merged with each other. If they are different types, or are not objects or arrays, they can not be merged with each other, and the target is overridden by the value from the source.

However, as you saw in the code there, you can pass in an isMergeableObject option to override the default definition of what is or isn't a "mergeable" value - if you pass in a function that claims that strings are mergeable, then those properties will be passed to your customMerge function.