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 1 commit
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
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ function deepmergeConstructor (options) {
isNotPrototypeKey(key = sourceKeys[i]) &&
(
key in target && (targetKeys.indexOf(key) !== -1 && // eslint-disable-line no-mixed-operators
((isMergeableObject(key) && cloneProtoObject && Object.getPrototypeOf(source[key]) !== JSON_PROTO && (result[key] = cloneProtoObject(source[key]))) ||
((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]))
)
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