From fe78faab5563a88ce4b145185e317c0a96049ce2 Mon Sep 17 00:00:00 2001 From: Yousef Date: Wed, 12 Jun 2024 08:36:44 +0200 Subject: [PATCH] fix(core): mergeDeep now can merge nulls (#4088) --- packages/core/src/utilities/mergeDeep.ts | 10 ++--- .../integration/core/mergeDeep.spec.ts | 45 +++++++++++++++++++ 2 files changed, 48 insertions(+), 7 deletions(-) diff --git a/packages/core/src/utilities/mergeDeep.ts b/packages/core/src/utilities/mergeDeep.ts index 5c49cfa5d41..ab32d72c9ef 100644 --- a/packages/core/src/utilities/mergeDeep.ts +++ b/packages/core/src/utilities/mergeDeep.ts @@ -5,14 +5,10 @@ export function mergeDeep(target: Record, source: Record { - if (isPlainObject(source[key])) { - if (!(key in target)) { - Object.assign(output, { [key]: source[key] }) - } else { - output[key] = mergeDeep(target[key], source[key]) - } + if (isPlainObject(source[key]) && isPlainObject(target[key])) { + output[key] = mergeDeep(target[key], source[key]) } else { - Object.assign(output, { [key]: source[key] }) + output[key] = source[key] } }) } diff --git a/tests/cypress/integration/core/mergeDeep.spec.ts b/tests/cypress/integration/core/mergeDeep.spec.ts index 2d1af093675..31e18e7416d 100644 --- a/tests/cypress/integration/core/mergeDeep.spec.ts +++ b/tests/cypress/integration/core/mergeDeep.spec.ts @@ -19,6 +19,21 @@ describe('mergeDeep', () => { expect(merged).to.deep.eq(result) }) + it('should merge when source has null value', () => { + const one = { + a: null, + } + const two = { + a: { c: 3 }, + } + const result = { + a: { c: 3 }, + } + const merged = mergeDeep(one, two) + + expect(merged).to.deep.eq(result) + }) + it('should not merge array', () => { const one = { a: [1], @@ -34,6 +49,36 @@ describe('mergeDeep', () => { expect(merged).to.deep.eq(result) }) + it('should merge when source has null value', () => { + const one = { + a: null, + } + const two = { + a: { c: 3 }, + } + const result = { + a: { c: 3 }, + } + const merged = mergeDeep(one, two) + + expect(merged).to.deep.eq(result) + }) + + it('should allow nulling a value', () => { + const one = { + a: { c: 3 }, + } + const two = { + a: { c: null }, + } + const result = { + a: { c: null }, + } + const merged = mergeDeep(one, two) + + expect(merged).to.deep.eq(result) + }) + it('should merge deep', () => { const one = { a: 1,