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

Also handle cloneProtoObject in mergeObject #45

Merged
merged 9 commits into from
Dec 22, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,9 @@ function deepmergeConstructor (options) {
for (i = 0, il = sourceKeys.length; i < il; ++i) {
isNotPrototypeKey(key = sourceKeys[i]) &&
(
key in target && (targetKeys.indexOf(key) !== -1 && (result[key] = _deepmerge(target[key], source[key])), true) || // eslint-disable-line no-mixed-operators
key in target && (targetKeys.indexOf(key) !== -1 && // eslint-disable-line no-mixed-operators
((isMergeableObject(source[key]) && cloneProtoObject && Object.getPrototypeOf(source[key]) !== JSON_PROTO && (result[key] = cloneProtoObject(source[key]))) ||
(result[key] = _deepmerge(target[key], source[key]))), true) || // eslint-disable-line no-mixed-operators
(result[key] = clone(source[key]))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is illegible. Could you refactor it into legible if or switch statements?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Of course. Just tried to maintain the same illegible style that was used here beforehand hehe.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Take a look now. Hopefully I got it right thanks to the tests. But it was truly a confusing piece of code.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I know you were just following what was laid down previously. Thank you for taking on the clean up.

)
}
Expand Down
23 changes: 23 additions & 0 deletions test/merge-proto-objects.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ const { Readable } = require('node:stream')
const deepmerge = require('../index')
const { test } = require('tape')

class Foo {
constructor (foo) {
this.foo = foo
}
}

test('merge nested objects should be immutable', function (t) {
t.plan(3)
const src = {
Expand Down Expand Up @@ -86,6 +92,23 @@ test('should not merge the buffers when cloned by reference', async t => {
t.same(result.logger.buffer, Buffer.of(1, 2, 3))
})

test('should clone by refernce with proto object in both source and target', async t => {
segevfiner marked this conversation as resolved.
Show resolved Hide resolved
t.plan(4)
const foo2 = new Foo(2)
const result = deepmerge({
cloneProtoObject (x) {
t.ok(x instanceof Foo)
return x
}
})(
{ foo: new Foo(1) },
{ foo: foo2 }
)
t.equal(typeof result.foo, 'object')
t.ok(result.foo instanceof Foo)
t.same(result.foo, foo2)
})

test('doc example', async t => {
const stream = process.stdout

Expand Down
Loading