Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature Request: Passing a collision callback for colliding props #262

Open
jorenbroekema opened this issue Dec 19, 2023 · 0 comments
Open

Comments

@jorenbroekema
Copy link

jorenbroekema commented Dec 19, 2023

It would be quite useful to be able to take certain action (such as debug logging) when deepmerging objects that causes properties to collide with one another.

Example:

const obj1 = {
  foo: "foo",
  bar: "bar",
  qux: {
    1: true,
    "hello": "world"
  }
}

const obj2 = {
  foo: "otherFoo", // collision w obj1
  bar: "bar",
  qux: {
    2: false,
    "hello": "planet" // collision w obj1
  }
}

const objMerged = deepmerge(obj1, obj2, { 
  onCollision: ({ target, destination, key, values, path }) => {
    // target = obj1, destination = obj2
    console.log(`Collision found for prop ${key} -> value: ${values[1]} takes precedence over value: ${values[0]}. Path to prop: ${path.join('.')}`);
  }
});

Console output:

Collision found for prop foo -> value: otherFoo takes precedence over value: foo. Path to prop: foo
Collision found for prop hello -> value: planet takes precedence over value: world. Path to prop: qux.hello

callback API is just a first idea for what meta data is useful to pass to a collision function.

I haven't really thought through how this would work fo: functions & other non-plain objects, but for Arrays I suppose it doesn't make that much sense, because they always collide and need to be merged in some way e.g. by concating them. So perhaps a better name is onPropCollision if it only makes sense for object properties.

I'm willing to create a PR for this, just let me know (maintainer(s))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant