Skip to content

Commit 341b65e

Browse files
committed
fix mergeWith to keep 'falsy' properties from right side
1 parent f36630f commit 341b65e

File tree

2 files changed

+69
-1
lines changed

2 files changed

+69
-1
lines changed

src/utils/__tests__/mergeWith.test.js

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import mergeWith from '../mergeWith'
2+
3+
describe('When mering props with `mergeWith`', () => {
4+
describe('and we are using "apply left prop" as resolver', () => {
5+
const resolver = x => x
6+
it('it should keep all defined values on left side.', () => {
7+
const a = {
8+
string: 'string',
9+
zero: 0,
10+
negaive: -1,
11+
float: 0.555555,
12+
deep: {
13+
er: 'foo',
14+
},
15+
array: [0, 1],
16+
emptyArray: [],
17+
}
18+
expect(mergeWith(a, {}, resolver)).toMatchObject({
19+
string: 'string',
20+
zero: 0,
21+
negaive: -1,
22+
float: 0.555555,
23+
deep: {
24+
er: 'foo',
25+
},
26+
array: [0, 1],
27+
emptyArray: [],
28+
})
29+
})
30+
31+
it('it should keep all defined values on right side.', () => {
32+
const b = {
33+
string: 'string',
34+
zero: 0,
35+
negaive: -1,
36+
float: 0.555555,
37+
deep: {
38+
er: 'foo',
39+
},
40+
array: [0, 1],
41+
emptyArray: [],
42+
}
43+
expect(mergeWith({}, b, resolver)).toMatchObject({
44+
string: 'string',
45+
zero: 0,
46+
negaive: -1,
47+
float: 0.555555,
48+
deep: {
49+
er: 'foo',
50+
},
51+
array: [0, 1],
52+
emptyArray: [],
53+
})
54+
})
55+
56+
it('it should copy existing values and use left one on conflict.', () => {
57+
const a = {
58+
string: 'string',
59+
}
60+
const b = {
61+
string: 'my string',
62+
}
63+
expect(mergeWith(a, b, resolver)).toMatchObject({
64+
string: 'string',
65+
})
66+
})
67+
})
68+
})

src/utils/mergeWith.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const mergeWith = (x, y, fn) => {
55
Object.keys(y).forEach((key) => {
66
if (x[key] && y[key]) {
77
result[key] = fn(x[key], y[key], key)
8-
} else if (y[key]) {
8+
} else {
99
result[key] = y[key]
1010
}
1111
})

0 commit comments

Comments
 (0)