Skip to content

Commit 18cf12f

Browse files
committed
Change the type of set container depending on the type of key.
1 parent 751082f commit 18cf12f

File tree

2 files changed

+29
-6
lines changed

2 files changed

+29
-6
lines changed

__tests__/updateIn.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,20 @@ describe('updateIn', () => {
8787
});
8888
});
8989

90+
it('deep set with numeric key 1', () => {
91+
const m = fromJS({ a: { b: 1 } });
92+
expect(m.updateIn(['a', 'c', 0], () => 20).toJS()).toEqual({
93+
a: { b: 1, c: [20] },
94+
});
95+
});
96+
97+
it('deep set with numeric key 2', () => {
98+
const m = fromJS({});
99+
expect(m.updateIn(['a', 0], () => 20).toJS()).toEqual({
100+
a: [20],
101+
});
102+
});
103+
90104
it('deep push', () => {
91105
const m = fromJS({ a: { b: [1, 2, 3] } });
92106
expect(m.updateIn(['a', 'b'], list => list.push(4)).toJS()).toEqual({

src/functional/updateIn.js

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import isDataStructure from '../utils/isDataStructure';
1111
import quoteString from '../utils/quoteString';
1212
import { NOT_SET } from '../TrieUtils';
1313
import { emptyMap } from '../Map';
14+
import { emptyList } from '../List';
1415
import { get } from './get';
1516
import { remove } from './remove';
1617
import { set } from './set';
@@ -66,10 +67,18 @@ function updateInDeeply(
6667
return nextUpdated === nextExisting
6768
? existing
6869
: nextUpdated === NOT_SET
69-
? remove(existing, key)
70-
: set(
71-
wasNotSet ? (inImmutable ? emptyMap() : {}) : existing,
72-
key,
73-
nextUpdated
74-
);
70+
? remove(existing, key)
71+
: set(
72+
wasNotSet
73+
? inImmutable
74+
? typeof key === 'number'
75+
? emptyList()
76+
: emptyMap()
77+
: typeof key === 'number'
78+
? []
79+
: {}
80+
: existing,
81+
key,
82+
nextUpdated
83+
);
7584
}

0 commit comments

Comments
 (0)