Skip to content

Commit

Permalink
fix array override and merge
Browse files Browse the repository at this point in the history
  • Loading branch information
unional committed May 13, 2019
1 parent cfb0916 commit 394ec61
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
22 changes: 18 additions & 4 deletions src/required.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,15 +112,29 @@ describe('requiredDeep()', () => {
}
}

const foo = new Foo()
const actual = requiredDeep({ a: {} }, undefined, { a: { b: foo } })
class Boo extends Foo {
d() { return this.a + 1 }
}
const boo = new Boo()
const actual = requiredDeep({ a: { b: { x: 1 } } }, undefined, { a: { b: boo } })
expect(actual).toEqual({
a: {
b: {
a: 1
a: 1,
x: 1,
c: boo.c,
d: boo.d
}
}
})
expect(actual.a.b.c).toBe(foo.c)
})

test('override array', () => {
const actual = requiredDeep({ a: [1] }, { a: [2] })
expect(actual).toEqual({ a: [2] })
})

test('value add to array', () => {
expect(requiredDeep({ a: [1] }, { a: 2 })).toEqual({ a: [1, 2] })
})
})
2 changes: 1 addition & 1 deletion src/required.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ function merge(entries: any[], reducer: (result: any, entry: any) => any) {
function deepmerge(source1: any, source2: any): any {
if (typeof source1 !== 'object' || source1 === null) return source2 || source1
if (Array.isArray(source1)) {
return Array.isArray(source2) ? source1.concat(source2) : source1.push(source2)
return Array.isArray(source2) ? source2 : [...source1, source2]
}

return getAllKeys(source1).concat(getAllKeys(source2)).reduce((p, k) => {
Expand Down

0 comments on commit 394ec61

Please sign in to comment.