Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit9d1ebc6

Browse files
committed
Fix deep merge with immutable lists
1 parenta4709aa commit9d1ebc6

File tree

3 files changed

+69
-1
lines changed

3 files changed

+69
-1
lines changed

‎src/reducers/fetch.js‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
importnormalizefrom'../utils/normalize'
22
import{isObject}from'../utils/is'
3+
import{mergeDeepOverwriteLists}from'../utils/mergeDeepOverwriteLists'
34
importImmutablefrom'immutable'
45

56
/**
@@ -46,7 +47,7 @@ export const success = (state, action) => {
4647

4748
// Merge new raw data with existing raw data
4849
constnormalizedData=normalize(action.uidField,Immutable.fromJS(responseData))
49-
map.set('raw',map.get('raw').mergeDeep(normalizedData))
50+
map.set('raw',mergeDeepOverwriteLists(map.get('raw'),normalizedData))
5051
}
5152

5253
// Update instance array with new data
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import{List}from'immutable'
2+
3+
constisMergeable=a=>(
4+
a&&typeofa==='object'&&typeofa.mergeWith==='function'&&!List.isList(a)
5+
)
6+
7+
/**
8+
* Performs a mergeDeep action but overwrites any Immutable Lists
9+
*/
10+
exportconstmergeDeepOverwriteLists=(a,b)=>{
11+
// If b is null, it would overwrite a, even if a is mergeable
12+
if(b===null)returnb
13+
14+
// If a is mergeable and not an Immutable List merge with b
15+
if(isMergeable(a)&&!List.isList(a)){
16+
returna.mergeWith(mergeDeepOverwriteLists,b)
17+
}
18+
19+
// Otherwise replace a with b
20+
returnb
21+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import{expect}from'chai'
2+
import{fromJS,List}from'immutable'
3+
import{mergeDeepOverwriteLists}from'../../src/utils/mergeDeepOverwriteLists'
4+
5+
describe('Utils::MergeDeepOverwriteLists',()=>{
6+
describe('mergeDeepOverwriteLists',()=>{
7+
it('should replace a with b if b is null',()=>{
8+
expect(mergeDeepOverwriteLists('test',null)).to.be.null
9+
})
10+
11+
it('should merge a with b when a is mergeable and not an Immutable List',()=>{
12+
consta=fromJS({
13+
prop1:'test a',
14+
prop2:{
15+
childProp1:'child test a'
16+
},
17+
prop3:[123,234],
18+
prop4:null
19+
})
20+
21+
constb=fromJS({
22+
prop1:'test b',
23+
prop2:{
24+
childProp1:null
25+
},
26+
prop3:[234],
27+
prop4:'b not null'
28+
})
29+
30+
constexpectedResult=fromJS({
31+
prop1:'test b',
32+
prop2:{
33+
childProp1:null
34+
},
35+
prop3:[234],
36+
prop4:'b not null'
37+
})
38+
39+
expect(mergeDeepOverwriteLists(a,b)).to.deep.equal(expectedResult)
40+
})
41+
42+
it('should replace a with b if a is an Immutable List',()=>{
43+
expect(mergeDeepOverwriteLists(List([123,234]),List([234]))).to.deep.equal(List([234]))
44+
})
45+
})
46+
})

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp