Skip to content

Commit

Permalink
fix(assign): fix overriding a nested object with null
Browse files Browse the repository at this point in the history
  • Loading branch information
aleclarson committed Jul 15, 2024
1 parent a9df894 commit 89b2788
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
7 changes: 3 additions & 4 deletions src/object/assign.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,11 @@ export function assign<X extends Record<string | symbol | number, any>>(
const merged = proto
? { ...initial }
: Object.assign(Object.create(proto), initial)
for (const key in override) {
if (Object.prototype.hasOwnProperty.call(override, key)) {
merged[key] = isPlainObject(initial[key])
for (const key of Object.keys(override)) {
merged[key] =
isPlainObject(initial[key]) && isPlainObject(override[key])
? assign(initial[key], override[key])
: override[key]
}
}
return merged
}
4 changes: 4 additions & 0 deletions tests/object/assign.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ describe('assign', () => {
const result = _.assign({}, { b: 'y' })
expect(result).toEqual({ b: 'y' })
})
test('handles null overriding nested object', () => {
const result = _.assign({ a: { b: { c: 1 } } }, { a: { b: null } })
expect(result).toEqual({ a: { b: null } })
})
test('works with Object.create(null)', () => {
const object = { a: Object.create(null) }
object.a.b = 1
Expand Down

0 comments on commit 89b2788

Please sign in to comment.