Skip to content
This repository has been archived by the owner on Jun 12, 2024. It is now read-only.

Commit

Permalink
Merge pull request #186 from chitoku-k/fix/merge
Browse files Browse the repository at this point in the history
Fix incorrect merge behavior
  • Loading branch information
MatteoGabriele authored Mar 9, 2019
2 parents c774f50 + 8ff9b0e commit d92b97d
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
17 changes: 17 additions & 0 deletions __tests__/helpers.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,23 @@ describe('merge', () => {
}
})
})

it ('should merge objects containing non-objects', () => {
const a = { c: [ 1, 2, 3 ], d: Promise.resolve({ a: 1 }) }
const b = { c: [ 4, 5 ], d: { b: 1 } }
const c = helpers.merge(a, b)

expect(c).toEqual({
c: [
4,
5,
3
],
d: {
b: 1
}
})
})
})

describe('getMethod', () => {
Expand Down
4 changes: 3 additions & 1 deletion src/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ export function getBasePath (base, path) {

export function merge (obj, src) {
Object.keys(src).forEach(function (key) {
if (obj[key] && typeof obj[key] === 'object') {
const type = obj[key] && Object.prototype.toString.call(obj[key])

if (type === '[object Object]' || type === '[object Array]') {
merge(obj[key], src[key])
return
}
Expand Down

0 comments on commit d92b97d

Please sign in to comment.