Skip to content

Commit

Permalink
fix(decorated-merger.ts): undefined values in new object should not o…
Browse files Browse the repository at this point in the history
…verride old object
  • Loading branch information
Neckaros committed Nov 24, 2018
1 parent 3e6f497 commit a8c3e71
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
7 changes: 3 additions & 4 deletions src/decorated-merger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,11 @@ export const merger = <T extends any, Y extends any>(
}
const allProperties = getAllPropertiesToMerge(merge, options)
for (const property of allProperties) {
const typeNew = typeof merge
if (typeNew === 'undefined' || typeNew === 'function') {
continue
}
const oldValue = into[property.name]
const newValue = merge[property.name]
if (newValue === undefined) {
continue
}
if (oldValue === undefined) {
into[property.name] = newValue
continue
Expand Down
9 changes: 9 additions & 0 deletions test/merge.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,15 @@ describe('Merging', () => {
expect(newMovie.to).toBeDefined()
})

it('Undefinded in new object should not override', () => {
let movieA = { title: 'test', genre: ['Drama'] }
const movieB = { title: undefined, genre: undefined }
const newMovie = merger(movieB, movieA)

expect(newMovie.title).toEqual('test')
expect(newMovie.genre[0]).toEqual('Drama')
})

it('Two simple class with exsiting array should merge', () => {
const car = getTestCar()
const carNew = getTestCar()
Expand Down

0 comments on commit a8c3e71

Please sign in to comment.